vue+nuxtjs中使用swiper( vue-awesome-swiper)缩略图(双向控制)

本文介绍了如何在Vue项目中集成Swiper组件,创建一个带有顶部轮播和底部缩略图的图片展示效果。通过在main.js中全局引入VueAwesomeSwiper,并在组件中导入Swiper和SwiperSlide,设置相应的样式和选项,实现了上下两个滑动区域的双向控制。在mounted阶段,通过引用Swiper实例来同步两个滑动区域的控制。

效果

在这里插入图片描述

npm安装

 npm install swiper@5.2.0 vue-awesome-swiper@4.1.1 --save

main.js全局安装

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

组件里调用

import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
   name: 'swiper-example-thumbs-gallery',
    title: 'Thumbs gallery with Two-way control',
    components: {
        Swiper,
        SwiperSlide
    },
}

template中

 <div class="thumb-example mt40 mb80">
    <swiper class="swiper gallery-top" :options="swiperOptionTop" ref="swiperTop">
            <swiper-slide v-for="it in item.photos" :key="it.id">
                 <img :src="it.photo_url_path" class="img-cover" alt="">
            </swiper-slide>
         <div class="swiper-button-next swiper-button-white" slot="button-next"></div>
         <div class="swiper-button-prev swiper-button-white" slot="button-prev"></div>
    </swiper>
       <!-- swiper2 Thumbs -->
       <swiper class="swiper gallery-thumbs" :options="swiperOptionThumbs" ref="swiperThumbs">
              <swiper-slide v-for="it in item.photos" :key="it.id">
                     <img :src="it.photo_url_path" class="img-cover" alt="">
               </swiper-slide>
       </swiper>
 </div>

data中定义

           swiperOptionTop: {
                loop: true,
                loopedSlides: 5, // looped slides should be the same
                spaceBetween: 10,
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev'
                }
            },
            swiperOptionThumbs: {
                loop: true,
                loopedSlides: 5, // looped slides should be the same
                spaceBetween: 10,
                centeredSlides: true,
                slidesPerView: 'auto',
                touchRatio: 0.2,
                slideToClickedSlide: true,
            }

mounted中

 mounted () {
        this.$nextTick(() => {
            const swiperTop = this.$refs.swiperTop[0].$swiper
            const swiperThumbs = this.$refs.swiperThumbs[0].$swiper
            swiperTop.controller.control = swiperThumbs
            swiperThumbs.controller.control = swiperTop
        });
    },

style部分

.thumb-example {
    width: 900px;
    &:last-child {
        margin-bottom: 0 !important;
    }
}
.swiper {
    &.gallery-top {
        width: 900px;
        height: 650px;
        cursor: pointer;
    }
    &.gallery-thumbs {
        height: 160px;
        box-sizing: border-box;
        padding: 10px 0;
    }
    &.gallery-thumbs .swiper-slide {
        width: 210px !important;
        height: 100%;
        // opacity: 0.6;
        margin-right: 20px !important;
        cursor: pointer;
        border: 2px solid #fff;
        box-sizing: border-box;
        img {
            width: 100%;
            height: 100%;
        }
    }
    &.gallery-thumbs .swiper-slide-active {
        // opacity: 1;
        border: 2px solid $color-logo;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勋染

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值