Time:2022-11-08 21:47:20 Author:Gavin

Step

  • Fitst: 执行npm i -S swiper@5,添加 swiper 库,因为 6 可能有 bug,所以用 5
    引包(css),在main.js中加入import "swiper/css/swiper.min.css";
  • Second: 书写 template 中 DOM 结构

    <div class="swiper-container" ref="cur">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="carousel in list" :key="carousel.id">
          <img :src="carousel.imgUrl" />
        </div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>
    
      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
  • Third: new Swiper 实例,实现展示效果,必须在所有 DOM 结构生成后起效
    引入import Swiper from "swiper";
    在生命周期或方法里添加如下代码将 swiper 实例化

    var mySwiper = new Swiper("获取到的DOM节点", {
        loop: true,
        // 如果需要分页器
        pagination: {
          el: ".swiper-pagination",
          //点击小球的时候也切换图片
          clickable: true,
        },
        // 如果需要前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      });

悠悠江水
33 声望0 粉丝

一路前进,一路成长,终身学习