Vue框架使用swiper5实现带缩略图的轮播图

 首先看效果图先,毕竟需求都是不一样的嘛,我发这个文章主要是看了一些网页上的好多都存在缩略图那里的高亮居中问题,所以本文章主要就是实现当前图片看到的一些效果。(声明一下,本人第一次发文章,还有很多要学习的地方,所以不适合的话勿喷。)

供第一,装依赖要对得上,我这里用的是 "swiper": "5.4.5"和"vue-awesome-swiper": "^3.1.3",很多人在这就会有疑问,这两版本不对应啊,不急,这就是我发这文章的意义,一是记录一下自己的坑,二是同道中人参考。我为啥不装对应的包呢,因为我这个项目前面写的人已经装了这个了,我不能改呀,改了别人的就会出错了。这里我也提供下对应版本吧,影响不大,只要是"swiper": "5.4.5"应该都可以实现图中效果。(对了,记得在main.js全局引入swiper)

 我就按照个人习放顺序了,

 图中可以看到有三个点击事件,目的就是为了拿取到图片的下标给相对应的缩略图高亮边框,在swiper官网中是通过slideChangeTransitionEnd(swiper)函数回调下标index的,我还没研透它的gallerySwiper.thumbs.swiper.$el.css({'border':'1px solid #ff6600'})复制样式,所以就先用我的傻瓜方法了。看看轮播图的轮播数据,

这里没有太多可以说的地方,注释也有写了,所以就过了 

这三个就是对应上面的下标获取的问题, 最后放上我的CSS样式吧。

 注释部分的代码是用了相对应的swiper版本的,我这个版本是不对应的。最后放个可以整体的代码吧。

