欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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截取字符串的Slice、Substring、Substr函數(shù)詳解和比較

    JavaScript截取字符串的Slice、Substring、Substr函數(shù)詳解和比較

    這篇文章主要介紹了JavaScript截取字符串的Slice、Substring、Substr函數(shù)詳解和比較,需要的朋友可以參考下
    2014-03-03
  • 一個php+js實時顯示時間問題

    一個php+js實時顯示時間問題

    本文給大家分享的是解決的php+js實時顯示時間問題,主要是自己當時的理解有問題,也許大家有和我一樣的情況,這里分享給大家
    2015-10-10
  • JavaScript 動態(tài)三角函數(shù)實例詳解

    JavaScript 動態(tài)三角函數(shù)實例詳解

    本文通過實例代碼給大家實例講解了javascript動態(tài)三角函數(shù)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-01-01
  • JS中改變this指向的方法(call和apply、bind)

    JS中改變this指向的方法(call和apply、bind)

    this是javascript的一個關鍵字,隨著函數(shù)使用場合不同,this的值會發(fā)生變化。但是總有一個原則,那就是this指的是調(diào)用函數(shù)的那個對象,通過本文給大家介紹JS中改變this指向的方法(call和apply、bind),需要的朋友參考下
    2016-03-03
  • 微信小程序使用npm包的方法步驟

    微信小程序使用npm包的方法步驟

    這篇文章主要介紹了微信小程序使用npm包的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • 微信小程序(訂閱消息)功能

    微信小程序(訂閱消息)功能

    這篇文章主要介紹了微信小程序訂閱消息功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • JavaScript編制留言簿程序代碼

    JavaScript編制留言簿程序代碼

    也許你有一個很酷的主頁,很希望與人分享。同時,你希望訪問你主頁的人能對你的主頁提供一些意見和建議,或者你對某個主題感興趣,而希望客人也許能給你一幫助,這就要用到留言簿。留言簿使得你能與每一個訪問你主頁的人交換信息。
    2008-09-09
  • JS鼠標3次點擊事件實現(xiàn)代碼及擴展思路

    JS鼠標3次點擊事件實現(xiàn)代碼及擴展思路

    這篇文章主要介紹了JS鼠標3次點擊事件實現(xiàn)及擴展思路,需要的朋友可以參考下
    2017-09-09
  • js監(jiān)聽input輸入框值的實時變化實例

    js監(jiān)聽input輸入框值的實時變化實例

    下面小編就為大家?guī)硪黄猨s監(jiān)聽input輸入框值的實時變化實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • JavaScript中的FileReader圖片預覽上傳功能實現(xiàn)代碼

    JavaScript中的FileReader圖片預覽上傳功能實現(xiàn)代碼

    本文通過實例代碼給大家介紹了js中的FileReader圖片預覽上傳功能,代碼分為html和js代碼兩部分,具體實現(xiàn)代碼大家參考下本文
    2017-07-07

最新評論