一般初始化配置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./dist/css/swiper.css">
</head>
<body>
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">
                <img src="./banner1.png" alt="">
            </div>
            <div class="swiper-slide">
                <img src="./banner1.png" alt="">
            </div>
            <div class="swiper-slide">
                <img src="./banner1.png" alt="">
            </div>
        </div>
        <!-- 如果需要分页器(下面的小点点) -->
        <div class="swiper-pagination"></div>
         
        <!-- 如果需要导航按钮 (左右按钮)-->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
         
    </div>
</body>
<script src="./jquery-2.0.0.min.js"></script>
<script src="./dist/js/swiper.js"></script>
<script>
    var mySwiper = new Swiper('.swiper-container', {
        direction: 'horizontal', //轮播方向
        autoplay: true,  //自动轮播
        loop:true,   //无限循环
        speed: 800,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },

        // 如果需要滚动条
        scrollbar: {
            el: '.swiper-scrollbar',
        },
        
        autoplayDisableOnInteraction: false,
        observer: true, //修改swiper自己或子元素时,自动初始化swiper
        observeParents: true, //修改swiper的父元素时,自动初始化swiper
    })
</script>

</html>

swiper禁止手动滑动

只需要在最外层的容器上增加class="swiper-no-swiping"

slideChangeTransitionEnd

回调函数,swiper从一个slide过渡到另一个slide结束时执行。

<swiper
      :options="swiperOption"
      ref="mySwiper"
      @slideChangeTransitionEnd="slideChangeTransitionEnd"
    >
</swiper>


slideChangeTransitionEnd() {
      let index = this.$refs.mySwiper.swiper.realIndex  //获得当前的item的索引
      this.choiceIntroduce = this.tabContents[index].content  //当前索引下的内容
      this.queryGoods(this.products[index].productId, this.payType) //调接口
      this.$forceUpdate()  //强制更新页面
    },

HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!


引用和评论

0 条评论