<template>
  <div id="app">
    <swiper ref="swiper" :options="swiperOptions">
      <swiper-slide v-for="(item, index) in imgList" :key="'swiper' + index">
        <div style="padding: 0 120px">
          <img class="swiper-slide__img" :src="item" alt="" />
        </div>
      </swiper-slide>
      <div
        class="swiper-button-prev"
        @click="buttonPrev"
        slot="button-prev"
      ></div>
      <div
        class="swiper-button-next"
        @click="buttonNext"
        slot="button-next"
      ></div>
    </swiper>
    <div class="swiper gallery-thumbs">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="(item, index) in imgList"
          :key="'thumbs' + index"
          @click="change(index)"
          :class="number == index ? 'cellActive' : ''"
        >
          <div class="swiperImg">
            <img class="swiper-slide__img2" :src="item" alt="" />
          </div>
        </div>
      </div>
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next" id="myNext"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {},
  data() {
    return {
      number: 0,
      swiperOptions: {
        spaceBetween: 10,//大轮播图间距
        navigation: {//大轮播图的切图按钮,如果想放在轮播图中可以看swiper官方文档
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        thumbs: {
          swiper: {
            el: ".gallery-thumbs",
            slidesPerView: 6,//缩略图显示张数
            slidesPerGroup: 6,//缩略图每组切图张数
            spaceBetween: 10,//缩略图间距
            slideToClickedSlide: true,
            navigation: {//缩略图的切图按钮,如果想放在轮播图中可以看swiper官方文档
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          },
          autoScrollOffset: true,
        },
      },
      imgList: [
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5xyw.jpg",
        "https://w.wallhaven.cc/full/zy/wallhaven-zym92v.jpg",
        "https://w.wallhaven.cc/full/9d/wallhaven-9d6wg8.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5x7y.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5ro5.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5xyw.jpg",
        "https://w.wallhaven.cc/full/zy/wallhaven-zym92v.jpg",
        "https://w.wallhaven.cc/full/9d/wallhaven-9d6wg8.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5x7y.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5ro5.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5xyw.jpg",
        "https://w.wallhaven.cc/full/zy/wallhaven-zym92v.jpg",
        "https://w.wallhaven.cc/full/9d/wallhaven-9d6wg8.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5x7y.jpg",
        "https://w.wallhaven.cc/full/jx/wallhaven-jx5ro5.jpg",
      ],
    };
  },
  mounted() {},
  methods: {
    buttonNext() {
      this.number = this.number + 1;
//这里因为一些原因我找不到它原来问题,但我解决了自己问题了,这里是六张一组缩列图,当第七张的时候缩列图就该切组
      if (this.number == 6) {
        document.getElementById("myNext").click();
      }
    },
    buttonPrev() {
      this.number = this.number - 1;
    },
    change(index) {
      this.number = index;
    },
  },
};
</script>

<style>
html {
  background-color: #121212;
}
</style>

<style lang="scss" scoped>
#app {
  width: 1200px;
  margin: 200px auto 0;
}
.swiper-slide {
  &__img {
    width: 100%;
    height: 600px;
  }
}
.swiper {
  &.gallery-thumbs {
    position: relative;
    overflow: hidden;
    box-sizing: border-box;
    height: 150px;
    padding: 10px 60px;
    background-color: rgb(105, 105, 105);
    &::before,
    &::after {
      z-index: 2;
      position: absolute;
      top: 0;
      content: "";
      height: 150px;
      width: 60px;
      background-color: rgb(105, 105, 105);
    }
    &::before {
      left: 0;
    }
    &::after {
      right: 0;
    }
    .swiper-button-next,
    .swiper-button-prev {
      top: 50% !important;
      transform: translateY(-50%) !important;
      width: 40px;
      height: 100px;
      margin-top: 0 !important;
      border-radius: 8px;
      color: #f3f4f4;
      background-color: rgba(0, 0, 0, 0.7);
      &::after {
        font-size: 2rem;
      }
    }
  }
  // &.gallery-thumbs .swiper-slide {
  //   opacity: 0.4;
  //   overflow: hidden;
  // }
  // &.gallery-thumbs .swiper-slide-thumb-active {
  //   border: 2px solid red;
  // }
}
.swiperImg {
  width: 171px;
  height: 120px;
}
.swiper-slide__img2 {
  height: 120px;
  width: 100%;
}
.cellActive {
  border: 2px solid red;
}
</style>

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3中使用Swiper 8实现缩略图和自动播放功能,你可以按照以下步骤进行操作: 1. 首先,在你的项目中安装Swiper 8和Vue-Awesome-Swiper依赖: ``` npm install swiper@8.0.0 vue-awesome-swiper@4.1.1 ``` 2. 在你的`main.js`文件中引入SwiperVue-Awesome-Swiper,并注册它们: ```javascript import { createApp } from 'vue'; import SwiperCore, { Navigation, Autoplay, Thumbs } from 'swiper'; import { Swiper, SwiperSlide } from 'vue-awesome-swiper'; // 引入Swiper样式 import 'swiper/swiper-bundle.css'; // 注册Swiper组件 SwiperCore.use(\[Navigation, Autoplay, Thumbs\]); const app = createApp(App); app.component('swiper', Swiper); app.component('swiper-slide', SwiperSlide); app.mount('#app'); ``` 3. 在你的组件中使用Swiper组件,并配置缩略图和自动播放选项: ```html <template> <swiper :thumbs="true" :autoplay="{ delay: 3000 }"> <swiper-slide v-for="(item, index) in slides" :key="index"> <!-- 此处为你的轮播内容 --> </swiper-slide> </swiper> </template> <script> export default { data() { return { slides: \[ // 轮播内容数据 \] }; } }; </script> ``` 请注意,以上代码是基于Vue3和Swiper 8的示例,确保你已经正确安装了相关依赖并按照上述步骤进行了配置。同时,你可以根据自己的需求进行进一步的样式和功能定制。 #### 引用[.reference_title] - *1* [vue使用swiper6.0实现缩略图控制](https://blog.csdn.net/qq_41287423/article/details/117363287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue实现缩略图轮播图vue-awesome-swiper)](https://blog.csdn.net/weixin_45089731/article/details/122865594)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue3使用Swiper实现简单轮播图](https://blog.csdn.net/Zheng_xinle/article/details/125413402)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小聪仔大智慧

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

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

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

打赏作者

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

抵扣说明:

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

余额充值