nuxt使用swiper,nuxt一个页面使用两个swiper,nuxt关联两个swiper

1 篇文章 0 订阅

1.安装swiper

            npm install vue-awesome-swiper --save


2.在plugins下新建vue-swiper.js文件

            import Vue from 'vue'
            import VueAwesomeSwiper from 'vue-awesome-swiper/dist/ssr'
            Vue.use(VueAwesomeSwiper)


3.在nuxt.config.js中引入

       css: [
          "swiper/dist/css/swiper.css"
        ],        
       plugins: [
           { src: "@/plugins/vue-swiper.js", ssr: false },
       ],

4. html部分

        <!--v-swiper:mySwiper 对应名字不同即可,mySwiper就是实例,可以直接this.mySwiper获取-->
        <div v-swiper:mySwiper="swiperOption" >
            <div class="swiper-wrapper">
               <div class="swiper-slide" v-for="banner in imglist">
                  <img :src="banner" class="swiper-img" @click="bigImg(banner)">
               </div>
            </div>
        </div>
        <div class="swiper-pagination swiper-pagination-bullets"></div>
        <div class="big-img" @click="closeBigImg">
            <div v-swiper:mySwiper2="swiperOption2" >
                <div class="swiper-wrapper">
                    <div class="swiper-slide swiper-slide-2" v-for="banner in imglist">
                       <img :src="banner" class="swiper-img-2">
                    </div>
                </div>

            </div>
        </div>

5. js实例及关联两个swiper

data () {
    return {
        banners: [],//图片集合
        swiperOption: {
            loop: true,
            /*slidesPerView: 'auto',*/
            centeredSlides: true,
            spaceBetween: 30,
            pagination: {
                el: '.swiper-pagination',
                type: 'fraction',
            },
            /*autoplay: {
                disableOnInteraction: false,  // 用户操作swiper之后,是否禁止autoplay
                delay: 3000, // 自动切换的时间间隔(单位ms)
            },*/
        },
        swiperOption2: {
            loop: true,
            centeredSlides: true,
            spaceBetween: 30,
        }
    }
},
mounted() {
    this.mySwiper.controller.control = this.mySwiper2; //Swiper1控制Swiper2
    this.mySwiper2.controller.control = this.mySwiper; //Swiper2控制Swiper1
},

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值