当前位置: 首页> 游戏> 手游 > uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页

uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页

时间:2025/7/9 15:19:42来源:https://blog.csdn.net/weixin_45563734/article/details/139979670 浏览次数:0次

uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页 超过指定时间未点击滑动 则继续开始滚动

在这里插入图片描述

  1. 直接上代码 componentSwiper.vue 需要注意页面切换时清除计时器
<template><view><view class="swiperPanel" @touchstart="startMove" @touchend="endMove"><view class="swiperItem" v-for="(item, index) in swiperList" :key="index":style="{transform: itemStyle[index].transform, zIndex: itemStyle[index].zIndex, opacity: itemStyle[index].opacity}"><view class="children" @click="routerTo(item)"><image class="pic" :src="item.url"></image><text class="name">{{item.name}}</text></view></view></view></view>
</template><script>export default {props: {swiperList: {type: Array,default: [{url: 'https://cdn.uviewui.com/uview/goods/1.jpg',name: '这是一个图片'}]},// 自动翻页 间隔2秒timeNum:{type:Number,default:2000},// 点击后 5秒内未操作 自动翻页开启interval:{type:Number,default:5000},},data() {return {slideNote: {x: 0,y: 0},screenWidth: 0,itemStyle: [],timer: null,timer1: null};},watch: {swiperList: {handler(val) {if (val.length) {var macInfo = uni.getSystemInfoSync();this.screenWidth = macInfo.screenWidth;// 计算swiper样式this.swiperList.forEach((item, index) => {this.itemStyle.push(this.getStyle(index))})}},deep: true,immediate: true}},mounted() {this.doSomething()},onUnload() {this.timer = nullthis.timer1 = null},beforeDestroy() {this.timer = nullthis.timer1 = null},methods: {doSomething() {this.$nextTick(() => {this.timer = setInterval(() => {var newList = JSON.parse(JSON.stringify(this.itemStyle))// 向左滑动var last = [newList.pop()]newList = last.concat(newList)this.itemStyle = newList}, this.timeNum)})},routerTo(data) {// 此处为点击逻辑// 或者给父组件抛出事件// this.$emit("方法名字",参数)},getStyle(e) {if (e > this.swiperList.length / 2) {var right = this.swiperList.length - ereturn {transform: 'scale(' + (1 - right / 10) + ') translate(-' + (right * 14) + '%,0px)',zIndex: 100 - right,opacity: 0.8 / right}} else {return {transform: 'scale(' + (1 - e / 10) + ') translate(' + (e * 14) + '%,0px)',zIndex: 100 - e,opacity: 0.8 / e}}},startMove(e) {clearInterval(this.timer)this.timer = nullthis.slideNote.x = e.changedTouches[0] ? e.changedTouches[0].pageX : 0;this.slideNote.y = e.changedTouches[0] ? e.changedTouches[0].pageY : 0;},endMove(e) {var newList = JSON.parse(JSON.stringify(this.itemStyle))if ((e.changedTouches[0].pageX - this.slideNote.x) < -10) {// 向左滑动var last = [newList.pop()]newList = last.concat(newList)} else if ((e.changedTouches[0].pageX - this.slideNote.x) >= 10) {// 向右滑动newList.push(newList[0])newList.splice(0, 1)}this.itemStyle = newList// 清除之前的定时器,以防多次点击clearTimeout(this.timer1);// 设置定时器,5秒后执行doSomething方法this.timer1 = setTimeout(this.doSomething, this.interval);}}}
</script><style lang="scss">.swiperPanel {margin: 20rpx 0;height: 360rpx;width: 100%;overflow: hidden;position: relative;.swiperItem {height: 100%;width: 100%;position: absolute;top: 0;left: 0;transition: all .5s;.children {height: 100%;width: 580rpx;margin: 2rpx auto;position: relative;.pic {height: 100%;width: 100%;border-radius: 20px;// box-shadow: 0 0 10px #333;}.name {position: absolute;width: 100%;bottom: 0;left: 0;background: rgba(0, 0, 0, 0.5);box-shadow: 0rpx 4rpx 21rpx 0rpx rgba(0, 0, 0, 0.07);border-radius: 0 0 20px 20px;height: 85rpx;line-height: 85rpx;font-family: Source Han Sans SC, Source Han Sans SC;font-weight: 400;font-size: 32rpx;color: #FFFFFF;text-align: center;font-style: normal;text-transform: none;}}}}
</style>
  1. 组件使用
	<view class=""><componentSwiper :swiper-list="spotList" /></view>
<script>
import componentSwiper from '@/components/componentSwiper.vue'
export default {components: {componentSwiper},data() {return {spotList:[{url: 'https://cdn.uviewui.com/uview/goods/1.jpg',name: '这是一个图片'},{url: 'https://cdn.uviewui.com/uview/goods/1.jpg',name: '这是一个图片'}]}}
}
  1. 以上为全部代码,希望可以帮到您,您也可以更具自身需求进行修改。
  2. 日常记录!完成。
关键字:uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: