原生js輪播特效
作為一名前端工程師,手寫輪播圖應(yīng)該是最基本掌握的技能,以下是我自己原生js寫的輪播,歡迎指點(diǎn)批評:
首先css代碼
a{text-decoration:none;color:#3DBBF5;} *{ margin: 0; padding: 0; } .wrapper{ width: 400px; height: 300px; margin: 100px auto; } #lunbo{ position: relative; overflow: hidden; } #list{ position: relative; white-space: nowrap; // 這塊用行元素模擬,所以才用該屬性,塊元素可修改這塊 } #list span{ display: inline-block; width: 400px; height: 300px; text-align: center; line-height: 300px; font-weight: bold; font-size: 100px; color: #fff; } #buttons{ position: absolute; bottom: 0; text-align: center; width: 100%; height: 40px; line-height: 40px; } #buttons span{ display: inline-block; width: 15px; height: 5px; background: #fff; margin: 0 10px; cursor: pointer; transition: all .5s; } #buttons span.on{ height: 20px; } .arrow{ position: absolute; top: 50%; transform: translateY(-50%); font-size: 80px; font-weight: bold; color: #fff; opacity: .3; transition: all .5s; } .wrapper:hover .arrow{ opacity: 1; } #prev{ left: 10px; } #next{ right: 10px; }
然后HTML代碼
<div class="wrapper"> <div id="lunbo"> <div id="list" style="left: -400px;"> <span style="background:yellow;">5</span><span style="background: red;">1</span><span style="background:black;">2</span><span style="background:green;">3</span><span style="background:blue;">4</span><span style="background:yellow;">5</span><span style="background: red;">1</span> </div> <div id="buttons"> <span index="1" class="on"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a href="javascript:;" id="prev" class="arrow"><</a> <a href="javascript:;" id="next" class="arrow">></a> </div> </div>
最后js代碼
window.onload=function () { var lunBo = document.getElementById("lunbo"); var list = document.getElementById("list"); var btn = document.getElementById("buttons").getElementsByTagName('span'); var prev = document.getElementById("prev"); var next = document.getElementById('next'); var interval = 3000; var timer; var index = 1; var animated = false; for (var i=0;i<btn.length;i++) { //按鈕加點(diǎn)擊事件 btn[i].onclick=function () { if(this.className=='on') //如果是狀態(tài)按鈕直接返回節(jié)約資源 { return }; var myIndex =parseInt(this.getAttribute('index'));//獲取按鈕的index屬性值 var offset = -400*(myIndex-index); //根據(jù)屬性值 計(jì)算偏移量 animate(offset) //輪播動畫 index = myIndex; // 改變索引值 showBtn(); //顯示狀態(tài)按鈕 } } function showBtn () { for (var i=0;i<btn.length;i++) { btn[i].className=''; } btn[index-1].className='on'; } prev.onclick=function () { //上一頁事件 if (animated) { //如果是動畫狀態(tài) 直接返回解決bug return; } if (index==1) { index =btn.length; } else{ index-=1; } animate(400); showBtn(); } next.onclick=function () { if (animated) { return; } if (index==btn.length) { index =1; } else{ index+=1; } animate(-400); showBtn(); } function animate(offset) { animated = true; //表示在動畫狀態(tài) var newLeft = parseInt(list.style.left) + offset; //計(jì)算新的left值 var time = 400; //設(shè)置動畫總時(shí)間 var interval = 10; //動畫幀時(shí)間 var speed = offset/(time/interval); //每幀運(yùn)動距離 function go () { if ((speed>0 && parseInt(list.style.left)<newLeft) || (speed<0 && parseInt(list.style.left)>newLeft)) { //通過條件判斷到它是否還要繼續(xù)進(jìn)行動畫 list.style.left = parseInt(list.style.left) + speed +'px'; setTimeout(go,interval) } else{ animated = false; //動畫狀態(tài)結(jié)束 list.style.left = newLeft + 'px'; //現(xiàn)在的位移 if (parseInt(list.style.left)<-2000) { // 輔助假圖 list.style.left = -400 + 'px'; } else if( parseInt(list.style.left)>-400){ list.style.left = -2000 + 'px'; } } } go(); } function play () { timer = setTimeout(function () { next.onclick(); play(); },interval) } play(); function stop () { clearTimeout(timer); } lunBo.onmouseover=stop; lunBo.onmouseout=play; }
以上是所有代碼,歡迎指點(diǎn)交流!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例學(xué)習(xí)Javascript之構(gòu)建方法、屬性
實(shí)例學(xué)習(xí)Javascript之構(gòu)建方法、屬性...2007-03-03bootstrap datetimepicker日期插件使用方法
這篇文章主要為大家詳細(xì)介紹了bootstrap datetimepicker的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01JS中使用textPath實(shí)現(xiàn)線條上的文字
最近項(xiàng)目經(jīng)理交給我一下新項(xiàng)目,要實(shí)現(xiàn)關(guān)系圖,需要在線條上繪制文字。下面小編把使用textPath實(shí)現(xiàn)線條上的文字功能分享到腳本之家平臺供大家參考2017-12-12JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行給出顏色標(biāo)識
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行給出顏色標(biāo)識,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04JavaScript實(shí)現(xiàn)異步圖像上傳功能
這篇文章主要介紹了JavaScript實(shí)現(xiàn)異步圖像上傳功能,本文展示了一種使用代碼示例立即顯示圖像的方法(使用圖像的Base64編碼版本),同時(shí)將其上載到服務(wù)器,而無需等待操作完成。需要的朋友可以參考下2018-07-07如何實(shí)現(xiàn)動態(tài)刪除javascript函數(shù)
如何實(shí)現(xiàn)動態(tài)刪除javascript函數(shù)...2007-05-05基于javascript實(shí)現(xiàn)圖片懶加載
這篇文章主要介紹了javascript實(shí)現(xiàn)圖片懶加載的方法及思路,有時(shí)我們需要用懶加載,也就是延遲加載圖片的方式,來提高網(wǎng)站的親和力,需要的朋友可以參考下2016-01-01