javascript無縫滾動
阿新 • • 發佈:2018-12-11
滑鼠滑過左側按鈕向左滑動,滑鼠滑過右側按鈕,向右滑動。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>無縫滾動</title> <style> @font-face { font-family: 'iconfont'; src: url('font/iconfont.eot'); src: url('font/iconfont.eot?#iefix') format('embedded-opentype'), url('font/iconfont.woff') format('woff'), url('font/iconfont.ttf') format('truetype'), url('font/iconfont.svg#iconfont') format('svg'); } .iconfont{ font-family:"iconfont" !important; font-size:80px;font-style:normal; -webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0.2px; -moz-osx-font-smoothing: grayscale; } *{ padding: 0; margin: 0; } .box{ position: relative; width: 900px; height: 200px; overflow: hidden; margin:20px auto 0; } ul{ list-style: none; height: 200px; position: absolute; top: 0; left: 0; } li{display: inline-block; width: 300px; height: 200px; } img{ width: 100%; height: 100%; } a{ display: inline-block; position: fixed; right: 90px; top: 20px; width: 80px; height: 200px; overflow: auto; z-index: 1; background:rgba(0,0,0,0.8); text-decoration: none; color: #fff; line-height: 200px; text-align: center; } .le{ left: 90px; transform: rotate(180deg); } </style> <script> window.onload=function(){ var roll=document.getElementById("roll"); var oUl=document.getElementsByTagName("ul")[0]; var oli=document.getElementsByTagName("li"); var btn=document.getElementsByTagName("a"); var iSpend=-2; var timer=null; oUl.style.width=oli[0].offsetWidth*oli.length+34+"px"; timer=setInterval(function(){ oUl.style.left=oUl.offsetLeft+iSpend+"px"; if (oUl.offsetLeft<-oUl.offsetWidth/2) { oUl.style.left='0px'; } else if (oUl.offsetLeft>0){ oUl.style.left=-oUl.offsetWidth/2+'px'; } },10) btn[0].onmouseover=function(){ iSpend=2; } btn[1].onmouseover=function(){ iSpend=-2; } oUl.onmouseover=function(){ clearInterval(timer); } oUl.onmouseout=function(){ timer=setInterval(function(){ oUl.style.left=oUl.offsetLeft+iSpend+"px"; if (oUl.offsetLeft<-oUl.offsetWidth/2) { oUl.style.left='0px'; } else if (oUl.offsetLeft>0){ oUl.style.left=-oUl.offsetWidth/2+'px'; } },10) } } </script> </head> <body> <div class="box" id="roll"> <a href="#" class="iconfont"></a> <a href="#" class="le iconfont"></a> <ul> <li><img src="images/1.jpg" alt=""></li> <li><img src="images/2.jpg" alt=""></li> <li><img src="images/3.jpg" alt=""></li> <li><img src="images/5.jpg" alt=""></li> <li><img src="images/1.jpg" alt=""></li> <li><img src="images/2.jpg" alt=""></li> <li><img src="images/3.jpg" alt=""></li> <li><img src="images/5.jpg" alt=""></li> </ul> </div> </body> </html>
效果展示: