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

首頁 > 編程 > JavaScript > 正文

原生js實現輪播圖

2019-11-19 17:24:36
字體:
來源:轉載
供稿:網友

本文實例為大家分享了js輪播圖的具體實現代碼,供大家參考,具體內容如下

CSS:

<style>   * {     margin: 0;     padding: 0;     list-style: none;     text-decoration: none;     font-family: "Microsoft YaHei", Arial, Helvetica, sans-serifsans-serif;   }      body {     background: #eee;   }      #Bigbox {     width: 720px;     height: 420px;     border: 1px solid #333;     margin: 60px auto;   }      #Box {     width: 700px;     height: 400px;     position: relative;     overflow: hidden;     top: 10px;     left: 10px;   }      #Ul {     height: 400px;     position: absolute;     top: 0;     left: 0;   }      #Ul li {     width: 700px;     height: 400px;     float: left;   }      #Left {     width: 60px;     height: 50px;     border-radius: 30%;     background: rgba(96, 96, 96, .5);     position: absolute;     top: 50%;     left: 0;     margin-top: -25px;     color: #fff;     line-height: 50px;     text-align: center;     cursor: pointer;     font-size: 20px;     display: none;   }      #Right {     width: 60px;     height: 50px;     border-radius: 30%;     background: rgba(96, 96, 96, .5);     position: absolute;     top: 50%;     right: 0;     margin-top: -25px;     color: #fff;     line-height: 50px;     text-align: center;     cursor: pointer;     font-size: 20px;     display: none;   } </style> 

html:

<div id="Bigbox">     <div id="Box">       <ul id="Ul">         <li>           1<img src="img/1.jpg" width="100%" height="100%">         </li>         <li>           2<img src="img/2.jpg" width="100%" height="100%">         </li>         <li>           3<img src="img/3.jpg" width="100%" height="100%">         </li>         <li>           4<img src="img/4.jpg" width="100%" height="100%">         </li>         <li>           5<img src="img/5.jpg" width="100%" height="100%">         </li>         <li>           6<img src="img/6.jpg" width="100%" height="100%">         </li>         <li>           7<img src="img/7.jpg" width="100%" height="100%">         </li>         <li>           8<img src="img/8.jpg" width="100%" height="100%">         </li>         <li>           9<img src="img/9.jpg" width="100%" height="100%">         </li>         <li>           10<img src="img/10.jpg" width="100%" height="100%">         </li>       </ul>       <div id="Left" onselectstart="return false">左</div>       <div id="Right" onselectstart="return false">右</div>     </div>  </div> 

js:

<script>  window.onload = function() {    var n = 0;    var timer = null;    var timer1 = null;    var timer2 = null;    var timer3 = null;    var oDiv = document.getElementById('Box')    var oUl = document.getElementById('Ul')    var oLi = oUl.getElementsByTagName('li')     //獲取div寬度    var oDivWidth = getStyle(oDiv, 'width').split('px')[0] //復制oUl的innerHTML    oUl.innerHTML += oUl.innerHTML     //設置ul寬度    oUl.style.width = oLi.length * oDivWidth + 'px'     //獲取ul寬度    var oUlWidth = getStyle(oUl, 'width').split('px')[0] //封裝獲取非行間樣式函數    function getStyle(obj, sName) {     if (obj.currentStyle) {      return obj.currentStyle[sName];     } else {      return getComputedStyle(obj, false)[sName];     }    }    //執行函數    clearInterval(timer3)    timer3 = setInterval(function() {      Run()     }, 2000)     //封裝運動函數    function Run() {     clearInterval(timer)     timer = setInterval(function() {      n -= 20;      oUl.style.left = n + 'px'      if (n % oDivWidth == 0) {       clearInterval(timer3)       clearInterval(timer)       clearInterval(timer1)       timer1 = setTimeout(function() {        Run()       }, 2000)      }      if (n <= -oUlWidth / 2) {       oUl.style.left = 0;       n = 0;       clearInterval(timer3)       clearInterval(timer)       clearInterval(timer1)       timer1 = setTimeout(function() {        Run()       }, 2000)      }     }, 30)    }     //鼠標移入停止滾動    oDiv.onmouseover = function() {     Left.style.display = 'block'     Right.style.display = 'block'     clearInterval(timer3)     clearInterval(timer2)     timer2 = setInterval(function() {      if (n % oDivWidth == 0) {       clearInterval(timer)       clearInterval(timer1)      }     }, 30)     }     //鼠標移出繼續執行    oDiv.onmouseout = function() {     Left.style.display = 'none'     Right.style.display = 'none'     clearInterval(timer3)     clearInterval(timer2)     clearInterval(timer1)     timer1 = setTimeout(function() {      Run()     }, 2000)    }     //向左    Left.onclick = function() {     //清除所有定時器     clearInterval(timer)     clearInterval(timer1)     clearInterval(timer2)     clearInterval(timer3)     timer = setInterval(function() {      n -= 50;      oUl.style.left = n + 'px'      if (n % oDivWidth == 0) {       clearInterval(timer)      }      if (n <= -oUlWidth / 2) {       oUl.style.left = 0;       n = 0;      }     }, 30)    }     //向右    Right.onclick = function() {     clearInterval(timer)     clearInterval(timer1)     clearInterval(timer2)     clearInterval(timer3)     if (n == 0) {      n = -oUlWidth / 2     }     clearInterval(timer)     timer = setInterval(function() {      n += 50;      oUl.style.left = n + 'px'      if (n % oDivWidth == 0) {       clearInterval(timer)      }      }, 30)    }   } </script> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 积石山| 绥芬河市| 天长市| 江津市| 石嘴山市| 洞口县| 兰溪市| 和龙市| 丽江市| 游戏| 蒙城县| 蓝田县| 蒲城县| 金平| 富平县| 梅州市| 崇仁县| 开鲁县| 昂仁县| 滨海县| 思南县| 闻喜县| 金川县| 阿拉善左旗| 赤峰市| 南江县| 马边| 塔河县| 西乌珠穆沁旗| 宜章县| 四会市| 江城| 山东| 比如县| 惠州市| 时尚| 阿尔山市| 札达县| 黑龙江省| 西充县| 余干县|