uni-app微信小程序之红包雨活动完整源码

 更新时间:2024年01月30日 11:47:47   作者:范特西是只猫  
最近公司需求做一个微信红包雨功能,这里给大家总结下实现的方法,这篇文章主要给大家介绍了关于uni-app微信小程序之红包雨活动的相关资料,需要的朋友可以参考下

1. 页面效果

GIF录屏有点卡,实际比较丝滑

每0.5s掉落一个红包

控制4s后自动移除红包

点击红包消除红包(或者自行+1,或者弹窗需求)

2. 页面样式代码

<!-- 红包雨活动 -->
<template>
	<scroll-view scroll-y="true">
		<view class="red-envelope-rain">
			<view v-for="(redEnvelope, index) in redEnvelopes" :key="index" class="red-envelope"
				:style="{ top: redEnvelope.top + 'px', left: redEnvelope.left + 'px' }"
				@click="handleRedEnvelopeClick(index)"></view>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				redEnvelopes: [],
				redEnvelopeInterval: null,
			}
		},
		onLoad(options) {
			// 每秒创建一个红包
			setInterval(this.initializeRedEnvelopes, 500); 
			// 更新红包位置,约 60 帧
			setInterval(this.moveRedEnvelopes, 1000 / 60); 
		},
		beforeDestroy() {
			this.stopRedEnvelopeRain();
		},
		methods: {
			initializeRedEnvelopes() {
				const numRedEnvelopes = 20; // 红包数量
				// for (let i = 0; i < numRedEnvelopes; i++) {
				const redEnvelope = {
					id: Date.now(),
					top: 0, // 随机纵坐标
					left: Math.random() * (uni.getSystemInfoSync().windowWidth - 50), // 随机横坐标
					speed: Math.random() * 6 + 1, // 随机速度
				};
				this.redEnvelopes.push(redEnvelope);

				setTimeout(() => {
					this.redEnvelopes = this.redEnvelopes.filter(p => p.id !== redEnvelope.id);
				}, 4000); // 4秒后移除红包
			},
			startRedEnvelopeRain() {
				this.redEnvelopeInterval = setInterval(() => {
					this.moveRedEnvelopes();
				}, 1000 / 60); // 每秒60帧
			},
			stopRedEnvelopeRain() {
				clearInterval(this.redEnvelopeInterval);
			},
			moveRedEnvelopes() {
				this.redEnvelopes.forEach((redEnvelope, index) => {
					console.log(redEnvelope, "redEnvelopes")
					redEnvelope.top += redEnvelope.speed;
					if (redEnvelope.top > uni.getSystemInfoSync().windowHeight) {
						this.redEnvelopes = this.redEnvelopes.filter(p => p.id !== redEnvelope.id);
					}
				});
			},
			// 处理红包点击事件,可以增加分数或显示提示等操作
			handleRedEnvelopeClick(index) {
				// 例如:this.score += 1;
				// 或者:this.showTip = true;
				// 可以根据实际需求自行添加逻辑
				this.redEnvelopes.splice(index, 1); // 点击后移除红包
			},
		}
	}
</script>

<style lang="scss">
	.red-envelope-rain {
		position: relative;
		overflow: hidden;
		width: 100vw;
		height: 100vh;
	}

	.red-envelope {
		position: absolute;
		background-image: url('https://i-1.lanrentuku.com/2020/11/4/a9b4bcdb-75f3-429d-9c21-d83d945b088e.png?imageView2/2/w/500');
		background-size: 100rpx 100rpx;
		background-repeat: no-repeat;
		width: 100rpx;
		height: 100rpx;
	}
</style>

附:uniapp仿微信红包打开动画效果

