微信小程序九宫格抽奖

做了一个微信小程序九宫格抽奖demo

截图:

代码如下:

Luckydraw.js

// pages/Luckydraw/Luckydraw.js
let timer;
let cjIn = false;
let cjChange = 0; //抽奖过程KEY
Page({

  /**
   * 页面的初始数据
   */
  data: {
	prizeList:[
		{id:'001',index:1,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称111'},
		{id:'002',index:2,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称222'},
		{id:'003',index:3,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称333'},
		{id:'004',index:4,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称444'},
		{id:'005',index:5,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称555'},
		{id:'006',index:6,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称666'},
		{id:'007',index:7,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称777'},
		{id:'008',index:8,imgsrc:'https://www.baidu.com/img/bd_logo1.png',prizeName:'奖品名称88888888888888'},
	],
	
	cjChange:null, //抽奖过程KEY
	prizeResult:null, //抽奖结果KEY
	prizeName:null, //抽奖结果KEY对应的奖品名称
	
	showAgain:false , //是否抽奖后显示再抽一次按钮
	
  },
  
  
  
  
  
  
  //抽奖操作
  cj(){
	  if(cjIn){
		  return;
	  }else{
		  cjIn = true;
	  }
	  
	  let This = this;
	  
	  clearInterval(timer);
	  timer = setInterval(This.changePrize,80);
	  
	  
	  
	  // test start
	  let res = {
		  stutus:1,
		  prizeResult:8,
		  prizeName:'奖品名称88888888888888',
		  
	  }
	  if(res.stutus == 1){
		  setTimeout(function(){
			  clearInterval(timer);
			  timer = setInterval(This.changePrize,160);
			  setTimeout(function(){
				  clearInterval(timer);
				  timer = setInterval(This.changePrize,300);
				  
				  setTimeout(function(){
					  This.setData({
						  prizeResult:res.prizeResult,
						  prizeName:res.prizeName,
					  });
				  },1000)
			  },1000)
		  },2000)
	  }
	  // test end
	  
	  
	  
  },
  //抽奖过程奖品切换
  changePrize(){
	  cjChange ++;
	  cjChange = cjChange > 8 ? 1 : cjChange;
	  
	  let This = this;
	  This.setData({
		  cjChange:cjChange
	  });
	  
	  if(This.data.prizeResult == cjChange){
		  clearInterval(timer);
		  This.setData({
			  showAgain:true
		  });
		  console.log('获得奖品:' + This.data.prizeName)
	  }
  },
  //点击再抽一次按钮
  againBtn(){
	cjIn = false;
	cjChange = 0; //抽奖过程KEY
	
	let This = this;
	This.setData({
		cjChange:null, //抽奖过程KEY
		prizeResult:null, //抽奖结果KEY
		prizeName:null, //抽奖结果KEY对应的奖品名称
		showAgain:false , //是否抽奖后显示再抽一次按钮
	});
  },
  
  
  
  
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

Luckydraw.wxss

/* pages/Luckydraw/Luckydraw.wxss */
page{ box-sizing: border-box; padding-top: 25rpx; background-color: #FF6600;}

.box{ width: 700rpx; height: 700rpx; margin: 0 auto; box-sizing: border-box; position: relative;
	-webkit-box-shadow: 8px 8px 0 rgba(0,0,0,.2);
			box-shadow: 8px 8px 0 rgba(0,0,0,.2);
}
.list{ width: 220rpx; height: 220rpx; position: absolute; background-color: #fff;
	display: flex; flex-direction: column; flex-wrap: wrap; justify-content: center; align-content: center; align-items: center;
}
/* 抽奖过程中选定样式 */
.list.change_in{ color: #fff; background-color: #00BFFF;}
.list_t{ width: 90%; height: 160rpx;}
.list_t image{ width: 100%; height: 100%;}
.list_b{ width: 90%; height: 40rpx; line-height: 40rpx; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}
/* 根据TOP */
.list:nth-child(1){ left: 50%; top: 50%; color: #fff; background-color: #1CBBB4;
	display: flex; justify-content: center; align-content: center; align-items: center;
	-webkit-transform: translate(-50%,-50%);
			transform: translate(-50%,-50%);
}
.list.grey:nth-child(1){ background-color: #5F9EA0;}
.list:nth-child(2) , .list:nth-child(3) , .list:nth-child(4){ top: 0;}
.list:nth-child(6) , .list:nth-child(7) , .list:nth-child(8){ bottom: 0;}
.list:nth-child(5) , .list:nth-child(9){ top: 50%;
	-webkit-transform: translateY(-50%);
			transform: translateY(-50%);
}
/* 根据LEFT */
.list:nth-child(2) , .list:nth-child(8) , .list:nth-child(9){ left: 0;}
.list:nth-child(3) , .list:nth-child(7){ left: 50%;
	-webkit-transform: translateX(-50%);
			transform: translateX(-50%);
}
.list:nth-child(4) , .list:nth-child(5) , .list:nth-child(6){ right: 0;}

Luckydraw.wxml

<!--pages/Luckydraw/Luckydraw.wxml-->
<view class="box">
	<view bindtap="cj" class="list {{cjChange > 0 ? 'grey' : ''}}">抽奖</view>
	<block wx:for="{{prizeList}}">
		<view class="list {{cjChange == item.index ? 'change_in' : ''}}">
			<view class="list_t"><image src="{{item.imgsrc}}"></image></view>
			<view class="list_b">{{item.prizeName}}</view>
		</view>
	</block>
</view>

<button wx:if="{{showAgain}}" style="margin: 50rpx;" bindtap="againBtn">再抽一次</button>


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值