swiper缩略图轮播2

使用vue3+ts完成swiper缩略图的效果,起初以为这个swiper是一个组件就可以完成,但现实是两个swiper做了属性互调,官网上没有描述清楚缩略图是否可以轮播,导致走了很多弯路(获取上图的实例然后操作swiper使其完成走马灯的效果)

1.安装并引入

复制代码

yarn

复制代码

yarn add swiper // 安装swiper依赖

2.在main.ts中引入他的css文件

复制代码

js

复制代码

import "swiper/css"

3.在你所需的页面中引入

复制代码

js

复制代码

<template> <div style="height: 100vh; overflow: hidden"> <swiper class="swiper mySwiper2" :spaceBetween="10" :thumbs="{ swiper: thumbsSwiper }" :modules="modules" :loop="true" :autoplay="true" :navigation="true" > <swiper-slide v-for="(item, index) of slides" :key="index"> <img class="img" :src="item" @click="toDetail" /> </swiper-slide> </swiper> <swiper @swiper="setThumbsSwiper" :spaceBetween="10" class="swiper mySwiper" :slidesPerView="4" :watchSlidesProgress="true" :modules="modules" :autoplay="true" :loop="true" > <swiper-slide v-for="(item, index) of slides" :key="index"> <img class="img" :src="item" /> </swiper-slide> </swiper> </div> </template> <script setup lang="ts"> import { ref, reactive } from "vue"; import { Swiper, SwiperSlide } from "swiper/vue"; import "swiper/css"; import "swiper/css/free-mode"; import "swiper/css/navigation"; import "swiper/css/pagination"; import "swiper/css/thumbs"; import { Thumbs, Navigation, Autoplay, Zoom, FreeMode } from "swiper/modules"; const modules = [Thumbs, Navigation, Autoplay, Zoom, FreeMode]; //必须要在这里面引入,只在组件定义的属性不生效,前面这里面没有定义autoPlay导致我的组件轮播不了,一度以为是swiper没有支持缩略图轮播 import { useRouter } from "vue-router"; const slides = reactive([ new URL("@/assets/images/1.png", import.meta.url).href, new URL("@/assets/images/2.png", import.meta.url).href, new URL("@/assets/images/3.png", import.meta.url).href, new URL("@/assets/images/4.png", import.meta.url).href, new URL("@/assets/images/5.png", import.meta.url).href, new URL("@/assets/images/6.png", import.meta.url).href, new URL("@/assets/images/7.png", import.meta.url).href, new URL("@/assets/images/8.png", import.meta.url).href, ]); const thumbsSwiper = ref<any>(null); const setThumbsSwiper = (swiper: any) => { thumbsSwiper.value = swiper; }; const router = useRouter(); const toDetail = () => { router.push("/detail"); }; </script> <style scoped lang="less"> .swiper { width: 100%; height: 100%; } .swiper-slide { text-align: center; font-size: 18px; background: #fff; display: flex; justify-content: center; align-items: center; } .swiper-slide img { display: block; width: 100%; height: 100%; object-fit: cover; } :deep(.swiper-button-prev, .swiper-button-next) { color: #04af85; } :deep(.swiper-button-next) { color: #04af85; } body { background: #000; color: #000; } .swiper { width: 100%; height: 300px; margin-left: auto; margin-right: auto; } .swiper-slide { background-size: cover; background-position: center; cursor: pointer; } .mySwiper2 { height: 80%; width: 100%; } .mySwiper { height: 20%; box-sizing: border-box; padding: 10px 0; background-color: #000; } .mySwiper .swiper-slide { width: 25%; height: 100%; opacity: 0.4; } .mySwiper .swiper-slide-thumb-active { opacity: 1; } .swiper-slide img { display: block; width: 100%; height: 100%; object-fit: cover; } </style>

注:自己写的自动轮播效果

复制代码

js

复制代码

// 在topSwiper引入activeIndexChange选中时间方法 @activeIndexChange="resetTimer" // 在topSwiper中获取实例 @swiper="setSwiper($event)" const setSwiper = (swiperInstance: any) => { swiperdata.value = swiperInstance } // 获取实例使用实例内嵌的slideTo方法切换 const goToNextSlide = () => { if (swiperdata.value) { const nextIndex = (swiperdata.value.activeIndex + 1) % slides.length // 获取当前页的下一个图片的索引 swiperdata.value.slideTo(nextIndex) // 选中当前页的下一个图片 } } //写的一个时间函数 const timer = ref<any>(null) //在图片选中状态会移除,重新再生成一个,避免用户点击或者滑动查看某一项时间未重置的问题 const resetTimer = () => { if (timer) clearInterval(timer.value) timer.value = setInterval(() => { goToNextSlide() }, 5000) } //最后在页面挂载后调用 resetTimer()

相关推荐
一 乐15 分钟前
智慧党建|党务学习|基于SprinBoot+vue的智慧党建学习平台(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·学习
BBB努力学习程序设计1 小时前
CSS Sprite技术:用“雪碧图”提升网站性能的魔法
前端·html
BBB努力学习程序设计1 小时前
CSS3渐变:用代码描绘色彩的流动之美
前端·html
冰暮流星1 小时前
css之动画
前端·css
jump6802 小时前
axios
前端
spionbo2 小时前
前端解构赋值避坑指南基础到高阶深度解析技巧
前端
用户4099322502122 小时前
Vue响应式声明的API差异、底层原理与常见陷阱你都搞懂了吗
前端·ai编程·trae
开发者小天2 小时前
React中的componentWillUnmount 使用
前端·javascript·vue.js·react.js
永远的个初学者2 小时前
图片优化 上传图片压缩 npm包支持vue(react)框架开源插件 支持在线与本地
前端·vue.js·react.js
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
npm i / npm install 卡死不动解决方法
前端·npm·node.js