js原生无缝滚动demo

1.原理:我们的视觉区域是1234四张图,首先复制一份12345追加到5的后面(如图1的样子),等运动到了2的位置的时候,利用视觉差我们使他瞬间跳回1的样子,就这样往复循环就ok了

下面是示例代码:

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>无缝滚动</title>
   <style>
   *{
    margin: 0;
    padding: 0;
   }
   ul,li{
    list-style: none;
   }
   .warp{
      height: 150px;
      width: 616px;
      margin: 50px auto;
      overflow: hidden;
      background-color: red;
      position: relative;
   }
   .imgs{
    position: absolute;
   }
  .imgs li{
    width: 150px;
    height: 146px;
    float: left;
    margin: 2px;
    cursor: pointer;
    background-color: yellow;
  }
   </style>
</head>
<body>
	  <div class="warp">
       <ul id="imgs" class="imgs">
           <li>1</li>
           <li>2</li>
           <li>3</li>
           <li>4</li>
           <li>5</li>
       </ul>  
    </div>
    <script type="text/javascript">
      window.οnlοad=function  () {
           var margin=4;//元素间距
           var speed=-5;//左移速度(右移为正)
           var timer=null;//定时器
           var oUl=document.getElementById("imgs");
           var aLi=oUl.getElementsByTagName("li");
           var liWidth=aLi[0].offsetWidth+margin;//li宽度
           var liCount=aLi.length;//li个数
      
           oUl.innerHTML+=oUl.innerHTML;//复制内容追加

           oUl.style.width=liWidth*aLi.length+'px';
           oUl.style.height=liWidth+'px';

           timer=setInterval(function(){
              //向左移动判断
              if(Math.abs(oUl.offsetLeft)>=liWidth*liCount){
                oUl.style.left="0px";
              }
              //向右的判断
              //if(Math.abs(oUl.offsetLeft)==0){
              //  oUl.style.left=-liCount*liWidth+'px';
              // }
            oUl.style.left=oUl.offsetLeft+speed+'px';
           },100)

           oUl.οnmοuseοver=function(){
              clearInterval(timer);
           }
           oUl.οnmοuseοut=function(){
              
           timer=setInterval(function(){
            if(Math.abs(oUl.offsetLeft)>=liWidth*liCount){
              oUl.style.left="0px";
            }
            oUl.style.left=oUl.offsetLeft+speed+'px';
           },100)
           }
      }
    </script>
</body>
</html>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值