当前位置: 首页 > Web前端 > HTML

无缝轮播图

时间:2023-04-02 22:20:03 HTML

html:

prenext
css:.container{position:relative;width:500px;高度:330px;溢出:隐藏;}.carousel-image{位置:绝对;宽度:3000px;}js:letpre=document.getElementById("pre");letnext=document.getElementById("next");letcarouselImage=document.getElementsByClassName("carousel-image");console.log(carouselImage[0].style.left)//注意:.style.left只能获取内联样式的标签左边,否则为Empty.offsetLeft可以获取标签的左边//向右滑动next.onclick=function(){animate(-500);}pre.onclick=function(){animate(500);}//移动函数functionanimate(offset){//为什么第二次调用go()函数时offset是NaNvarleftDistance=parseInt(carouselImage[0].style.left)+offset;varnewoffset=offset;varspeed=newoffset/20;//注意:如果将这行代码放在go()函数中,第二次执行go()时newoffset将为NaN,导致定时器失效。应该和作用域链的知识点有关。每步速度>0的移动距离向右//动画函数functiongo(newoffset){currentLeft=carouselImage[0].style.left;//当前图片的位置if(speed>0&&leftDistance>parseInt(currentLeft)||speed<0&&leftDistance-500){car??ouselImage[0].style.left=-1500+"px";}}去(新偏移量);}效果图微信公众号:天子一等