微信小程序九宫格抽奖
一个很简陋的抽奖demo,记录在这里。
效果图:
结构:
<!--index.wxml--> <view class="container-out"> <view class='circle' wx:for="{{ circleList }}" wx:key="{{ index }}" style="top:{{ item.circleTop }}rpx;left:{{ item.circleLeft }}rpx;background-color:{{ (index%2==0)?colorEven:colorOdd }}" ></view> <view class='container-in'> <view class='award' wx:for="{{ awardList }}" wx:key="{{ index }}" style="top:{{ item.awardTop }}rpx;left:{{ item.awardLeft }}rpx;background-color: {{(index==selectedIdx)?'#ffff00':'#ffffff'}};" > <image class='award-img' src="{{ item.awardUrl }}"></image> </view> <view class="start-btn" bindtap="startHandle" >开始</view> </view> </view>
样式:
/**index.wxss**/ page { height: 100%; background-color: #f0f0f0; } .container-out { position: relative; width: 650rpx; height: 650rpx; margin: 150rpx auto; border-radius: 40rpx; background-color: #ff6666; } .circle { position: absolute; display: block; border-radius: 50%; height: 20rpx; width: 20rpx; } .container-in { position: absolute; width: 580rpx; height: 580rpx; top: 0; right: 0; left: 0; bottom: 0; margin: auto; background-color: #ff0000; border-radius: 40rpx; } .award { position: absolute; height: 150rpx; width: 150rpx; background-color: #fff; border-radius: 15rpx; } .award image { display: block; width: 120rpx; height: 120rpx; margin: 10rpx auto; } .start-btn { position: absolute; width: 150rpx; height: 150rpx; line-height: 150rpx; text-align: center; font-weight: 600; top: 215rpx; left: 215rpx; border-radius: 15rpx; background-color: #ffff00; }
逻辑:
const app = getApp(); Page({ /** * 页面的初始数据 */ data: { // 圆点列表 circleList: { 0: { circleTop: '20', circleLeft: '20'}, 1: { circleTop: '10', circleLeft: '160'}, 2: { circleTop: '10', circleLeft: '310'}, 3: { circleTop: '10', circleLeft: '460'}, 4: { circleTop: '20', circleLeft: '610'}, 5: { circleTop: '160', circleLeft: '620'}, 6: { circleTop: '310', circleLeft: '620'}, 7: { circleTop: '460', circleLeft: '620'}, 8: { circleTop: '610', circleLeft: '610'}, 9: { circleTop: '620', circleLeft: '460'}, 10: { circleTop: '620', circleLeft: '310'}, 11: { circleTop: '620', circleLeft: '160'}, 12: { circleTop: '610', circleLeft: '20'}, 13: { circleTop: '460', circleLeft: '10'}, 14: { circleTop: '310', circleLeft: '10'}, 15: { circleTop: '160', circleLeft: '10'} }, colorOdd: " #CD00CD",// 奇数点颜色 colorEven: "#ffff00",// 偶数点颜色 awardList: { 0: { awardTop: '40', awardLeft: '40', awardUrl: '/images/1.jpg'}, 1: { awardTop: '40', awardLeft: '215', awardUrl: '/images/2.jpg'}, 2: { awardTop: '40', awardLeft: '390', awardUrl: '/images/3.jpg'}, 3: { awardTop: '215', awardLeft: '390', awardUrl: '/images/4.jpg'}, 4: { awardTop: '390', awardLeft: '390', awardUrl: '/images/5.jpg'}, 5: { awardTop: '390', awardLeft: '215', awardUrl: '/images/6.jpg'}, 6: { awardTop: '390', awardLeft: '40', awardUrl: '/images/7.jpg'}, 7: { awardTop: '215', awardLeft: '40', awardUrl: '/images/8.jpg'} }, isContinue: false, // 正在抽奖 selectedIdx: 0 // 被选中的奖品 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 让圆点闪烁 setInterval(() => { if (this.data.colorOdd == '#CD00CD') { this.setData({ colorOdd: '#ffff00', colorEven: '#CD00CD' }) } else { this.setData({ colorOdd: '#CD00CD', colorEven: '#ffff00' }) } }, 500); }, startHandle () { if (this.data.isContinue) { return; } this.setData({ isContinue: true }) let that = this; let selectedIdx = 0 let i = 0; const timer = setInterval( () => { selectedIdx ++; i += Math.random() * 100; if (i > 1000) { clearInterval(timer); wx.showModal({ title: '中奖啦!!!', content: '恭喜你获得兰博基尼5元代金券', showCancel: false, success: function (res) { if (res.confirm) { that.setData({ isContinue: false }) } } }) } selectedIdx = selectedIdx % 8; that.setData({ selectedIdx }) }, 200) } })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)