css+js实现轮播banner

基本功能

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

完整代码


<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">
        
        <img class="img" src="images/1.jpg" alt="" />
      li>
      <li class="item">
        
        <img class="img" src="images/2.jpg" alt="" />
      li>
      <li class="item">
        
        <img class="img" src="images/3.jpg" alt="" />
      li>
      <li class="item">
        
        <img class="img" src="images/4.jpg" alt="" />
      li>
      <li class="item">
        
        <img class="img" src="images/5.jpg" alt="" />
      li>
      <li class="item">
        
        <img class="img" src="images/6.jpg" alt="" />
      li>
      <li class="item">
        
        <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>

样图

css+js实现轮播banner_第1张图片

依赖

优化

上一张、下一张有问题

完整代码

https://github.com/withwz/swiper

你可能感兴趣的:(前端)