js实现无缝轮播图特效
用原生js实现无缝轮播图,供大家参考,具体内容如下
index.js:
varconfig={ imgWidth:380,//图片尺寸 dotWidth:8,//小圆点尺寸 doms:{ divImgs:document.querySelector('.imgs'), divDots:document.querySelector('.circle'), divDirection:document.querySelector('.direction'), divContainer:document.querySelector('.container') }, curIndex:0,//实际图片索引,0~imgNumber-1 timer:{ duration:16,//运动间隔时间 total:1000,//总时间 id:null//计时器编号 } } //图片的数量 config.imgNumber=config.doms.divImgs.children.length; //初始化元素尺寸 config.imgsWidth=(config.imgNumber+2)*config.imgWidth; config.dotsWidth=(config.imgNumber+2)*config.dotWidth; //初始化 functioninti(){ intiWidth(); intiCount(); intiElement(); intiPosition(); functionintiWidth(){ config.doms.divImgs.style.width=config.imgsWidth+'px'; config.doms.divDots.style.width=config.dotsWidth+'px'; } functionintiCount(){ for(vari=0;imarginLeft){ distance=newLeft-marginLeft; }else{ distance=totalWidth-Math.abs(newLeft-marginLeft); } } //每次改变的距离 vareverDistence=distance/number; //逐步改变marginLeft functionanimateSwitch(){ clearAnimate(); config.timer.id=setInterval(function(){ marginLeft+=everDistence; if(directions==='left'&&Math.abs(marginLeft)>totalWidth){ marginLeft+=totalWidth; } elseif(directions==='right'&&Math.abs(marginLeft) config.imgNumber-1){ index=0; } switchTo(index,'left'); } //小圆点点击事件 config.doms.divDots.onclick=function(e){ if(e.target.tagName==='P'){ varindex=Array.from(this.children).indexOf(e.target); switchTo(index,index>config.curIndex?'left':'right') } }
index.html:
无缝轮播图