1. 准备图片,最好是宽高一样的。设置图片的宽度
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="images">
<img src="https://i.loli.net/2018/10/13/5bc1604f2a255.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f3b560.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f83051.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f836b9.jpg" width=480 alt="">
</div>
</body>
</html>
2. 设置一个窗口;让图片横向显示;设置边框,然后观察
<div class="window">
<div class="images">
<img src="https://i.loli.net/2018/10/13/5bc1604f2a255.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f3b560.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f836b9.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f83051.jpg" width=480 alt="">
</div>
</div>
<style>
.images{
display: flex;
align-items: flex-start;/*交叉轴的起点对齐*/
}
.window{
border: 3px solid green;
}
</style>
图片超出了绿色边框的宽度,绿色边框的宽度是和页面宽度相同。
3. 设置窗口的宽高,和第一个图片宽高相同,然后设置 overflow: hidden ;
<style>
.images{
display: flex;
align-items: flex-start;
}
.window{
border: 3px solid green;
width: 480px;
height: 320px;
overflow: hidden;
}
此时显示的只有第一张图片,只需要改变images 的水平位置,就可以看到其他图片:
<div class="window">
<div class="images" style="transform: translateX(-480px)">
<img src="https://i.loli.net/2018/10/13/5bc1604f2a255.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f3b560.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f836b9.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f83051.jpg" width=480 alt="">
</div>
</div>
4. 添加 button ,通过 js 控制使点击显示相应图片,初步实现轮播
<div class="window">
<div class="images" id="images">
<img src="https://i.loli.net/2018/10/13/5bc1604f2a255.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f3b560.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f836b9.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f83051.jpg" width=480 alt="">
</div>
</div>
<button id="p1">第1张</button>
<button id="p2">第2张</button>
<button id="p3">第3张</button>
<button id="p4">第4张</button>
<style>
.images{
display: flex;
align-items: flex-start;
transition: transform 0.5s;
}
.window{
width: 480px;
height: 320px;
overflow: hidden;
}
</style>
$(p1).on('click',function(){
$(images).css({
transform: 'translateX(0)'})
})
$(p2).on('click',function(){
$(images).css({
transform: 'translateX(-480px)'
})
})
$(p3).on('click',function(){
$(images).css({
transform: 'translateX(-960px)'
})
})
$(p4).on('click',function(){
$(images).css({
transform: 'translateX(-1440px)'
})
})
5. 调整代码,可以支持更多张图片轮播
<div class="window">
<div class="images" id="images">
<img src="https://i.loli.net/2018/10/13/5bc1604f2a255.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f3b560.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f836b9.jpg" width=480 alt="">
<img src="https://i.loli.net/2018/10/13/5bc1604f83051.jpg" width=480 alt="">
</div>
</div>
<span id="buttons">
<span>第1张</span>
<span>第2张</span>
<span>第3张</span>
<span>第4张</span>
</span>
var allButtons = $('#buttons > span')
for( let i=0; i<allButtons.length; i++){
$(allButtons[i]).on('click',function(x){
var index = $(x.currentTarget).index()
//index()可以得到当前元素在父元素下所有子元素中排在第几个,并返回这个值
var n = index*-480
$('#images').css({
transform: 'translateX('+ n + 'px)'
})
})
}
6. 实现轮播自动播放
var allButtons = $('#buttons > span')
for( let i=0; i<allButtons.length; i++){
$(allButtons[i]).on('click',function(x){
var index = $(x.currentTarget).index()
var n = index*-480
$('#images').css({
transform: 'translateX('+ n + 'px)'
})
})
}
var a = 0
var size = allButtons.length
allButtons.eq(a%size).trigger('click')
//eq()找出对应的dom,封装成对应的 jQuery;trigger用于触发事件
setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
},1000) // 实现自动轮播
7. 文字高亮
.red{
color: red;
}
var a = 0
var size = allButtons.length
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
},1000) // 实现自动轮播;添加和删除class实现相应文字的高亮
8. 鼠标移入图片时自动轮播停止,移开时恢复自动轮播
var a = 0
var size = allButtons.length
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
var timerId = setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
},1000)
$('.window').on('mouseenter',function(){
window.clearInterval(timerId)
})
$('.window').on('mouseleave',function(){
timerId = setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
},1000)
})
9. 点击文字时文字高亮
var allButtons = $('#buttons > span')
for( let i=0; i<allButtons.length; i++){
$(allButtons[i]).on('click',function(x){
var index = $(x.currentTarget).index()
var n = index*-480
$('#images').css({
transform: 'translateX('+ n + 'px)'
})
a = index
allButtons.eq(a)
.addClass('red')
.siblings('.red').removeClass('red')
})
}
var a = 0
var size = allButtons.length
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
var timerId = setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
},3000)
$('.window').on('mouseenter',function(){
window.clearInterval(timerId)
})
$('.window').on('mouseleave',function(){
timerId = setInterval(()=>{
a += 1
allButtons.eq(a%size).trigger('click')
.addClass('red')
.siblings('.red').removeClass('red')
},3000)
})
10. 简化代码
var allButtons = $('#buttons > span')
for( let i=0; i<allButtons.length; i++){
$(allButtons[i]).on('click',function(x){
var index = $(x.currentTarget).index()
var n = index*-480
$('#images').css({
transform: 'translateX('+ n + 'px)'
})
a = index
activeButtons(allButtons.eq(a))
})
}
var a = 0
var size = allButtons.length
playSlide(a%size)
var timerId = setTimer()
function playSlide(index){
allButtons.eq(index).trigger('click')
}
function setTimer(){
return setInterval(()=>{
a += 1
playSlide(a%size)
},3000)
}
function activeButtons($buttons){
$buttons.addClass('red')
.siblings('.red').removeClass('red')
}
$('.window').on('mouseenter',function(){
window.clearInterval(timerId)
})
$('.window').on('mouseleave',function(){
timerId = setTimer()
})