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

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

需求:

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

在这里插入图片描述

二、代码实现:

1.封装一个组件myswiper.vue

<!-- 折叠轮播图 组件-->
<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.在需要的页面引用


<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>

完成~

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份使用uniapp框架开发的微信小程序卡片轮播图代码示例: ```html <template> <view class="swiper-container"> <swiper class="swiper-wrapper" :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval" :duration="duration"> <block v-for="(item, index) in cardList" :key="index"> <swiper-item class="swiper-slide"> <view class="card"> <image class="card-img" :src="item.imageUrl"></image> <view class="card-content">{{item.content}}</view> </view> </swiper-item> </block> </swiper> </view> </template> <script> export default { data() { return { indicatorDots: true, autoplay: true, interval: 5000, duration: 1000, cardList: [ { imageUrl: 'https://example.com/card1.jpg', content: '这是卡片1的内容' }, { imageUrl: 'https://example.com/card2.jpg', content: '这是卡片2的内容' }, { imageUrl: 'https://example.com/card3.jpg', content: '这是卡片3的内容' } ] } } } </script> <style> .swiper-container { height: 200rpx; width: 100%; } .swiper-slide { display: flex; justify-content: center; } .card { width: 90%; height: 150rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 6rpx rgba(0,0,0,0.2); display: flex; flex-direction: column; justify-content: flex-end; padding: 20rpx; margin-bottom: 20rpx; } .card-img { width: 100%; height: 80%; border-radius: 10rpx; object-fit: cover; margin-bottom: 10rpx; } .card-content { font-size: 28rpx; color: #333; } </style> ``` 这份代码与前面微信小程序的代码类似,只是使用了uniapp框架进行开发,并且在模板中使用了vue语法。需要注意的是,uniapp中的Swiper组件与微信小程序Swiper组件略有不同,具体使用方式可以参考uniapp官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值