Swiper实现轮播效果

swiper官网:https://3.swiper.com.cn/

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://unpkg.com/swiper@8/swiper-bundle.min.css"
    />
    <script src="https://unpkg.com/swiper@8/swiper-bundle.min.js"></script>
  </head>
  <style>
    .swiper {
      width: 620px;
      height: 720px;
    }

    /* 左右箭头 */
    .swiper-button-prev:after {
      display: none;
    }
    .swiper-button-prev {
      background: url("./image/left-1.png");
      background-size: contain;
      background-repeat: no-repeat;
      width: 110px;
      height: 120px;
      left: 5px;
    }
    .swiper-button-next:after {
      display: none;
    }
    .swiper-button-next {
      background: url("./image/right-1.png");
      background-size: contain;
      background-repeat: no-repeat;
      width: 110px;
      height: 120px;
      right: 0px;
      z-index: 999;
      position: absolute;
    }

    /* 图片显示 */
    .swiper-slide {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .swiper-slide img {
      width: 300px;
    }
    .swiper-slide-active img {
      position: absolute;
      width: 395px;
      transition: width 0.5s;
    }
    .swiper-slide-next img {
      position: absolute;
      left: -230px;
    }
    .swiper-slide-prev img {
      position: absolute;
      right: -230px;
    }
    .swiper-slide-active {
      z-index: 999;
    }
    .swiper-slide-next {
      z-index: 888;
    }
    .swiper-slide-prev {
      z-index: 888;
    }
    .bullet-style {
      width: 0px;
      height: 0px;
      border: 6px solid black;
      background-color: black;
      background-clip: padding-box;
      transform: rotateZ(45deg);
      display: inline-block;
      margin: 0 8px;
      cursor: pointer;
    }
    .bullet-style:hover {
      border-color: gray;
      background-color: gray;
    }
    .bullet-style-active {
      border-style: double;
      border-width: 8px;
    }
  </style>
  <body>
    <div class="swiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">
          <img src="./image/world-3-3-1.png" />
        </div>
        <div class="swiper-slide">
          <img src="./image/world-4-4-1.png" />
        </div>
        <div class="swiper-slide">
          <img src="./image/world-2-2-1.png" />
        </div>
      </div>
      <!-- 分页器 -->
      <div class="swiper-pagination"></div>

      <!--前进后退按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
    <!-- <div class="swiper-button-next swiper-button-black"></div> -->
    <script type="module">
      import Swiper from "https://unpkg.com/swiper@8/swiper-bundle.esm.browser.min.js";
      let addBullentsEvent = () => {};
      const mySwiper = new Swiper(".swiper", {
        loop: true, // 循环模式选项
        // 分页器
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
          // 自定义分页其样式
          type: "custom",
          renderCustom: function (swiper, current, total) {
            let renderHTML = "";
            for (let i = 0; i < total; i++) {
              if (i + 1 == current) {
                renderHTML += `<div class="bullet-style bullet-style-active"></div>`;
              } else {
                renderHTML += `<div class="bullet-style"></div>`;
              }
            }
            return renderHTML;
          },
        },
        // 前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },

        slidesPerView: 2, //设置slider容器能够同时显示的slides数量
        spaceBetween: 80, //slide之间的距离(单位px)
        centeredSlides: true, //设定为true时,active slide会居中,而不是默认状态下的居左。
        observer: true, //修改swiper自己或子元素时,重新加载
        observeParents: true, //修改swiper的父元素时,重新加载
        centerInsufficientSlides: true, //当slides 的总数小于 slidesPerView 时,slides居中。
        autoplay: {
          delay: 3000, //3秒切换一次
        },

        // 事件

        on: {
          paginationRender: function () {
            console.log("分页器渲染了");
            // 重新添加事件
            addBullentsEvent();
          },
          autoplayStart: function () {
            console.log("开始自定切换");
          },
          autoplayStop: function () {
            console.log("关闭自动切换");
          },
        },
      });

      addBullentsEvent = () => {
        const bullents = document.getElementsByClassName("bullet-style");
        for (let i = 0; i < bullents.length; i++) {
          console.log("obj.onclick", bullents[i].onclick);
          bullents[i].removeEventListener("click", () => {});
          bullents[i].addEventListener("click", function () {
            mySwiper.slideTo(i + 2); //切换到对应的slide,速度为1秒
            mySwiper.autoplay.start();
          });
        }
      };

      window.addEventListener("load", () => {
        addBullentsEvent();
        document
          .getElementsByClassName("swiper-button-next")[0]
          .addEventListener("click", () => {
            mySwiper.autoplay.start();
          });
        document
          .getElementsByClassName("swiper-button-prev")[0]
          .addEventListener("click", () => {
            mySwiper.autoplay.start();
          });
      });
    </script>
  </body>
</html>

效果:

相关推荐
前端小万2 分钟前
令人头痛的前端环境
前端·前端工程化
明月_清风15 分钟前
Nginx 模块机制深度解析:从核心原理到生产实践
前端·nginx
APIshop31 分钟前
1688 跨境寻源通详情接口深度解析:从接入到实战
前端·网络·chrome
爱上好庆祝39 分钟前
学习js的第四天
前端·css·学习·html·css3·js
d111111111d41 分钟前
UAER问题+修复小bug
前端·javascript·笔记·stm32·单片机·嵌入式硬件·学习
kyriewen111 小时前
Next.js:让你的React应用从“裸奔”到“穿衣服”
开发语言·前端·javascript·react.js·设计模式·ecmascript
MXN_小南学前端1 小时前
基于 Vue3 + ECharts 的数据大屏实例(提供gitHub仓库地址)
前端·javascript·echarts
宁雨桥1 小时前
for of,for in以及传统for循环的区别与不同场景下的使用选择
前端·javascript
椰羊~王小美2 小时前
除了前端 JS 配置的国际化,对于 JS 没覆盖到的文本,怎么实现国际化
前端·javascript·状态模式