轮播图demo左右滑动-pc端
这里使用的是自己封装的动画,最完整,但是有点繁琐,还实现了节流的功能
外面只引入了四张图片的地址

有注释,直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图demo左右滑动-pc端</title>
<style>
li,i,ul{
list-style: none;
margin: 0;
padding: 0;
}
.swiper{
margin: 100px auto;
overflow: hidden;
width: 1226px;
height: 460px;
position: relative;
}
.swiper ul{
position: absolute;
top: 0;
left: 0;
height: 460px;
width: 500%;
}
.swiper ul li{
width: 1226px;
height: 460px;
float: left;
}
.swiper img{
display: block;
width: 100%;
height: 100%;
}
.swiper span{
font-size: 25px;
display: block;
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
color: #ccc;
margin-top: -25px;
position: absolute;
top: 50%;
display: none;
cursor: pointer;
}
.swiper .button-right{
right: 0;
}
.swiper .button-left{
left: 0;
}
.swiper span:hover{
background-color: #e0e0e0;
color: black;
}
.bullets{
position: absolute;
bottom: 30px;
right: 60px;
width: 300px;
height: 20px;
text-align: right;
}
.bullets i{
display: inline-block;
height: 10px;
width: 10px;
border: 3px solid #cccccc;
background-color: #e0e0e0;
border-radius: 50%;
margin: 0 3px;
}
.bullets .active{
background-color: #ffffff;
}
</style>
</head>
<body>
<div class="swiper">
<!-- 轮播的图片 -->
<ul></ul>
<!-- 左右按钮 -->
<span class="button-left"><</span>
<span class="button-right">></span>
<!-- 小圆圈 -->
<div class="bullets"></div>
</div>
<script>
let num=0
let circle=0
let flag=true
let swiper=document.querySelector('.swiper')
let ul=document.querySelector('.swiper ul')
let bullets=document.querySelector('.bullets')
let button_left=document.querySelector('.button-left')
let button_right=document.querySelector('.button-right')
let arr=['./img/main-back-1.jpg','./img/main-back-2.jpg','./img/main-back-3.jpg','./img/main-back-4.jpg']
let html=''
for(let i=0;i<arr.length;i++){
html+='<li><a><img src='+arr[i]+'></img></a></li>'
let ii=document.createElement('i')
ii.setAttribute('index',i)
bullets.appendChild(ii)
}
ul.innerHTML=html
let last_li=ul.children[0].cloneNode(true)
ul.appendChild(last_li)
bullets.children[0].className='active'
swiper.addEventListener('mouseenter',function(){
button_left.style.display="block"
button_right.style.display="block"
clearInterval(timer)
timer=null
})
swiper.addEventListener('mouseleave',function(){
button_left.style.display="none"
button_right.style.display="none"
timer=setInterval(function(){
button_right.click()
},2000)
})
button_right.addEventListener('click',function(){
if(flag){
flag=false
if(num==ul.children.length-1){
num=0
ul.style.left=0
}
num++
quan_animate(ul,-num*swiper.offsetWidth,function(){flag=true})
circle++
if(circle==bullets.children.length) circle=0
circleChange()
}
})
button_left.addEventListener('click',function(){
if(flag){
flag=false
if(num==0){
num=ul.children.length-1
ul.style.left=-num*swiper.offsetWidth+'px'
}
num--
quan_animate(ul,-num*swiper.offsetWidth,function(){flag=true})
circle--
if(circle<0) circle=bullets.children.length-1
circleChange()
}
})
for(let i=0;i<bullets.children.length;i++){
bullets.children[i].addEventListener('click',function(){
circle=this.getAttribute('index')
circleChange()
num=circle
quan_animate(ul,-num*swiper.offsetWidth)
})
}
function circleChange(){
for(let i=0;i<bullets.children.length;i++){
bullets.children[i].className=''
}
bullets.children[circle].className='active'
}
let timer=setInterval(function(){
button_right.click()
},2000)
function quan_animate(obj,target,callback){
clearInterval(obj.timer)
obj.timer=setInterval(function(){
let step=(target-obj.offsetLeft)/10
step=step>0?Math.ceil(step):Math.floor(step)
if(obj.offsetLeft==target){
clearInterval(obj.timer)
callback&&callback()
}
obj.style.left=obj.offsetLeft+step+'px'
},15)
}
</script>
</body>
</html>
作为自己学习过程的总结,菜鸡一枚,有什么问题与错误,请不吝赐教