如何使用setInterval实现简单的轮播图效果?

<div>
    <img src="./images/1.jpg" id="imgS" />
</div>
<button type="button">下一张</button>
<button type="button">上一张</button>
 <script >

var imgs = ["./images/1.jpg", “./images/2.jpg”, “./images/3.jpg”, “./images/4.jpg”, “./images/5.jpg”, “./images/6.jpg”];
var img=document.getElementById(“imgS”);
var btns=document.getElementsByTagName(“button”);
var ind = 0;
btns[0].onclick=function(){
ind++;
if(indimgs.length){ind=0;}
img.src=imgs[ind];
}
btns[1].onclick=function(){
ind–;
if(ind
-1){ind=imgs.length-1;}
img.src=imgs[ind];
}
function change(){
ind=(ind+1)%6;
img.src=imgs[ind];
}
var timer=setInterval(change,21000);
img.onmouseover=function(){
clearInterval(timer);
}
img.onmouseout=function(){
timer=setInterval(change,2
1000);
}

</script>

猜你喜欢

转载自blog.csdn.net/weixin_43748930/article/details/85852237
今日推荐