uni-app 九宫格抽奖活动

一、效果图

二、代码 

<template>
	<view class="container">
		<view class="container_circle" v-for="(v,i) in  circleList" :key="i"
		:style="{'top':v.topCircle+'rpx','left':v.leftCircle+'rpx','background-color':(i%2 ==0)?oneCircleColor:twoCircleColor}"></view>
		<view class="container_content">
			<view class="content_out" v-for="(v,i) in prizeList" :key="i"
			:style="{'top':v.topAward+'rpx','left':v.leftAward+'rpx',
			'background':  (i==indexSelect)?prizeCheckColor:prizeDefaultColor,
			'background-size':' 400%',
			'animation': 'animatebox 4s linear infinite'
			}">
			<image class="award_image" :src="v.imgList"></image>
			</view>
			<view class="content_btn" @tap="handleStart" :style="{'background-color':isRunning?'#55ffff':'#f7f7f7'}">开始</view>
		</view>
		<view class="container_num">剩余抽奖次数{{luckDrawNum}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				circleList:[], // 圆圈数组
				prizeList:[], // 奖品数组
				oneCircleColor:'#29f5ff', // 圆圈颜色1
				twoCircleColor:'#aaaaff', // 圆圈颜色2
				prizeDefaultColor:'#FFF', // 奖品的默认颜色
				prizeCheckColor:'linear-gradient(72.5deg, #aa0000, #ff0000, #ff00ff, #550000, #ffaaff, #aa0000, #ff0000)', // 奖品的选中颜色
				indexSelect: 0, //被选中的奖品index
				isRunning: false, //是否正在抽奖
				//奖品图片数组
				imgList:[
					'https://cdn.cnbj1.fds.api.mi-img.com/nr-pub/202209261921_a1b840c267bd26bcf4dc654d52f259e5.png?w=800&h=800',
					'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/0bcd64f412dfb5e15695fa96d21ecb23.png?w=800&h=800',
					'https://cdn.cnbj1.fds.api.mi-img.com/nr-pub/202208111030_e3554c41e0484da99b16bb9e02142e68.png?w=800&h=800',
					'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1653393310.29013051.png',
					'https://cdn.cnbj1.fds.api.mi-img.com/nr-pub/202209261921_a1b840c267bd26bcf4dc654d52f259e5.png?w=800&h=800',
					'https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/9E253411E26FD16C7215D7E74321FA45.png',
					'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/cc0be95d45d0063b0aa8bb541be22c77.png?w=800&h=800',
					'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/0c4b5e30d14ca8b51dc6fa6917295ff5.png?w=800&h=800'
				],
				luckDrawNum:2  ,//剩余抽奖次数
				repeat:true
			};
		},
		onLoad() {
			//圆点设置
			let topCircle = 7.5;
			let leftCircle = 7.5;
			let circleList = [];
			for(let i = 0; i < 24; i++) {
				if(i == 0) {
					topCircle = 15;
					leftCircle = 15;
				}else if (i < 6) {
					topCircle = 7.5;
					leftCircle += 102.5;
				}else if (i == 6) {
					topCircle = 15;
					leftCircle = 620;
				}else if (i < 12) {
					topCircle += 94;
					leftCircle = 620;
				}else if (i == 12) {
					topCircle = 565;
					leftCircle = 620;
				}else if (i < 18) {
					topCircle = 570;
					leftCircle-= 102.5; 
				}else if (i == 18) {
					topCircle = 565;
					leftCircle = 15;
				}else if (i < 24) {
					topCircle -= 94;
					leftCircle = 7.5;
				}else {
					return;
				}
				circleList.push({ topCircle, leftCircle });
			}
			this.circleList = circleList;
			//圆点闪烁
			setInterval(()=> {
			if (this.oneCircleColor == '#29f5ff') {
					this.oneCircleColor = '#aaaaff';
					this.twoCircleColor = '#29f5ff';
				} else {
					this.oneCircleColor = '#29f5ff';
					this.twoCircleColor = '#aaaaff';
				}
			},500)
			let prizeList = [];
			//间距,怎么顺眼怎么设置吧.
			let topAward = 25;
			let leftAward = 25;
			for (let j = 0; j < 8; j++) {
				if (j == 0) {
					topAward = 25;
					leftAward = 25;
				} else if (j < 3) {
					topAward = topAward;
					//166.6666是宽.15是间距.下同
					leftAward = leftAward + 166.6666 + 15;
				} else if (j < 5) {
					leftAward = leftAward;
					//150是高,15是间距,下同
					topAward = topAward + 150 + 15;
				} else if (j < 7) {
					leftAward = leftAward - 166.6666 - 15;
					topAward = topAward;
				} else if (j < 8) {
					leftAward = leftAward;
					topAward = topAward - 150 - 15;
				}
				let imgList = this.imgList[j];
				prizeList.push({ topAward, leftAward,imgList});
			}
			this.prizeList = prizeList;
		},
		methods:{
			// 点击开始
			handleStart(){
				if (this.isRunning) return;
				if (this.luckDrawNum != 0) {
					this.luckDrawNum--
				} else{
					uni.showModal({
						title: '提示',
						content: '分享获取抽奖次数',
						showCancel: false, 
						success: (res)=> {
							if (res.confirm) {
							}
						}
					});
					this.indexSelect = 0
					return
				}
				this.isRunning = true;
				let indexSelect = 0;
				let i = 0;
				let timer = setInterval(()=> {
					indexSelect++;
					i += 30;
					let randomNum = 1000+Math.ceil(Math.random()*1000)//控制奖品
					if (i > randomNum) {
						//去除循环
						clearInterval(timer);
						//获奖提示
						uni.showModal({
							title: '恭喜您',
							content: '获得了第' + (this.indexSelect + 1) + '个奖品',
							showCancel: false, 
							success: (res)=> {
								if (res.confirm) {
									this.isRunning = false;
								}
							}
						});
					}
					indexSelect = indexSelect % 8;
					this.indexSelect = indexSelect;
				},200 + i)
			}
		}
	}
