js實現圖片無縫滾動,
js無縫滾動效果幾乎在任何網頁上都能看到它的身影,有的可能是使用外掛程式,其實使用原始的javascript比較簡單。
主要的是使用js位置知識。
- 1.innerHTML:設定或擷取元素的html標籤
- 2.scrollLeft:設定或擷取位於對象左邊界和視窗中目前可見內容的最左端之間的距
- 3.offsetWidth:設定或擷取指定標籤的寬度
- 4.setInterval():設定方法定時啟動
- 5.clearInterval();清除定時器
:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>javascript scroll製作</title></head><body><style> /*conment*/ *{ margin: 0; padding: 0; } img{max-width: 100%;} .container{ max-width: 620px; margin: 0 auto; padding-top: 50px; } .text-center{text-align: center;} .list-inline li{ display: inline-block; } .hide{display: none;} hr{ margin:20px 0; } .tag{ background-color: #ccc; padding: 5px 0; } .tag li{ padding: 0 10px; border-left: 1px solid #fff; cursor:pointer; } .tag li:first-child{ border-left: transparent; } .tag li.active{ background-color: #ddd; } .scroll{ position: relative; padding: 10px; margin-bottom: 20px; background-color: #ddd; } .wrap{ overflow: hidden; } .content{ min-width: 3000px; height: 200px; } .content ul{ float: left; } .content ul li{ display: inline-block; max-width: 200px; } #prev,#next{ width: 50px; height: 50px; margin-top: -25px; background-color: #ccc; line-height: 50px; text-align: center; cursor: pointer; } #prev{ position: absolute; left: 0; top:50%; border-radius: 0 25px 25px 0; } #next{ position: absolute; right: 0; top:50%; border-radius: 25px 0 0 25px; }</style> <div class="container"> <h1 class="text-center">圖片滾動製作</h1> <hr> <div class="scroll"> <div class="wrap" id="wrap"> <div id="content" class="content" > <ul id="list1"> <li> <img src="freelance.gif" alt=""> </li> <li> <img src="button.gif" alt=""></li> <li> <img src="load.gif" alt=""></li> <li> <img src="straw.gif" alt=""></li> </ul> <ul id="list2"> </ul> </div> </div> <div id="prev"> prev </div> <div id="next"> next </div> </div> </div><script> var wrap=document.getElementById('wrap'); var list1=document.getElementById('list1'); var list2=document.getElementById('list2'); var prev=document.getElementById('prev'); var next=document.getElementById('next'); //建立複製一份內容列表 list2.innerHTML=list1.innerHTML; //向左迴圈滾動 function scroll(){ if(wrap.scrollLeft>=list2.offsetWidth){ wrap.scrollLeft=0; } else{ wrap.scrollLeft++; } } timer = setInterval(scroll,1); //滑鼠停留使用clearInterval() wrap.onmouseover=function(){ clearInterval(timer); } wrap.onmouseout=function(){ timer = setInterval(scroll,1); } //向左加速 function scroll_l(){ if(wrap.scrollLeft>=list2.offsetWidth){ wrap.scrollLeft=0; } else{ wrap.scrollLeft++; } } //向右滾動 function scroll_r(){ if(wrap.scrollLeft<=0){ wrap.scrollLeft+=list2.offsetWidth; } else{ wrap.scrollLeft--; } } prev.onclick=function(){ clearInterval(timer); change(0) } next.onclick=function(){ clearInterval(timer); change(1) } function change(r){ if(r==0){ timer = setInterval(scroll_l,60); wrap.onmouseout = function(){ timer = setInterval(scroll_l,60); } } if(r==1){ timer = setInterval(scroll_r,60); wrap.onmouseout = function(){ timer = setInterval(scroll_r,60); } } } </script> </body></html>
以上就是為大家分享的js實現圖片無縫滾動代碼,希望對大家的學習javascript程式設計有所協助。
您可能感興趣的文章:
- 徹底搞懂JS無縫滾動代碼
- js 實現無縫滾動 相容IE和FF
- 連結圖片無縫(無間斷)向左平滑滾動Js版代碼
- jcarousellite.js 基於Jquery的圖片無縫滾動外掛程式
- JS圖片無縫滾動(簡單利於使用)
- div+css+js實現無縫滾動類似marquee無縫滾動相容firefox
- js向上無縫滾動,網站公告效果 具體代碼
- 可自訂速度的js圖片無縫滾動樣本分享
- JS圖片無縫、平滑滾動代碼