首页 > 技术文章 > 无缝滚动+定时器

impossible1994727 2017-05-03 10:55 原文

<html>
    <head>
        <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <title></title>
        <style>
            #demo {
 /*background: #FFF;*/
 overflow:hidden;
 border: 1px dashed #CCC;
 width: 830px;//width=100%;
 height:150px;
 }
 #demo img {
 border: 3px solid #F2F2F2;
 }
 #indemo {
 float: left;
 width: 400%;//越大越好
 }
 #demo1 {
 float: left;
 }
 #demo2 {
 float: left;
 }
 img{
     float: left;
     width: 320px;
     height: 150px;
 }
</style>
    </head>
    <body>
        
<div id="demo">
<div id="indemo">
<div id="demo1">
<a href="#"><img src="img/1106628.jpg" border="0" /></a>
<a href="#"><img src="img/1106914.jpg" border="0" /></a>
<a href=""><img src="img/1111588.jpg"/>
</a>
<a href=""><img src="img/1113230.jpg"/></a>
</div>
<div id="demo2"></div>
</div>
</div>
    </body>
    <script>    
var speed=10;
var tab=document.getElementById("demo");
var tab1=document.getElementById("demo1");
var tab2=document.getElementById("demo2");
tab2.innerHTML=tab1.innerHTML;

function Marquee(){
if(tab2.offsetWidth-tab.scrollLeft==0){
tab.scrollLeft=0;
}
else{
// alert(tab2.offsetWidth);alert(tab.scrollLeft)
 tab.scrollLeft++;
 
 }
}

var MyMar=setInterval(Marquee,speed);


//鼠标事件

tab.onmouseover=function() {clearInterval(MyMar)};
tab.onmouseout=function() {MyMar=setInterval
(Marquee,speed)};
</script>

</html>

注释:

tab2.offsetWidth:是指整个demo2的宽度;

tab.scrollLeft:指对象(#demo)的左边界和窗口中目前可见内容(#demo和#demo2)的最左端的距离;

推荐阅读