说明:代码来源网络,此处只做记录,方便日后使用
<html> <style> #demo{300px;margin:0 auto; overflow:hidden} #indemo{300%} #demo1{300px; float:left} #demo2{300px; float:left} </style> <div id="demo"> <div id="indemo"> <div id="demo1">如何用js实现一个横向滚动新闻</div> <div id="demo2"></div> </div> </div> <script> var speed=10;//数字越大越慢 var demo=document.getElementById("demo") var demo1=document.getElementById("demo1") var demo2=document.getElementById("demo2") demo2.innerHTML=demo1.innerHTML; function marquee(){ if(demo.scrollLeft>=demo1.offsetWidth){ demo.scrollLeft=0; }else{ demo.scrollLeft++; } } setInterval(marquee,speed) </script> </html>