原生js實(shí)現(xiàn)圖片輪播特效
本文特意為原生js實(shí)現(xiàn)圖片輪播特效代碼做了下總結(jié),分享給大家供大家參考,歡迎大家學(xué)習(xí)。
運(yùn)行效果圖:
具體代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>最簡(jiǎn)單的輪播廣告</title> <style> body, div, ul, li { margin: 0; padding: 0; } ul { list-style-type: none; } body { background: #000; text-align: center; font: 12px/20px Arial; } #box { position: relative; width: 492px; height: 172px; background: #fff; border-radius: 5px; border: 8px solid #fff; margin: 10px auto; } #box .list { position: relative; width: 490px; height: 170px; overflow: hidden; border: 1px solid #ccc; } #box .list li { position: absolute; top: 0; left: 0; width: 490px; height: 170px; opacity: 0; transition: opacity 0.5s linear } #box .list li.current { opacity: 1; } #box .count { position: absolute; right: 0; bottom: 5px; } #box .count li { color: #fff; float: left; width: 20px; height: 20px; cursor: pointer; margin-right: 5px; overflow: hidden; background: #F90; opacity: 0.7; border-radius: 20px; } #box .count li.current { color: #fff; opacity: 0.7; font-weight: 700; background: #f60 } </style> </head> <body> <div id="box"> <ul class="list"> <li class="current" style="opacity: 1;"><img src="img/images04/01.jpg" width="490" height="170"></li> <li style="opacity: 0;"><img src="img/images04/02.jpg" width="490" height="170"></li> <li style="opacity: 0;"><img src="img/images04/03.jpg" width="490" height="170"></li> <li style="opacity: 0;"><img src="img/images04/04.jpg" width="490" height="170"></li> <li style="opacity: 0;"><img src="img/images04/05.jpg" width="490" height="170"></li> </ul> <ul class="count"> <li class="current">1</li> <li class="">2</li> <li class="">3</li> <li class="">4</li> <li class="">5</li> </ul> </div> <script> var box=document.getElementById('box'); var uls=document.getElementsByTagName('ul'); var imgs=uls[0].getElementsByTagName('li'); var btn=uls[1].getElementsByTagName('li'); var i=index=0; //中間量,統(tǒng)一聲明; var play=null; console.log(box,uls,imgs,btn);//獲取正確 //圖片切換, 淡入淡出效果我是用(transition: opacity 0.8s linear)做的,不糾結(jié)、簡(jiǎn)單 在css里面 function show(a){ //方法定義的是當(dāng)傳入一個(gè)下標(biāo)時(shí),按鈕和圖片做出對(duì)的反應(yīng) for(i=0;i<btn.length;i++ ){ btn[i].className=''; //很容易看懂吧?每個(gè)按鈕都先設(shè)置成看不見,然后把當(dāng)前按鈕設(shè)置成可見。 btn[a].className='current'; } for(i=0;i<imgs.length;i++){ //把圖片的效果設(shè)置和按鈕相同 imgs[i].style.opacity=0; imgs[a].style.opacity=1; } } //切換按鈕功能,響應(yīng)對(duì)應(yīng)圖片 for(i=0;i<btn.length;i++){ btn[i].index=i; //不知道你有沒有發(fā)現(xiàn),循環(huán)里的方法去調(diào)用循環(huán)里的變量體i,會(huì)出現(xiàn)調(diào)到的不是i的變動(dòng)值的問題。所以我先在循環(huán)外保存住 btn[i].onmouseover=function(){ show(this.index); clearInterval(play); //這就是最后那句話追加的功能 } } //自動(dòng)輪播方法 function autoPlay(){ play=setInterval(function(){ //這個(gè)paly是為了保存定時(shí)器的,變量必須在全局聲明 不然其他方法調(diào)不到 或者你可以調(diào)用auto.play 也許可以但是沒時(shí)間試了 index++; index>=imgs.length&&(index=0);//可能有優(yōu)先級(jí)問題,所以用了括號(hào),沒時(shí)間測(cè)試了。 show(index); },1000) } autoPlay();//馬上調(diào)用,我試過用window.onload調(diào)用這個(gè)方法,但是調(diào)用之后影響到了其他方法,使用autoPlay所以只能這樣調(diào)用了 //div的鼠標(biāo)移入移出事件 box.onmouseover=function(){ clearInterval(play); }; box.onmouseout=function(){ autoPlay(); }; //按鈕下標(biāo)也要加上相同的鼠標(biāo)事件,不然圖片停止了,定時(shí)器沒停,會(huì)突然閃到很大的數(shù)字上。 貌似我可以直接追加到之前定義事件中。 </script> </body> </html>
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
Openlayers+EasyUI Tree動(dòng)態(tài)實(shí)現(xiàn)圖層控制
這篇文章主要為大家詳細(xì)介紹了Openlayers+EasyUI Tree動(dòng)態(tài)實(shí)現(xiàn)圖層控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09JavaScript獲取Excel表格的列序號(hào)和列名
這篇文章主要介紹了JavaScript獲取Excel表格的列序號(hào)和列名,在處理Excel文件時(shí),通常要獲取xx列的數(shù)據(jù),這就要求先找到列序號(hào),下文關(guān)于列名獲取需要的小伙伴可以參考一下2022-05-05JavaScript輪播停留效果的實(shí)現(xiàn)思路
輪播停留與無線滾動(dòng)十分類似,都是利用屬性及變量控制移動(dòng)實(shí)現(xiàn)輪播。下面通過本文給大家分享JavaScript輪播停留效果的實(shí)現(xiàn)思路,感興趣的朋友一起看看吧2018-05-05javascript引擎長(zhǎng)時(shí)間獨(dú)占線程造成卡頓的解決方案
這篇文章主要介紹了javascript引擎長(zhǎng)時(shí)間獨(dú)占線程造成卡頓的解決方案,需要的朋友可以參考下2014-12-12js實(shí)現(xiàn)的常用的左側(cè)導(dǎo)航效果
使用js簡(jiǎn)單實(shí)現(xiàn)下常用的左側(cè)導(dǎo)航效果為提高導(dǎo)航性能而生,各位朋友可以參考應(yīng)用,希望對(duì)大家有所幫助2013-10-10一個(gè)簡(jiǎn)單的全屏圖片上下打開顯示網(wǎng)頁(yè)效果示例
這是一個(gè)簡(jiǎn)單的全屏圖片上下打開顯示網(wǎng)頁(yè)效果,源碼如下,喜歡的朋友可以練練手2014-07-07