原生js輪播(仿慕課網(wǎng))
更新時間:2017年02月15日 10:33:50 作者:完美續(xù)航
本文主要分享了原生js實現(xiàn)仿慕課網(wǎng)的輪播效果。具有很好的參考價值,下面跟著小編一起來看下吧
效果如下:
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript</title> <style> *{margin:0;padding:0;border:0;} a{text-decoration:none;color:#fff;font-size:40px;line-height:200px;display:none;text-align:center;} #container{width:300px;height:200px;margin:50px auto;position:relative;overflow:hidden;} #list{width:2100px;height:200px;position:absolute;top:0;} #list span{width:300px;height:200px;display:inline-block;text-align:center;font-size:22px;float:left;color:#fff;} .one{background:red;} .two{background:orange;} .three{background:blue;} .four{background:green;} .five{background:black;} #buttons{width:200px;height:30px;position:absolute;bottom:0px;left:100px;z-index:9;} #buttons span{display:inline-block;cursor:pointer;width:12px;height:12px;border-radius:6px;background: #2a2a2a} #prev{width:40px;height:200px;position:absolute;left:0px;} #next{width:40px;height:200px;position:absolute;right:0px;} #container .on{background:#fff;} </style> </head> <body> <div id="container"> <div id="list" style="left:-300px"> <span class="five">我是黑色第五張</span> <span class="one">我是紅色第一張</span> <span class="two">我是黃色第二張</span> <span class="three">我是藍色第三張</span> <span class="four">我是綠色第四張</span> <span class="five">我是黑色第五張</span> <span class="one">我是紅色第一張</span> </div> <div id="buttons"> <span class="on" index="1"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a id="prev" href="javascript:;" rel="external nofollow" rel="external nofollow" ><</a> <a id="next" href="javascript:;" rel="external nofollow" rel="external nofollow" >></a> </div> <script> var container = document.getElementById('container'), list = document.getElementById('list'), buttons = document.getElementById('buttons').getElementsByTagName('span'), prev = document.getElementById('prev'), next = document.getElementById('next'), index = 1, len = 5, interval = 3000, animated = false, timer; function animate(offset){ if(offset == 0) return; animated = true; var time = 150, inter = 5, speed = offset/(time/inter), left = parseInt(list.style.left) + offset; var go = function(){ if((speed>0 && parseInt(list.style.left)<left) || (speed<0 && parseInt(list.style.left)>left)){ list.style.left = parseInt(list.style.left) + speed + 'px'; setTimeout(go,inter); }else{ list.style.left = left + 'px'; if(left > -100){ list.style.left = -300*len + 'px'; } if(left < (-300*len)){ list.style.left = '-300px' } animated = false; } } go(); } function showButton(){ for(var i=0 ; i<buttons.length ; i++){ if(buttons[i].className == 'on'){ buttons[i].className = ''; break; } } buttons[index - 1].className = 'on'; } function play(){ timer = setTimeout(function(){ next.onclick(); play(); },interval); } function stop(){ clearTimeout(timer); } next.onclick = function(){ if(animated) { return; } if(index == 5){ index = 1; }else{ index++; } animate(-300); showButton(); } prev.onclick = function(){ if(animated) { return; } if(index == 1){ index = 5; }else{ index--; } animate(300); showButton(); } for (var i = 0; i < buttons.length; i++) { buttons[i].onclick = function () { if (animated) { return; } if(this.className == 'on') { return; } var myIndex = parseInt(this.getAttribute('index')); var offset = -300 * (myIndex - index); animate(offset); index = myIndex; showButton(); } } container.onmouseover = function(){ prev.style.display = next.style.display = 'block'; stop(); } container.onmouseout = function(){ prev.style.display = next.style.display = 'none'; play(); } play(); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
使用JavaScript實現(xiàn)頁面局部更新的方法總結(jié)
在JavaScript中,Ajax(Asynchronous JavaScript and XML)是一種用于在后臺與服務(wù)器進行異步通信的技術(shù),本文給大家介紹了使用JavaScript實現(xiàn)頁面局部更新的三種方法,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12在ES5與ES6環(huán)境下處理函數(shù)默認參數(shù)的實現(xiàn)方法
本文給大家介紹在ES5與ES6環(huán)境下處理函數(shù)默認參數(shù)的實現(xiàn)方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05ES6 proxy和reflect的使用方法與應(yīng)用實例分析
這篇文章主要介紹了ES6 proxy和reflect的使用方法,結(jié)合具體實例形式分析了ES6 proxy和reflect基本功能、原理、使用方法與操作注意事項,需要的朋友可以參考下2020-02-02