当前位置: 首页 > news >正文

vue2轮播图组件

子组件

<template><div class="swiper-container" @touchstart="handleTouchStart"@touchend="handleTouchEnd"><!-- 轮播内容 --><div class="swiper-wrapper" :style="wrapperStyle"><div v-for="(item, index) in slides" :key="index"class="swiper-slide"><img :src="item.image"><h2>{{ item.title }}</h2></div></div><!-- 分页器 --><div class="swiper-pagination"><span v-for="n in slides.length" :key="n":class="{ active: currentIndex === n - 1 }"@click="goTo(n - 1)"></span></div><!-- 导航按钮 --><div class="swiper-button-prev" @click="prev"></div><div class="swiper-button-next" @click="next"></div></div>
</template><script>
export default {props: {slides: {type: Array,required: true},autoplay: {type: Boolean,default: true},interval: {type: Number,default: 3000}},data() {return {currentIndex: 0,startX: 0,timer: null}},computed: {wrapperStyle() {return {transform: `translateX(-${this.currentIndex * 100}%)`,transition: 'transform 0.3s ease'}}},mounted() {this.startAutoplay()console.log('this.slides ',this.slides);},beforeDestroy() {this.clearAutoplay()},methods: {startAutoplay() {if (this.autoplay) {this.timer = setInterval(this.next, this.interval)}},clearAutoplay() {if (this.timer) {clearInterval(this.timer)this.timer = null}},handleTouchStart(e) {this.startX = e.touches[0].clientXthis.clearAutoplay()},handleTouchEnd(e) {const endX = e.changedTouches[0].clientXconst diffX = endX - this.startXif (Math.abs(diffX) > 50) { // 滑动阈值diffX > 0 ? this.prev() : this.next()}this.startAutoplay()},goTo(index) {if (index >= 0 && index < this.slides.length) {this.currentIndex = index}},prev() {this.currentIndex = this.currentIndex > 0 ? this.currentIndex - 1 : this.slides.length - 1},next() {this.currentIndex = this.currentIndex < this.slides.length - 1 ? this.currentIndex + 1 : 0}}
}
</script><style scoped>
.swiper-container {position: relative;overflow: hidden;width: 100%;height: 400px;
}.swiper-wrapper {display: flex;height: 100%;
}.swiper-slide {flex-shrink: 0;width: 100%;height: 100%;}.swiper-pagination {position: absolute;bottom: 10px;width: 100%;display: flex;justify-content: center;gap: 8px;
}.swiper-pagination span {width: 8px;height: 8px;border-radius: 50%;background: rgba(255,255,255,0.5);cursor: pointer;transition: background 0.3s;
}.swiper-pagination span.active {background: #fff;
}.swiper-button-prev,
.swiper-button-next {position: absolute;top: 50%;transform: translateY(-50%);width: 40px;height: 40px;background: rgba(0,0,0,0.3);cursor: pointer;border-radius: 50%;display: flex;align-items: center;justify-content: center;color: white;
}.swiper-button-prev::after,
.swiper-button-next::after {content: '';width: 10px;height: 10px;border: 2px solid white;border-width: 2px 2px 0 0;
}.swiper-button-prev {left: 10px;transform: rotate(-135deg);
}.swiper-button-next {right: 10px;transform: rotate(45deg);
}.swiper-button-prev::after,
.swiper-button-next::after {margin-top: 0px;
}
</style>

父组件

<template><div><Swiper :slides="slides" :interval="5000"></Swiper></div>
</template><script>
import Swiper from '../components/swiperV.vue'export default {name: 'VueMainjsswiper',components: { Swiper },data() {return {slides: [{ image: require("@/assets/avtor.webp"), title: 'Slide 1' },{ image: require("@/assets/avtor.webp"), title: 'Slide 2' },{ image: require("@/assets/avtor.webp"), title: 'Slide 3' }]}},mounted() {console.log('Slides Data:', JSON.stringify(this.slides))
}
}
</script>

运行效果:
在这里插入图片描述

http://www.xdnf.cn/news/683119.html

相关文章:

  • 计算机网络实验课(二)——抓取网络数据包,并实现根据条件过滤抓取的以太网帧,分析帧结构
  • 如何检查液质联用仪LCMS的真空度
  • 提升前端性能:减少DOM操作
  • 在线项目管理工具对比:Trello、Worktile等20款软件测评
  • Java的Spring Cloud生态中实现SSE(Server-Sent Events)服务端实践
  • YoloV11改进策略:卷积篇-风车卷积-即插即用
  • 代码随想录算法训练营第60期第四十九天打卡
  • day05-常用API(二):Lambda、方法引用详解
  • Python装饰器与异常捕获的高级用法详解
  • 基于 STM32 的农村污水处理控制系统设计与实现
  • @vue/composition-api
  • uniapp-商城-72-shop(5-商品列表,购物车实现回顾)
  • Linux 6.15 内核发布,新功能
  • 【免费】【无需登录/关注】坐标系批量转换与可视化网页工具
  • 31. 自动化测试开发之实现INI配置文件解析
  • 从CPU缓存出发对引用池进行优化
  • C51-指针函数
  • Linux编译器——gcc/g++的使用
  • 基于Python的智能天气提醒助手开发指南
  • ValueError: BuilderConfig ‘xxxx‘ not found. Available:[xxx]
  • Cannot read properties of undefined (reading ‘clearSelection‘)
  • 华为仓颉语言初识:并发编程之线程的基本使用
  • PCB线路板压合工艺难点解析与技术对策
  • NB-IoT NPUSCH(三)-资源映射
  • gdiplus,GDI +为什么2001年发布后几乎没有再更新了
  • 2025 海外短剧 CPS 系统开发:技术驱动下的全球化内容分销新范式
  • SSM整合:Spring+SpringMVC+MyBatis完美融合实战指南
  • 第十二天 区块链在车辆数据存证中的应用
  • Erp系统介绍与业务方案详情
  • 彻底理解一个知识点的具体步骤