uniapp开发小程序,实现堆叠卡片轮播图

一、实现堆叠卡片轮播图:

需求:

  1. 实现堆叠轮播图效果
  2. 堆叠到后面的图片有虚化效果
  3. 可以在堆叠图片上写文字或叠加图片等效果
  4. 可以手动滑动,也可以定时自动轮播

二、代码实现:

1.封装一个组件myswiper.vue

html 复制代码
<!-- 折叠轮播图 组件-->
<template>
	<view  class="swpbig">
		<view class="swiperPanel" ref="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">
					<view class="pic">
						<image class="pici" :src="item.url"></image>
						<image  class="banner_signal" src="../../static/banner_signal.png" mode=""></image>
						<view class="swpnew">标题</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
 
<script>
	var timer;
	export default {
		props: {
			swiperList: {
				type: Array,
				default: ()=>[
					{
						    type: 'Array',
							url: '../../static/ceshi1.png',
						}, {
							type: 'Array',
							url: '../../static/ceshi2.png',
						}, {
							type: 'Array',
							url: '../../static/ceshi3.png',
						}
				]
				}
		},
		data() {
			return {
				slideNote: {
					x: 0,
					y: 0
				},
				screenWidth: 0,
				itemStyle: []
			};
		},
		created() {
			var macInfo = uni.getSystemInfoSync();
			this.screenWidth = macInfo.screenWidth;
			// 计算swiper样式
			this.swiperList.forEach((item, index) => {
				this.itemStyle.push(this.getStyle(index))
			})
			timer = setInterval(()=>{
				this.rightSlider();
			},3000)
		},
		methods: {
			rightSlider(){
				var newList = JSON.parse(JSON.stringify(this.itemStyle));
				var last = [newList.pop()]
				newList = last.concat(newList)
				this.itemStyle = newList;
			},
			getStyle(e) {
				if (e > this.swiperList.length / 2) {
					var right = this.swiperList.length - e
					return {
						transform: 'scale(' + (1 - right / 10) + ') translate(-' + (right * 9) + '%,0px)',
						zIndex: 9999 - right,
						opacity: 0.5 / right
					}
				} else {
					return {
						transform: 'scale(' + (1 - e / 10) + ') translate(' + (e * 9) + '%,0px)',
						zIndex: 9999 - e,
						opacity: 0.5 / e
					}
				}
			},
			startMove(e) {
				clearInterval(timer);
				this.slideNote.x = e.changedTouches[0] ? e.changedTouches[0].pageX : 0;
				this.slideNote.y = e.changedTouches[0] ? e.changedTouches[0].pageY : 0;
			},
			endMove(e) {
				timer = setInterval(()=>{
					this.rightSlider();
				},3000)
				var newList = JSON.parse(JSON.stringify(this.itemStyle));
				if ((e.changedTouches[0].pageX - this.slideNote.x) < 0) {
					// 向左滑动
					var last = [newList.pop()]
					newList = last.concat(newList)
				} else {
					// 向右滑动
					newList.push(newList[0])
					newList.splice(0, 1)
				}
				this.itemStyle = newList
			}
		}
	}
</script>
 
<style lang="scss">
	.swpbig{
		overflow: hidden;
		
	}
	.swiperPanel {
		margin-top: 200upx;
		height: 680upx;
		width: 686upx;
		overflow: hidden;
		position: relative;
		margin-left:80upx;
 
		.swiperItem {
			height: 100%;
			width: 100%;
			position: absolute;
			top: 0;
			left: 0upx;
			transition: all .5s;
 
			.children {
				// height: 100%;
				width: 570upx;
				margin: 2upx 160upx 2upx 0;
				position: relative;
				
				.pic {
					position: relative;
					height: 645rpx;
					width: 560rpx;
					border-radius: 10upx;
				}
				.pici{
				    position: absolute;
				    height: 645rpx;
				    width: 560rpx;
				    border-radius: 10upx;	
				}
				.banner_signal{
					position: absolute;
					height: 645rpx;
					width: 560rpx;
					top: 0;
				}
				.swpnew{
					position: absolute;
					height: 63rpx;
					font-size: 45rpx;
					font-weight: bold;
					color: #FFF;
					line-height: 53rpx;
					right: 28rpx;
					margin-top: 12rpx;
					letter-spacing: 5rpx;
				}
			}
		}
	}
</style>
 

2.在需要的页面引用

html 复制代码
<template>
	<view class="">
		<mySwiper :swiperList='swiperList'></mySwiper >
	</view>
</template>

<script>
	// 组件导入
	import mySwiper from '../../components/myswiper.vue'
	export default {
		// 组件注册
		components: {
			blackSwiper
		},

		data() {
			return {
				//组件数据处理:
				swiperList: [{
					type: 'Array',
					url: "https://copyright.bdstatic.com/vcg/creative/cc9c744cf9f7c864889c563cbdeddce6.jpg@h_1280",
				}, {
					type: 'Array',
					url: 'https://inews.gtimg.com/om_bt/OjPq2cnMN_-ivDKjxpCZ2kk_ab8YC5VMnL-0pQ21fUvd4AA/1000',
				}, {
					type: 'Array',
					url: 'https://inews.gtimg.com/om_bt/OuevRi3lDJoCccAqM17UARGbNlk9CRf3pGPv7He7zA8yYAA/1000',
				}]
			};
		},

	}
</script>

完成~

相关推荐
自然 醒28 分钟前
荣耀手机,系统MagicOS 9.0 USB配置没有音频来源后无法被adb检测到,无法真机调试的解决办法
adb·uni-app
全职计算机毕业设计2 小时前
基于微信小程序的城市特色旅游推荐应用的设计与实现
微信小程序·小程序
*拯6 小时前
Uniapp Android/IOS 获取手机通讯录
android·ios·uni-app
gaojianqiao12347 小时前
uniapp引入七鱼客服微信小程序SDK
微信小程序·uni-app
说私域8 小时前
线下消费经济“举步维艰”,开源AI智能名片链动2+1+S2B2C小程序线上“狂飙突进”!
人工智能·小程序·开源·零售
zhangzuying10269 小时前
在uni-app中实现类似文心一言的流式对话功能:从fetch到websocket的实践
websocket·uni-app·文心一言
假客套13 小时前
2025 后端自学UNIAPP【项目实战:旅游项目】3、API接口请求封装,封装后的简单测试以及实际使用
uni-app·旅游项目实战
JAVA叶知秋21 小时前
uniapp自定义底部导航栏h5有效果小程序无效的解决方案
小程序·uni-app
moxiaoran57531 天前
uni-app学习笔记(二)--vue页面代码的构成和新建页面
笔记·学习·uni-app
一只程序熊1 天前
【uniapp】errMsg: “navigateTo:fail timeout“
服务器·前端·uni-app