純js實現,完整代碼如下:
<!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="keywords" content="關鍵字1,關鍵字2" /> <meta name="Description" content="描述信息" /> <title>循環滾動</title> <!--CSS/JS--> <style type="text/css"> *{margin:0;padding:0;} ul,li{list-style:none;display:block;} #scrollBox{height:150px;width:300px;margin:100px auto;background:#f09;overflow:hidden;} #scrollBox #con1,#con2{width:280px;float:left;} #scrollBox li{height:15px;line-height:15px;text-align:center;} </style> </head> <body> <!--div--> <div id="scrollBox"> <ul id="con1"> <li>我是測試內容1!!<li> <li>我是測試內容2!!<li> <li>我是測試內容3!!<li> <li>我是測試內容4!!<li> <li>我是測試內容5!!<li> <li>我是測試內容6!!<li> <li>我是測試內容7!!<li> <li>我是測試內容8!!<li> <li>我是測試內容9!!<li> </ul> <ul id="con2"></ul> </div> <script type="text/javascript"> var area =document.getElementById('scrollBox'); var con1 = document.getElementById('con1'); var con2 = document.getElementById('con2'); con2.innerHTML=con1.innerHTML; function scrollUp(){ if(area.scrollTop>=con1.offsetHeight){ area.scrollTop=0; }else{ area.scrollTop++ } } var time = 50; var mytimer=setInterval(scrollUp,time); area.onmouseover=function(){ clearInterval(mytimer); } area.onmouseout=function(){ mytimer=setInterval(scrollUp,time); } </script> </body> </html>
預覽效果請點擊:我的github