js實現(xiàn)無縫輪播圖
本文實例為大家分享了js實現(xiàn)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
CSS樣式:
<style type="text/css"> #box{width: 1000px;height: 375px;border: 3px solid black; margin: 30px auto;position: relative;overflow: hidden;} #box .img{position: absolute;left: 0;top: 0;} #box .img img{width: 1000px;height: 375px;float: left;} #box .btn input{border-radius: 50%;border: 0;width: 40px; height: 40px;font-size: 25px;z-index: 5;top: 165px;position: absolute;} #left{left: 0;} #right{right: 0;} </style>
HTML結(jié)構(gòu):
<div id="box"> <div class="img"> <img src="img/1.jpg" > <img src="img/2.jpg" > <img src="img/3.jpg" > <img src="img/4.jpg" > <img src="img/1.jpg" > //利用第一張圖,達到無縫輪播的視覺效果 </div> <div class="btn"> <input type="button" id="left" value="<" /> <input type="button" id="right" value=">" /> </div> </div>
JavaScript:
<script> class LunBO{ constructor() { this.img = document.querySelector(".img"); this.child = this.img.children; this.left = document.getElementById("left"); this.right = document.getElementById("right"); this.index = 0; } init(){ this.img.style.width = this.child.length * this.child[0].offsetWidth + "px"; //有多少圖片設(shè)置多少寬度 } addEvent(){ var that = this; this.left.onclick = function(){ that.changeIndex(1); //點擊左按鈕,設(shè)置索引 } this.right.onclick = function(){ that.changeIndex(2); //點擊右按鈕,設(shè)置索引 } } changeIndex(i){ if(i==1){ if(this.index == 0){ this.index = this.child.length - 2; this.img.style.left = -(this.child.length-1) * this.child[0].offsetWidth + "px"; }else{ this.index--; } }else{ if(this.index == this.child.length-1){ this.index = 1; this.img.style.left = 0; }else{ this.index++; } } this.setSplace(); } setSplace(){ move(this.img,"left",-this.index * this.child[0].offsetWidth); //調(diào)用動畫 } } var lb = new LunBO(); lb.init(); lb.addEvent(); </script>
設(shè)置動畫:
function move(ele,attr,target){ clearInterval(ele.t); ele.t=setInterval(()=>{ let iNow; if(attr=="opacity"){ iNow=getStyle(ele,attr)*100; }else{ iNow=parseInt(getStyle(ele,attr)); } let speed=(target-iNow)/8; speed=speed<0?Math.floor(speed):Math.ceil(speed); if(iNow==target){ clearInterval(ele.t); }else{ if(attr=="opacity"){ ele.style.opacity=(iNow+speed)/100; }else{ ele.style[attr]=iNow+speed+"px"; } } },30) } function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; }else{ return getComputedStyle(ele,false)[attr]; } }
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解js模板引擎art template數(shù)組渲染的方法
art-template 是一個簡約、超快的模板引擎。這篇文章主要介紹了詳解js模板引擎art template數(shù)組渲染的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10JavaScript將Web頁面內(nèi)容導(dǎo)出到Word及Excel的方法
這篇文章主要介紹了JavaScript將Web頁面內(nèi)容導(dǎo)出到Word及Excel的方法,涉及javascript操作ActiveXObject控件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02Webpack中SplitChunksPlugin 配置參數(shù)詳解
這篇文章主要介紹了Webpack中SplitChunksPlugin 配置參數(shù)詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03webuploader實現(xiàn)上傳圖片到服務(wù)器功能
這篇文章主要為大家詳細介紹了webuploader實現(xiàn)上傳圖片到服務(wù)器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08getAsDataURL在Firefox7.0下無法預(yù)覽本地圖片的解決方法
本文是對getAsDataURL在Firefox7.0下無法預(yù)覽本地圖片的解決方法。進行了分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11JS前端開發(fā)模擬虛擬dom轉(zhuǎn)真實dom詳解
這篇文章主要為大家介紹了JS前端開發(fā)模擬虛擬dom轉(zhuǎn)真實dom詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01