css+js实现轮播banner

基本功能

  • 轮回播放
  • 自定义过渡时间、间隔时间
  • 上一张、下一张

完整代码

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .container {
      width: 126px;
      height: 86px;
      overflow: hidden;
      margin: 100px auto;
      background-color: aquamarine;
    }

    .swiper {
      list-style: none;
      display: flex;
      /* transition: all 400ms ease-in-out; */
    }

    .swiper:hover {
      animation-play-state: paused;
    }

    .item {
      flex-basis: 126px;
      flex-shrink: 0;
      width: 126px;
      height: 86px;
      background: rgba(11, 22, 11, .4);
      box-sizing: border-box;
      border: 2px solid red;
    }

    .img {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }


    .control {
      display: flex;
      justify-content: center;
    }
  </style>
</head>

<body>
  <div class="container">
    <ul class="swiper">
      <li class="item">
        <!-- 1 -->
        <img class="img" src="images/1.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 2 -->
        <img class="img" src="images/2.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 3 -->
        <img class="img" src="images/3.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 4 -->
        <img class="img" src="images/4.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 5 -->
        <img class="img" src="images/5.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 6 -->
        <img class="img" src="images/6.jpg" alt="" />
      </li>
      <li class="item">
        <!-- 7 -->
        <img class="img" src="images/7.jpg" alt="" />
      </li>
    </ul>
  </div>
  <div class="control">
    <div>
      轮播间隔:
      <input id="input1" type="text" value="800">
      <br />
      过渡时间:
      <input id="input2" type="text" value="400">
      <br />
      <button id="btn">重置轮播</button>
      <br />
      <button id="pre">上一张</button>
      <button id="next">下一张</button>
    </div>
  </div>

  <script>
    window.onload = function () {
      const swiper = document.querySelectorAll('.swiper')[0]
      const count = document.querySelectorAll('.item').length
      let timer
      let mark = -1
      let i = 0

      const getI = (i) => {
        if (i < 1 || (i < count - 1 && i > mark)) {
          mark = i
          return ++i
        } else {
          --i
          mark = i
          return i
        }
      }

      const refrence = () => {
        clearInterval(timer)
        const interval = document.querySelector('#input1').value
        const duration = document.querySelector('#input2').value
        console.log(interval, duration)
        swiper.style.transition = `all ${duration}ms ease-in-out`
        timer = setInterval(() => {
          i = getI(i)
          swiper.style.transform = `translate(${-126 * i}px)`
        }, interval)
      }

      refrence()

      document.querySelector('#btn').onclick = refrence

      document.querySelector('#pre').onclick = () => {
        i = getI(i)
        swiper.style.transform = `translate(${-126 * i}px)`
      }

      document.querySelector('#next').onclick = () => {
        i = getI(i)
        swiper.style.transform = `translate(${-126 * i}px)`
      }

    }

  </script>
</body>

</html>

样图

image.png

依赖

优化

上一张、下一张有问题

完整代码

https://github.com/withwz/swiper

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值