<template>
	<view>
		<view v-if="packerState != 3" class="packer-box flex-column">
			<view class="packer-bg anim-ease-in" :class="{ 'anim-fade-out': packerState == 2 }"></view>
			<view class="packer-bottom-box anim-ease-in" :class="{ 'anim-out-bottom': packerState == 2 }">
				<view class="arc-bottom-edge"></view>
				<view class="packer-bottom-bg"></view>
			</view>
			<view class="packer-top-box anim-ease-in" :class="{ 'anim-out-top': packerState == 2 }">
				<view class="flex-row sender-info">
					<image class="sender-avatar"></image>
					<view>{{'XXX'}}发出的红包</view>
				</view>
				<view class="packer-greeting double-text">{{'恭喜发财,大吉大利'}}</view>
				<view class="arc-edge"></view>
				<view v-if="packerState == 1" class="anim-rotate packer-btn-pos">
					<view class="packer-btn" style="transform: translateZ(-4px);">開</view>
					<view class="packer-btn-middle" v-for="(item, index) in 7" :key="index" :style="{transform: `translateZ(${index - 3}px)`}"></view>
					<view class="packer-btn packer-btn-front">開</view>
				</view>
				<view v-else class="packer-btn packer-btn-pos" @click="openPacker">開</view>
			</view>
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				packerState: 0
			}
		},
		methods: {
			openPacker() {
				// 加载数据,触发硬币旋转动画
				this.packerState = 1;
				this.request(() => {
					// 调用抢红包接口成功,触发开红包动画
					this.packerState = 2;
					// 开红包动画结束后,移除相关节点,否则会阻挡其它下层节点
					setTimeout(() => {
						this.packerState = 3;
					}, 500);
				})
				
			},
			request(success) {
				setTimeout(() => {
					success()
				}, 3000);
			}
		}
	}
</script>
 
<style>
	.flex-row {
		display: flex;
		flex-direction: row;
		position: relative;
	}
	.flex-column {
		display: flex;
		flex-direction: column;
		position: relative;
	}
	.packer-box {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99;
		color: rgb(235, 205, 153);
		padding: 60rpx;
	}
 
	.packer-bg {
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
	}
 
	.packer-top-box {
		width: 600rpx;
		background-color: rgb(244, 94, 77);
		text-align: center;
		margin: auto;
		transform: translateY(-160rpx);
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		position: relative;
	}
 
	.sender-info {
		margin-top: 200rpx;
		justify-content: center;
		line-height: 60rpx;
		font-size: 36rpx;
	}
 
	.sender-avatar {
		width: 60rpx;
		height: 60rpx;
		border-radius: 10rpx;
		background-color: #fff;
		margin-right: 10rpx;
	}
 
	.packer-greeting {
		font-size: 48rpx;
		line-height: 60rpx;
		height: 120rpx;
		margin: 40rpx 30rpx 200rpx;
	}
 
	.arc-edge {
		position: relative;
	}
 
	.arc-edge::after {
		width: 100%;
		height: 200rpx;
		position: absolute;
		left: 0;
		top: -100rpx;
		z-index: 9;
		content: '';
		border-radius: 50%;
		background-color: rgb(244, 94, 77);
		box-shadow: 0 6rpx 6rpx 0 rgba(0, 0, 0, 0.1);
	}
 
	.packer-bottom-box {
		transform: translate(-50%, 0);
		width: 600rpx;
		height: 360rpx;
		border-bottom-left-radius: 30rpx;
		border-bottom-right-radius: 30rpx;
		overflow: hidden;
		position: absolute;
		bottom: calc(50% - 440rpx);
		left: 50%;
	}
 
	.anim-ease-in {
		animation-duration: 0.5s;
		animation-timing-function: ease-in;
		animation-fill-mode: forwards;
	}
	
	.anim-out-top {
		animation-name: slideOutTop;
	}
	
	.anim-out-bottom {
		animation-name: slideOutBottom;
	}
	
	.anim-fade-out {
		animation-name: fadeOut;
	}
 
	@keyframes fadeOut {
		from {
			opacity: 1;
		}
 
		to {
			opacity: 0;
		}
	}
 
	@keyframes slideOutTop {
		from {
			transform: translateY(-160rpx);
		}
 
		to {
			transform: translateY(-200%);
		}
	}
 
	@keyframes slideOutBottom {
		from {
			transform: translate(-50%, 0);
		}
 
		to {
			transform: translate(-50%, 200%);
		}
	}
 
	.arc-bottom-edge {
		position: relative;
	}
 
	.arc-bottom-edge::after {
		width: 120%;
		height: 200rpx;
		position: absolute;
		left: -10%;
		top: -100rpx;
		z-index: 8;
		content: '';
		border-radius: 50%;
		box-shadow: 0 60rpx 0 0 rgb(242, 85, 66);
	}
 
	.packer-bottom-bg {
		background-color: rgb(242, 85, 66);
		height: 280rpx;
		margin-top: 100rpx;
	}
 
	.packer-btn {
		border-radius: 50%;
		width: 200rpx;
		height: 200rpx;
		line-height: 200rpx;
		font-size: 80rpx;
		text-align: center;
		color: #333;
		background-color: rgb(235, 205, 153);
		box-shadow: 0 0 6rpx 0 rgba(0, 0, 0, 0.1);
	}
	.packer-btn-pos {
		transform: translateX(-50%);
		position: absolute;
		left: 50%;
		z-index: 10;
		bottom: -200rpx;
	}
	.packer-btn-front {
		position: absolute;
		top: 0;
		transform: translateZ(4px);
	}
	.packer-btn-middle {
		position: absolute;
		top: 0;
		border-radius: 50%;
		width: 200rpx;
		height: 200rpx;
		background-color: rgb(235, 180, 120);
	}
	.anim-rotate {
		margin-left: -100rpx;
		transform-style: preserve-3d;
		animation: rotate 1s linear infinite;
	}
	@keyframes rotate{
		0%{
			transform: rotateY(0deg);
		}
		100%{
			transform: rotateY(360deg);
		}
	}
</style>

总结 

到此这篇关于uni-app微信小程序之红包雨活动的文章就介绍到这了,更多相关uni-app小程序红包雨活动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解JavaScript对象类型

    详解JavaScript对象类型

    这篇文章主要为大家详细介绍了JavaScript对象类型,分析了JavaScript六种数据类型,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • js遍历详解(forEach, map, for, for...in, for...of)

    js遍历详解(forEach, map, for, for...in, for...of)

    在本篇文章里小编给大家整理的是关于js中的各种遍历(forEach, map, for, for...in, for...of)相关知识点用法总结,需要的朋友们参考下。
    2019-08-08
  • Bootstrap 网格系统布局详解

    Bootstrap 网格系统布局详解

    在平面设计中,网格是一种由一系列用于组织内容的相交的直线(垂直的、水平的)组成的结构(通常是二维的)。这篇文章主要介绍了Bootstrap 网格系统布局,需要的朋友可以参考下
    2017-03-03
  • JavaScript通过RegExp实现客户端验证处理程序

    JavaScript通过RegExp实现客户端验证处理程序

    通过RegExp实现客户端验:让文本框只允许输入数字、文本框只允许输入中文、邮箱输入格式的判断等等,具体实现如下,感兴趣的朋友可以参考下哈
    2013-05-05
  • JavaScript实现获取本机IP地址

    JavaScript实现获取本机IP地址

    这篇文章主要介绍了JavaScript实现获取本机IP地址方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • webpack之基础打包优化的实现

    webpack之基础打包优化的实现

    本文主要介绍了webpack之基础打包优化的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下<BR>
    2022-02-02
  • 小程序外卖订单界面的示例代码

    小程序外卖订单界面的示例代码

    这篇文章主要介绍了小程序外卖订单界面的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • 在window.setTimeout方法中传送对象

    在window.setTimeout方法中传送对象

    setTimeout方法是js中的延时方法,很多js的bug,只需要使用该方法延时一下,就会自动解决了,简直就是万能药方,也是我比较喜欢使用的最后手段。
    2006-12-12
  • firefox事件处理之自动查找event的函数(用于onclick=foo())

    firefox事件处理之自动查找event的函数(用于onclick=foo())

    在ie中,事件对象是作为一个全局变量来保存和维护的。 所有的浏览器事件,不管是用户触发的,还是其他事件, 都会更新window.event 对象。
    2010-08-08
  • javascript封装 Cookie 应用接口

    javascript封装 Cookie 应用接口

    本文通过几个简单的示例向大家展示了javascript封装cookie的注意事项及操作方法,非常的简单实用,最后附上一则具体实例,有需要的小火把可以参考下。
    2015-08-08

最新评论