vue中实现带缩略图得轮播图(vue-awesome-swiper)

1.引入swiper和vue-awesome-swiper插件

npm install swiper@4 --save
npm install vue-awesome-swiper@3 --save

2.在main.js中引入:

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

3.使用:
template:布局

<template>
  <div>
    <div class="thumb-example">
      <!-- swiper1 -->
      <swiper
        class="swiper gallery-top"
        :options="swiperOptionTop"
        ref="swiperTop"
      >
        <swiper-slide class="slide-1" v-for="item in bigImg" :key="item.id">
          <img :src="item.url" style="height:570px;width:100%" 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
          class="slide"
          style="width:100px;height:100px;"
          v-for="item in bigImg"
          :key="item.id"
        >
          <img style="width:100px;height:100px;" :src="item.url" alt="" />
        </swiper-slide>
        <div class="swiper-button-next swiper-button-white" slot="button-next">
          <div>
            <img src="../../../../assets/images/nextImg.png" alt="" />
          </div>
        </div>
        <div class="swiper-button-prev swiper-button-white" slot="button-prev">
          <div>
            <img src="../../../../assets/images/prevImg.png" alt="" />
          </div>
        </div>
      </swiper>
    </div>
  </div>
</template>

<script>
export default {
  mounted() {
    // 实现swiper双向控制
    this.$nextTick(() => {
      const swiperTop = this.$refs.swiperTop.swiper
      const swiperThumbs = this.$refs.swiperThumbs.swiper
      swiperTop.controller.control = swiperThumbs
      swiperThumbs.controller.control = swiperTop
    })
  },
  data() {
    return {
      //轮播大图配置
      bigImg: [
        {
          url: 'https://t7.baidu.com/it/u=3165657288,4248157545&fm=193&f=GIF',
          id: 0
        },
        {
          url: 'https://t7.baidu.com/it/u=2942499027,2479446682&fm=193&f=GIF',
          id: 1
        },
        {
          url: 'https://t7.baidu.com/it/u=2610975262,3538281461&fm=193&f=GIF',
          id: 2
        },
        {
          url: 'https://t7.baidu.com/it/u=4138158235,3956816634&fm=193&f=GIF',
          id: 3
        },
        {
          url: 'https://t7.baidu.com/it/u=2942499027,2479446682&fm=193&f=GIF',
          id: 4
        },
        {
          url: 'https://t7.baidu.com/it/u=2610975262,3538281461&fm=193&f=GIF',
          id: 5
        }
      ],
      swiperOptionTop: {
        zoom: true,
        loop: true,
        loopedSlides: 5, // looped slides should be the same
        spaceBetween: 10,
        observer: true, //修改swiper自己或子元素时,自动初始化swiper
        observeParents: true, //修改swiper的父元素时,自动初始化swiper
        // autoplay: {  //自动轮播
        //   delay: 2000,
        //   disableOnInteraction: false
        // },
        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,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  },

  methods: {}
}
</script>
<style lang="less" scoped>
h3 {
  margin: 20px 0 0 10px;
}
.thumb-example {
  width: 864px;
  margin-top: 20px;
  // background: #000;
}
.swiper-slide {
  background-size: cover;
  background-position: center;
}
.gallery-top {
  // height: 80% !important;
  height: 600px;
  width: 100%;
}
.gallery-thumbs {
  height: 20% !important;
  box-sizing: border-box;
  padding: 10px 0px;
  width: 864px;
  margin-left: 2px;
  .swiper-button-next {
    right: 0px;
  }
  .swiper-button-prev {
    left: 0px;
  }
  .swiper-button-next,
  .swiper-button-prev {
    background: #fff;
    width: 45px;
    text-align: center;
    height: 101px;
    top: 26%;
    div {
      margin-top: 30px;
      background: rgb(207, 205, 205);
      height: 45px;
      border-radius: 50%;
      img {
        margin: 7px 0 0 2px;
        width: 30px;
      }
    }
  }
  .swiper-button-next:hover div {
    background: rgb(189, 186, 186);
  }
  .swiper-button-prev:hover div {
    background: rgb(189, 186, 186);
  }
}
.gallery-thumbs .swiper-slide {
  width: 20%;
  height: 80px;
  // opacity: 0.4;
}
.gallery-thumbs .swiper-slide-active {
  border: 2px solid red;
}
</style>

最终效果
在这里插入图片描述
好了,版本一定要对,要不然会有坑

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个简单的缩略图轮播图Vue 组件示例: ```vue <template> <div class="carousel"> <div class="carousel-container"> <div class="carousel-slides" :style="{ transform: 'translateX(' + (-currentIndex * slideWidth) + 'px)' }"> <div class="carousel-slide" v-for="(slide, index) in slides" :key="index"> <img :src="slide.image" :alt="slide.title"> <h3>{{ slide.title }}</h3> </div> </div> </div> <div class="carousel-thumbnails"> <div class="carousel-thumbnail" v-for="(slide, index) in slides" :key="index" :class="{ active: index === currentIndex }" @click="setCurrentSlide(index)"> <img :src="slide.thumbnail" :alt="slide.title"> </div> </div> </div> </template> <script> export default { data() { return { slides: [ { title: 'Slide 1', image: 'https://via.placeholder.com/500x300', thumbnail: 'https://via.placeholder.com/100x60' }, { title: 'Slide 2', image: 'https://via.placeholder.com/500x300', thumbnail: 'https://via.placeholder.com/100x60' }, { title: 'Slide 3', image: 'https://via.placeholder.com/500x300', thumbnail: 'https://via.placeholder.com/100x60' } ], currentIndex: 0, slideWidth: 0 } }, mounted() { this.slideWidth = this.$refs.slides.offsetWidth / this.slides.length; }, methods: { setCurrentSlide(index) { this.currentIndex = index; } } } </script> <style> .carousel { position: relative; width: 100%; height: 300px; overflow: hidden; } .carousel-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; } .carousel-slides { display: flex; transition: transform 0.5s ease-in-out; } .carousel-slide { position: relative; width: 100%; height: 100%; flex-shrink: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; } .carousel-slide img { max-width: 100%; max-height: 100%; } .carousel-slide h3 { margin-top: 10px; } .carousel-thumbnails { position: absolute; bottom: 0; left: 0; width: 100%; display: flex; justify-content: center; align-items: center; background-color: rgba(0, 0, 0, 0.5); } .carousel-thumbnail { width: 100px; height: 60px; margin: 10px; cursor: pointer; transition: opacity 0.5s ease-in-out; opacity: 0.5; } .carousel-thumbnail.active { opacity: 1; } </style> ``` 这个轮播图组件包括了一个轮播图和一个缩略图列表。你可以根据需要修改样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值