js原生之焦點圖轉換加定時器執行個體,js定時器執行個體

來源:互聯網
上載者:User

js原生之焦點圖轉換加定時器執行個體,js定時器執行個體

在jQuery之焦點圖轉換-左右的基礎上,將jQuery代碼改成js原生,並添加定時器(setInterval()和clearInterval())

<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>焦點圖轉換--原生和定時器</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;//建立索引值   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;   }  }    //函數封裝  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;  }    //通過定時器調用封裝好的函數  var stop=setInterval(lunbo,1500);    //滑鼠放上和離開時定時器的狀態  pic.onmouseenter=function(){   clearInterval(stop)  }  pic.onmouseleave=function(){   stop=setInterval(lunbo,1500)  } </script></body></html>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援幫客之家!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.