js原生之焦點圖轉(zhuǎn)換加定時器實例
更新時間:2016年12月12日 08:28:52 作者:fighting_liu
本文主要分享了在jQuery之焦點圖轉(zhuǎn)換-左右的基礎上,將jQuery代碼改成js原生,并添加定時器(setInterval()和clearInterval())的實例代碼。需要的朋友可以參考借鑒
在jQuery之焦點圖轉(zhuǎn)換-左右的基礎上,將jQuery代碼改成js原生,并添加定時器(setInterval()和clearInterval())
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>焦點圖轉(zhuǎn)換--原生和定時器</title> <link rel="stylesheet" href="css/reset.css"> <style type="text/css"> .pic-show{width: 480px;overflow: hidden;} #pic{width: 1920px;height: 320px;position: relative;} #pic img{display: block;float: left;} .pic-show>img{display: block;cursor: pointer;opacity: 0.8; position: absolute;top: 142px;left: 30px;} .pic-show>img:last-child{display: block; position: absolute;left: 414px; } ul{width: 120px;height: 18px;position: absolute;top: 280px;left: 185px;} li{float: left;width: 20px;height: 18px;margin-left: 5px;} a{display: block;width: 20px;height: 18px;text-decoration: none;border: 1px solid #ccc;border-radius: 50%;background-color: #ccc;opacity: 0.6;} a:hover{background-color: #094a99;} .aCss{background-color: #094a99;} p{width: 480px;text-align: center;} </style> </head> <body> <div class="pic-show"> <div id="pic"> <img src="images/1.jpg" alt=""> <img src="images/2.jpg" alt=""> <img src="images/3.jpg" alt=""> <img src="images/4.jpg" alt=""> </div> <img id="prev" src="images/slider-prev.png" alt=""> <img id="next" src="images/slider-next.png" alt=""> </div> <ul id="list"> <li><a href="#" title="日落"></a></li> <li><a href="#" title="鋼琴"></a></li> <li><a href="#" title="大海"></a></li> <li><a href="#" title="秋色"></a></li> </ul> <p id="p">這是一段測試文字</p> <script src="js/jquery-3.0.0.js"></script> <script type="text/javascript"> var num=0; function G(id){ return document.getElementById(id) } var pic = G('pic') var next = G('next') var prev = G('prev') var p = G('p') var list = G('list') var arr = G('list').getElementsByTagName('a') //點擊next next.onclick=function(){ if(num<3){ num=num+1; } else{ num=0; } console.log(num); var mlNum=num * -480+'px'; pic.style.marginLeft=mlNum; for(var j=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); var txt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } //點擊prev prev.onclick=function(){ if(num>0){ num=num-1; } else{ num=3; } console.log(num); var mlNum2=num * -480+'px'; pic.style.marginLeft=mlNum2; for(var j=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); var txt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } for(var i=0;i<arr.length;i++){ arr[i].index=i;//創(chuàng)建索引值 arr[i].onclick=function(event){ num=this.index; var mlNum3=num * -480+'px'; pic.style.marginLeft=mlNum3; for(var j=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } this.style.backgroundColor='#094a99'; event.preventDefault(); var txt=this.getAttribute("title"); p.textContent=txt; } } //函數(shù)封裝 function lunbo(){ if(num<3){ num=num+1; } else{ num=0; } console.log(num); var mlNum=num * -480+'px'; pic.style.marginLeft=mlNum; for(var j=0;j<arr.length;j++){ arr[j].style.backgroundColor='#ccc'; } arr[num].style.backgroundColor="#094a99"; event.preventDefault(); var txt=arr[num].getAttribute("title"); console.log(txt); p.textContent=txt; } //通過定時器調(diào)用封裝好的函數(shù) var stop=setInterval(lunbo,1500); //鼠標放上和離開時定時器的狀態(tài) pic.onmouseenter=function(){ clearInterval(stop) } pic.onmouseleave=function(){ stop=setInterval(lunbo,1500) } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
您可能感興趣的文章:
- JavaScript定時器實現(xiàn)的原理分析
- javascript 定時器工作原理分析
- JavaScript實現(xiàn)頁面定時刷新(定時器,meta)
- JS Canvas定時器模擬動態(tài)加載動畫
- JavaScript暫停和繼續(xù)定時器的實現(xiàn)方法
- JS定時器使用,定時定點,固定時刻,循環(huán)執(zhí)行詳解
- JavaScript 定時器 SetTimeout之定時刷新窗口和關閉窗口(代碼超簡單)
- javascript中SetInterval與setTimeout的定時器用法
- JavaScript定時器和優(yōu)化的取消定時器方法
- Javascript中setTimeOut和setInterval的定時器用法
- JavaScript學習筆記之定時器
- JS定時器用法分析【時鐘與菜單中的應用】
相關文章
JavaScript截取字符串的Slice、Substring、Substr函數(shù)詳解和比較
這篇文章主要介紹了JavaScript截取字符串的Slice、Substring、Substr函數(shù)詳解和比較,需要的朋友可以參考下2014-03-03JavaScript 動態(tài)三角函數(shù)實例詳解
本文通過實例代碼給大家實例講解了javascript動態(tài)三角函數(shù)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01JS中改變this指向的方法(call和apply、bind)
this是javascript的一個關鍵字,隨著函數(shù)使用場合不同,this的值會發(fā)生變化。但是總有一個原則,那就是this指的是調(diào)用函數(shù)的那個對象,通過本文給大家介紹JS中改變this指向的方法(call和apply、bind),需要的朋友參考下2016-03-03JavaScript中的FileReader圖片預覽上傳功能實現(xiàn)代碼
本文通過實例代碼給大家介紹了js中的FileReader圖片預覽上傳功能,代碼分為html和js代碼兩部分,具體實現(xiàn)代碼大家參考下本文2017-07-07