JavaScript无缝滚动

1.HTML结构:

1 <div id="div1">
2             <ul>
3                 <li><img src="img2/1.jpg" width="400px" height="400" ></li>
4                 <li><img src="img2/2.jpg" width="400px" height="400"></li>
5                 <li><img src="img2/3.jpg" width="400px" height="400" ></li>
6                 <li><img src="img2/4.jpg" width="400px" height="400"></li>
7             </ul>
8         </div>

2.css样式:

*{margin:0;padding:0;}
            #div1{width:400px;height:400px;border:1px solid #ccc;position:absolute;top:50px;left:50px;overflow: hidden;}
            #div1 ul{position:relative;left:0;top:0;}
            #div1 ul li{list-style: none;width:400px;height:400px;float:left;}

3.JavaScript代码:

 1 window.onload=function (){
 2                 var odiv=document.getElementById('div1');
 3                 var oul=odiv.getElementsByTagName('ul')[0];
 4                 var oli=oul.getElementsByTagName('li');
 5                 
 6                 oul.innerHTML=oul.innerHTML+oul.innerHTML;
 7                 oul.style.width=oli[0].offsetWidth*oli.length+'px';
 8                 //设置定时器
 9                 function mover (){
10                     if(oul.offsetLeft<-oul.offsetWidth/2){
11                         oul.style.left='0';
12                     }
13                     if(oul.offsetLeft>0){
14                         oul.style.left=-oul.offsetWidth/2+'px';
15                     }
16                     oul.style.left=oul.offsetLeft-400+'px';
17                 }
18                 var timer=setInterval(mover,300);
19                 odiv.onmousemove=function(){
20                     clearInterval(timer);
21                 }
22                 odiv.onmouseout=function (){
23                     timer=setInterval(mover,300);
24                 }
25             }

可以直接改下width值

posted on 2017-08-02 15:53  封寻丸子  阅读(126)  评论(0编辑  收藏  举报

导航