原生js滑動(dòng)輪播封裝
本文實(shí)例為大家分享了原生js滑動(dòng)輪播的具體代碼,供大家參考,具體內(nèi)容如下
封裝滑動(dòng)輪播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>cmm無縫輪播</title> <style type="text/css"> *{margin: 0 ;padding : 0} #container{ height: 470px; width: 590px; border: 1px solid red; position: relative; margin: 50px auto; } #box{ position: absolute; list-style: none; } #box li{ float: left; } #pages { width: 100%; height: 30px; background: #ccc; position: absolute; bottom: 0; } #pages i { width: 20px; height: 20px; display: inline-block; border-radius: 10px; background: #fff; margin: 5px; } #pages i.current { background: #f00; } #prev, #next { width: 45px; height: 100px; position: absolute; top: 0; bottom: 0; margin: auto; background: #ccc; line-height: 100px; text-align: center; font-size: 40px; color: #fff; } #next { right: 0; } </style> </head> <body> <div id="container"> <ul id="box"> <li><img src="images/1.jpg"></li> <li><img src="images/2.jpg"></li> <li><img src="images/3.jpg"></li> <li><img src="images/4.jpg"></li> </ul> <div id="pages"></div> <div id="prev"><</div> <div id="next">></div> </div> <script src="js/tools.js"></script> <script> var lis = $("li"), length = lis.length, liWidth = lis[0].clientWidth, currentIndex = 0, nextIndex = 1, timer = null, move = null, circls = null, durations = 2000; // 動(dòng)態(tài)設(shè)置ul寬度 $("#box").style.width = length * liWidth + "px"; // 動(dòng)態(tài)設(shè)置小圓點(diǎn) var html = ""; for(var i = 0 ;i <length ;i++){ html += "<i></i>" } $("#pages").innerHTML= html; circls = $("i"); circls[0].className = "current"; // 切換動(dòng)畫 move = function(){ // 設(shè)置box運(yùn)動(dòng)終點(diǎn)值 var _left = -1 * nextIndex * liWidth; // 開始動(dòng)畫 animate($("#box"),{left:_left},200) // 修改小圓點(diǎn)樣式 circls[currentIndex].className = ""; circls[nextIndex].className = "current"; // 修改索引 currentIndex = nextIndex; nextIndex++; if(nextIndex >= length){ nextIndex = 0; } } // 自動(dòng)動(dòng)畫 timer = setInterval(move, durations) // container中鼠標(biāo)移入,移出事件 on($("#container"),"mouseenter",function(){ clearInterval(timer); }) on($("#container"),"mouseleaver",function(){ timer = setInterval(move, durations); }) // 點(diǎn)擊小圓點(diǎn),切換至對應(yīng)的圖片 on($("#pages"),"click",function(e){ e = e || event; var src = e.target || src.Element; if(src.nodeName === "I"){ var _index = Array.from(circls).indexOf(src); if(_index === currentIndex){ return } nextIndex = _index; move(); } }) // 點(diǎn)擊翻頁進(jìn)行切換 on($("#prev"),"click",function(){ nextIndex = currentIndex - 1; if(nextIndex < 0){ nextIndex = length; } move(); }) on($("#next"),"click",function(){ move(); }) </script> </body> </html>
更多關(guān)于輪播圖效果的專題,請點(diǎn)擊下方鏈接查看學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)移動(dòng)端輪播圖滑動(dòng)切換
- 原生JS無縫滑動(dòng)輪播圖
- js原生實(shí)現(xiàn)移動(dòng)端手指滑動(dòng)輪播圖效果的示例
- js實(shí)現(xiàn)從左向右滑動(dòng)式輪播圖效果
- JS實(shí)現(xiàn)自動(dòng)輪播圖效果(自適應(yīng)屏幕寬度+手機(jī)觸屏滑動(dòng))
- JS實(shí)現(xiàn)touch 點(diǎn)擊滑動(dòng)輪播實(shí)例代碼
- js實(shí)現(xiàn)固定寬高滑動(dòng)輪播圖效果
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- 原生js實(shí)現(xiàn)移動(dòng)開發(fā)輪播圖、相冊滑動(dòng)特效
- js實(shí)現(xiàn)滑動(dòng)輪播效果
相關(guān)文章
在javascript中,如果刪除二維數(shù)組中重復(fù)的元素
在javascript中,如果刪除二維數(shù)組中重復(fù)的元素...2007-05-05Javascript獲取與設(shè)置ckeditor數(shù)據(jù)的實(shí)現(xiàn)方法
最近編輯器后臺升級成了ckeditor,但原來后臺有很多對應(yīng)編輯器內(nèi)容的替換功能,那么就需要用js獲取ckeditor編輯器里面的內(nèi)容,這里就為大家介紹一下具體的實(shí)現(xiàn)方法2023-08-08javascript document.referrer 用法
document對象的referrer屬性,返回導(dǎo)航到當(dāng)前網(wǎng)頁的超鏈接所在網(wǎng)頁的URL。2009-04-04JavaScript性能優(yōu)化之小知識總結(jié)
JavaScript的性能問題不容小覷,這就需要我們開發(fā)人員在編寫JavaScript程序時(shí)多注意一些細(xì)節(jié),本文給大家介紹javascript性能優(yōu)化之小知識總結(jié),需要的朋友可以參考下2015-11-11JavaScript打開word文檔的實(shí)現(xiàn)代碼(c#)
在C#中打開word文檔其實(shí)不算太難,方法也比較多,用javascript怎么打開呢?其實(shí),也不難2012-04-04使用javascript函數(shù)編寫簡單銀行取錢存錢流程
本文通過實(shí)例代碼給大家講解了使用javascript函數(shù)編寫簡單銀行取錢存錢流程,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05JS實(shí)現(xiàn)求數(shù)組起始項(xiàng)到終止項(xiàng)之和的方法【基于數(shù)組擴(kuò)展函數(shù)】
這篇文章主要介紹了JS實(shí)現(xiàn)求數(shù)組起始項(xiàng)到終止項(xiàng)之和的方法,基于數(shù)組擴(kuò)展函數(shù)實(shí)現(xiàn)該功能,涉及javascript針對數(shù)組的簡單判斷、遍歷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06