您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

js:无缝轮播实现原理

彭世瑜 发布时间:2021-11-19 10:17:41 ,浏览量:1

实现滚动效果 在这里插入图片描述

demo地址:https://mouday.github.io/front-end-demo/swiper.html

代码:


    body {
      display: flex;
      justify-content: center;
    }

    .box-wrap {
      width: 440px;
      height: 80px;
      border: 1px solid #333;
      position: relative;
      overflow: hidden;
    }

    .box-list {
      position: absolute;
      display: flex;
      align-items: center;
      left: 0;
    }

    .box {
      width: 80px;
      height: 80px;
      margin-right: 10px;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #eeeeee;
      color: #000000;
      font-weight: 500;

    }
  

  
    
      1
      2
      3
      4
      5
    
  

  
    let box_list = document.querySelector('.box-list');

    // 复制一份list
    box_list.innerHTML += box_list.innerHTML;

    let left = 0;
    let timer = null;

    // 启动定时器,动态移动list
    function startTimer() {
      timer = setInterval(() => {
        left -= 2;
        // 每个元素的宽度和margin距离
        if (left == -(5 * (80 + 10))) {
          left = 0;
        }
        box_list.style.left = left;
      }, 20)
    }

    // 鼠标移入暂停
    box_list.onmouseenter = function () {
      clearInterval(timer);
    }

    // 鼠标离开继续
    box_list.onmouseleave = function () {
      startTimer()
    }
    // 页面加载的时候启动定时器
    startTimer();
  
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0591s