原生js輪播(仿慕課網(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">我是藍(lán)色第三張</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>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
原生JavaScript實(shí)現(xiàn)簡(jiǎn)單五子棋游戲
這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)簡(jiǎn)單五子棋游戲,文中示例代碼注釋的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06JavaScript實(shí)現(xiàn)簡(jiǎn)單的tab選項(xiàng)卡切換
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的tab選項(xiàng)卡切換的相關(guān)資料,需要的朋友可以參考下2016-01-01使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的方法總結(jié)
在JavaScript中,Ajax(Asynchronous JavaScript and XML)是一種用于在后臺(tái)與服務(wù)器進(jìn)行異步通信的技術(shù),本文給大家介紹了使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的三種方法,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12在ES5與ES6環(huán)境下處理函數(shù)默認(rèn)參數(shù)的實(shí)現(xiàn)方法
本文給大家介紹在ES5與ES6環(huán)境下處理函數(shù)默認(rèn)參數(shù)的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05ES6中module模塊化開(kāi)發(fā)實(shí)例淺析
這篇文章主要介紹了ES6中module模塊化開(kāi)發(fā),結(jié)合實(shí)例形式分析了ES6中模塊化開(kāi)發(fā)的相關(guān)功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04ES6 proxy和reflect的使用方法與應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 proxy和reflect的使用方法,結(jié)合具體實(shí)例形式分析了ES6 proxy和reflect基本功能、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02