无缝滚动js (手写通俗易懂)

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>无标题文档</title>
 6 <style>
 7 *{padding:0; margin:0;}
 8 ul{ list-style:none;}
 9 #div{ width:300px; height:100px; margin:100px auto; border:1px solid #ff0000; position:relative; overflow:hidden;}
10 #div ul{ position:absolute; height:100px; left:0;}
11 #div ul li{ width:300px; height:100px; line-height:100px; text-align:center; float:left}
12 </style>
13 <script>
14 window.onload=function(){
15     var oDiv=document.getElementById("div");
16     var oUl=oDiv.getElementsByTagName("ul")[0];
17     var oLi=oUl.getElementsByTagName("li");
18     var oInput=document.getElementsByTagName('input');
19     oUl.innerHTML +=oUl.innerHTML;
20     oUl.style.width=oLi[0].offsetWidth*oLi.length+"px";
21     var iSeep=-2;
22     var tamer=null;
23     
24     
25     clearInterval(tamer);
26     function starMove(){
27          tamer=setInterval(function(){
28             
29             oUl.style.left=oUl.offsetLeft+iSeep+"px";
30             if(-oUl.offsetLeft >= oUl.offsetWidth/2){
31                 oUl.style.left="0px";
32             }else if(oUl.offsetLeft>0){
33                 oUl.style.left=-oUl.offsetWidth/2+"px";
34             }
35         },30)
36     }
37     starMove();
38     oDiv.onmouseover=function(){
39         clearInterval(tamer);
40     }
41     oDiv.onmouseout=function(){
42         starMove();
43     }
44     oInput[0].onclick=function(){
45         iSeep=-2;
46     }
47     oInput[1].onclick=function(){
48         iSeep=2;
49     }
50 }
51 </script>
52 </head>
53 
54 <body>
55 <input type="button" value="左">
56 <input type="button" value="右">
57 <div id='div'>
58     <ul>
59         <li>1</li>
60         <li>2</li>
61         <li>3</li>
62         <li>4</li>
63     </ul>
64 </div>
65 </body>
66 </html>

 

转载于:https://www.cnblogs.com/bokeyuan-hack/p/javascript.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值