uni-app 微信小程序之红包雨活动

文章目录

  • [1. 页面效果](#1. 页面效果)
  • [2. 页面样式代码](#2. 页面样式代码)

1. 页面效果

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

  1. 每0.5s掉落一个红包
  2. 控制4s后自动移除红包
  3. 点击红包消除红包(或者自行+1,或者弹窗需求)

2. 页面样式代码

html 复制代码
<!-- 红包雨活动 -->
<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>
相关推荐
用户9714171814276 小时前
UniApp + Vue3 持久化登录(清除后台仍保持登陆状态)
uni-app
小白学鸿蒙7 小时前
新手记录使用uniapp-x开发鸿蒙应用
华为·uni-app·harmonyos
Y_3_77 小时前
微信小程序动态二维码外部实时展示系统
微信小程序·小程序·notepad++
初遇你时动了情21 小时前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app
gys989521 小时前
uniapp使用sqlite模块
数据库·sqlite·uni-app
abigale0321 小时前
开发实战 - ego商城 -补充:使用uniapp扩展组件
uni-app·uni-ui
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
爱喝水的小周1 天前
《UniApp 页面导航跳转全解笔记》
前端·uni-app
CV大师杨某1 天前
如何在uni-app中禁用iOS橡皮筋效果?
ios·uni-app
More more1 天前
uniapp实时查看在线监控,JessibucaMobile实现横屏播放
前端·javascript·uni-app·jessibucamobile