這篇文章主要為大家展示了原生js如何實(shí)現(xiàn)滑動(dòng)輪播封裝,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lá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無(wú)縫輪播</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)畫(huà) move = function(){ // 設(shè)置box運(yùn)動(dòng)終點(diǎn)值 var _left = -1 * nextIndex * liWidth; // 開(kāi)始動(dòng)畫(huà) 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)畫(huà) 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),切換至對(duì)應(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)擊翻頁(yè)進(jìn)行切換 on($("#prev"),"click",function(){ nextIndex = currentIndex - 1; if(nextIndex < 0){ nextIndex = length; } move(); }) on($("#next"),"click",function(){ move(); }) </script> </body> </html>
標(biāo)題名稱:原生js如何實(shí)現(xiàn)滑動(dòng)輪播封裝-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://newbst.com/article32/hhpsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、搜索引擎優(yōu)化、網(wǎng)站收錄、微信小程序、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容