</script>

<style lang="scss">
.container {
	position: relative;
	height: 600rpx;
	width: 650rpx;
	margin: 100rpx auto;
	border-radius: 40rpx;
	background: linear-gradient(72.5deg, #e9f78c, #ffffb9, #cc55e5, #ef9bcb, #e5b8d8, #ffb140, #f7ed54);
	background-size: 400%;
	animation: animatebox 4s linear infinite;
	@keyframes animatebox {
		0% {
			background-position: 0%;
		}
	
		50% {
			background-position: 100%;
		}
	
		100% {
			background-position: 0%;
		}
	}
	
	
	.container_circle {
		position: absolute;
		display: block;
		border-radius: 50%;
		height: 20rpx;
		width: 20rpx;
	}
	.container_content{
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		width: 580rpx;
		height: 530rpx;
		background-color: #b375ff;
		
		
		border-radius: 40rpx;
		margin: auto;
		
		background: linear-gradient(72.5deg, #ef499c, #cc55e5, #ffb140, #f7ed54, #ffb140, #cc55e5, #ef499c);
		background-size: 400%;
		animation: animate1 4s linear infinite;
		@keyframes animate1 {
			0% {
				background-position: 0%;
			}
		
			50% {
				background-position: 100%;
			}
		
			100% {
				background-position: 0%;
			}
		}
		
		
		.content_out{
			position: absolute;
			height: 150rpx;
			width: 166.6666rpx;
			background-color: #f5f0fc;
			border-radius: 15rpx;
			box-shadow: 0 5px 0 #d87fde;
			.award_image{
				position: absolute;
				margin: auto;
				top: 0;
				left: 0;
				bottom: 0;
				right: 0;
				height: 140rpx;
				width: 130rpx;
			}
		}
		.content_btn{
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%,-50%);
			border-radius: 15rpx;
			height: 150rpx;
			width: 166.6666rpx;
			background-color: #ffe400;
			box-shadow: 0 5rpx 0 #e7930a;
			color: #b375ff;
			text-align: center;
			font-size: 55rpx;
			font-weight: bolder;
			line-height: 150rpx;
			background: linear-gradient(72.5deg, #f7a0ec, #ffb140, #e58da0, #ef7ecb, #ab9ae5, #ffec8a, #f6f7a6);
			background-size: 400%;
			animation: animate 4s linear infinite;
			@keyframes animate {
				0% {
					background-position: 0%;
				}
			
				50% {
					background-position: 100%;
				}
			
				100% {
					background-position: 0%;
				}
			}
			
		}
	}
	.container_num{
		position: absolute;
		top: -60rpx;
		left: 50%;
		transform: translateX(-50%);
		background-image: -webkit-linear-gradient(left, #083a96, #e63609 25%, #083a96 50%, #e63609 75%, #083a96);
		-webkit-text-fill-color: transparent;
		background-clip: text;
		background-size: 200% 100%;
		animation: masked-animation 4s infinite linear;
		@keyframes masked-animation {
			 0%{ background-position: 0 0;}
			 100% { background-position: -100% 0;}
		}
	}
}
</style>

 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uni-app 是一种基于 Vue.js 的跨平台应用开发框架,它可以帮助开发者快速构建同时运行在 iOS、Android、Web 等平台的应用。在 uni-app 中实现转盘抽奖功能,通常会使用组件和一些交互效果库来模拟。以下是一个简单的步骤概述: 1. **创建组件**: 首先,你需要在项目中创建一个新的组件,比如名为 `SpinWheel` 的.vue文件,用于显示转盘并处理旋转事件。 ```html <template> <view class="spin-wheel"> <image :src="wheelImage" /> <indicator-dots :dots="dots"></indicator-dots> </view> </template> <script> import IndicatorDots from '@/components/IndicatorDots.vue'; export default { components: { IndicatorDots }, data() { return { wheelImage: 'path/to/your/spin_wheel_image', dots: [], // 数字点数组,对应奖品位置 }; }, methods: { spin() { // 在这里编写旋转转盘的逻辑,可能包含随机选择一个数字点 } }, }; </script> ``` 2. **动画效果**: 使用 Vue 的动画库(如 vant-weapp 或者自己编写 CSS 动画)来模拟转盘的旋转和停止动画。 3. **旋转逻辑**: 当用户触发抽奖时,调用 `spin` 方法,这个方法内部可能需要生成一个随机数,然后根据这个随机数选择对应的奖品点。 4. **事件监听**: 可能还需要添加点击或滑动事件监听器,以便在指针停止时触发结果展示。 5. **结果展示**: 结果可以显示在组件内部或者其他页面,比如弹出层或者通知。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值