国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js原生之焦點圖轉(zhuǎn)換加定時器實例

2019-11-19 18:34:15
字體:
供稿:網(wǎng)友

在jQuery之焦點圖轉(zhuǎn)換-左右的基礎(chǔ)上,將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);    //鼠標(biāo)放上和離開時定時器的狀態(tài)  pic.onmouseenter=function(){   clearInterval(stop)  }  pic.onmouseleave=function(){   stop=setInterval(lunbo,1500)  } </script></body></html>

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持武林網(wǎng)!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 文山县| 永吉县| 横山县| 昌黎县| 介休市| 津南区| 井研县| 三亚市| 罗江县| 广昌县| 常宁市| 南通市| 兰溪市| 柘荣县| 濉溪县| 莱西市| 吴旗县| 宝应县| 望都县| 庄浪县| 清流县| 麻江县| 黄石市| 迁安市| 嘉定区| 棋牌| 耒阳市| 全州县| 文山县| 澄江县| 屯门区| 玉龙| 贡觉县| 郯城县| 修文县| 内江市| 尼玛县| 措勤县| 隆安县| 曲松县| 丰顺县|