DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js原生之焦點圖轉換加定時器實例
js原生之焦點圖轉換加定時器實例
編輯:關於JavaScript     

在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>

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved