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小程序红包雨活动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 微信小程序自定义组件实现单选功能

    微信小程序自定义组件实现单选功能

    这篇文章主要为大家详细介绍了微信小程序自定义组件实现单选功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • Javascript客户端将指定区域导出到Word、Excel的代码

    Javascript客户端将指定区域导出到Word、Excel的代码

    Javascript 浏览器客户端导出指定区域到Word、Excel,非常不错的应用实例
    2008-10-10
  • 微信小程序自定义navigationBar顶部导航栏适配所有机型(附完整案例)

    微信小程序自定义navigationBar顶部导航栏适配所有机型(附完整案例)

    这篇文章主要介绍了微信小程序自定义navigationBar顶部导航栏适配所有机型(附完整案例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • JavaScript fetch接口案例解析

    JavaScript fetch接口案例解析

    本文通过案例给大家介绍了JavaScript fetch接口,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • Vuex模块化应用实践示例

    Vuex模块化应用实践示例

    这篇文章主要介绍了Vuex模块化应用实践示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • 微信小程序实现购物车功能

    微信小程序实现购物车功能

    这篇文章主要为大家详细介绍了微信小程序实现购物车功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • js加载读取内容及显示与隐藏div示例

    js加载读取内容及显示与隐藏div示例

    这篇文章主要介绍了js加载读取内容及显示与隐藏div的方法,需要的朋友可以参考下
    2014-02-02
  • 让网页根据不同IE版本显示不同的内容

    让网页根据不同IE版本显示不同的内容

    在上一篇blog 《IE8里判断当前网页显示模式》里面提到IE有不同的显示模式以及如何用Javascript 来动态判定。 Web开发者可以根据不同显示模式导入不同的内容。
    2009-02-02
  • 基于JS制作一个简单的网页版地图

    基于JS制作一个简单的网页版地图

    本文将利用JS实现一个简单的网页版地图,这个简单的网页版地图能根据城市名进行位置查询。文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-05-05
  • TypeScript中import type与import的区别详析

    TypeScript中import type与import的区别详析

    ES6引入了模块化,其设计思想是在编译时就能确定模块的依赖关系,以及输入和输出的变量,下面这篇文章主要给大家介绍了关于TypeScript中import type与import区别的相关资料,需要的朋友可以参考下
    2022-07-07

最新评论