微信小程序制作扭蛋机代码实例
网络编程 2021-07-04 15:01www.168986.cn编程入门
这篇文章主要介绍了微信小程序制作扭蛋机代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
公司要制作活动小程序,其中有一个扭蛋机的效果实现抽奖的功能。在网上找了好久竟没有找到(不知道是不是我找代码的方式有问题)。还是自己做一个吧- _ - ,效果如下:
1.wxml
我这里没有用wx:for遍历
<!-- 扭蛋机 --> <view class="egg"> <image class="egg_ji" src="{{imgUrl}}small_program/game/game_luck_draw_nd.png" mode="widthFix"></image> <image class="play {{start?'go':''}}" bindtap="eggPlay" src="{{imgUrl}}small_program/game/game_luck_draw_eggplay.png" mode="widthFix"></image> <image class="ball ball_1 {{start?'weiyi_1':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg1.png" mode="widthFix"></image> <image class="ball ball_2 {{start?'weiyi_2':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg2.png" mode="widthFix"></image> <image class="ball ball_3 {{start?'weiyi_3':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg3.png" mode="widthFix"></image> <image class="ball ball_4 {{start?'weiyi_4':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg4.png" mode="widthFix"></image> <image class="ball ball_5 {{start?'weiyi_5':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg5.png" mode="widthFix"></image> <image class="ball ball_6 {{start?'weiyi_6':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg6.png" mode="widthFix"></image> <image class="ball ball_7 {{start?'weiyi_7':''}}" src="{{imgUrl}}small_program/game/game_luck_draw_egg7.png" mode="widthFix"></image> <image hidden="{{qiu}}" animation="{{ani}}" class="ball ball_end" src="{{imgUrl}}small_program/game/game_luck_draw_egg7.png" mode="widthFix" ></image> </view>
2.wxss
这一步比较麻烦,需要调试扭蛋的位置和动画路径
/ 扭蛋机 / .egg{ width: 100%; position: absolute; z-index: 3; : 260rpx; } .egg .egg_ji{ width: 70%; margin-left: 15%; z-index: 3; } .egg .play{ width: 80rpx; position: absolute; z-index: 4; : 405rpx; left: 275rpx; } .egg .ball{ width: 75rpx; position: absolute; z-index: 2; } .egg .ball_1{ : 290rpx; left: 300rpx; } .egg .ball_2{ : 295rpx; left: 360rpx; } .egg .ball_3{ : 260rpx; left: 240rpx; } .egg .ball_4{ : 260rpx; left: 420rpx; } .egg .ball_5{ : 230rpx; left: 280rpx; } .egg .ball_6{ : 230rpx; left: 340rpx; } .egg .ball_7{ : 220rpx; left: 390rpx; } .egg .ball_end{ : 410rpx; left: 390rpx; } .weiyi_1 { animation: around1 1.5s linear infinite; } .weiyi_2 { animation: around2 1.5s linear infinite; } .weiyi_3 { animation: around3 1.5s linear infinite; } .weiyi_4 { animation: around4 1.5s linear infinite; } .weiyi_5 { animation: around5 1.5s linear infinite; } .weiyi_6 { animation: around6 1.5s linear infinite; } .weiyi_7 { animation: around7 1.5s linear infinite; } .go{ animation: around 0.3s linear 1; } / 位移 / @keyframes around{ 100% { -webkit-transform: rotate(-180deg) } } @keyframes around1 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(-100rpx, -200rpx) } 40% { -webkit-transform: translate(40rpx, -280rpx) } 60% { -webkit-transform: translate(150rpx, -200rpx) } 80% { -webkit-transform: translate(150rpx, -50rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around2 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(100rpx, -200rpx) } 40% { -webkit-transform: translate(-20rpx, -280rpx) } 60% { -webkit-transform: translate(-150rpx, -200rpx) } 80% { -webkit-transform: translate(-150rpx, -50rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around3 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(180rpx, 10rpx) } 40% { -webkit-transform: translate(240rpx, -110rpx) } 60% { -webkit-transform: translate(100rpx, -240rpx) } 80% { -webkit-transform: translate(-50rpx, -130rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around4 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(-180rpx, 10rpx) } 40% { -webkit-transform: translate(-240rpx, -110rpx) } 60% { -webkit-transform: translate(-100rpx, -240rpx) } 80% { -webkit-transform: translate(50rpx, -130rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around5 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(40rpx, 70rpx) } 40% { -webkit-transform: translate(50rpx, -210rpx) } 60% { -webkit-transform: translate(-80rpx, -100rpx) } 80% { -webkit-transform: translate(190rpx, -50rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around6 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(-150rpx, -50rpx) } 40% { -webkit-transform: translate(130rpx, -140rpx) } 60% { -webkit-transform: translate(-110rpx, -180rpx) } 80% { -webkit-transform: translate(-130rpx, -20rpx) } 100% { -webkit-transform: translate(0, 0) } } @keyframes around7 { 0% { -webkit-transform: translate(0rpx, 0rpx) } 20% { -webkit-transform: translate(80rpx, -50rpx) } 40% { -webkit-transform: translate(-180rpx, -100rpx) } 60% { -webkit-transform: translate(50rpx, -150rpx) } 80% { -webkit-transform: translate(-180rpx, -20rpx) } 100% { -webkit-transform: translate(0, 0) } }
3.js
这一步要比css要是要简单的多,点击使动画动起来,调用api接口获取奖品就可以了
Page({ / 页面的初始数据 / data: { imgUrl: app.data.imgUrl, start : false, qiu: true, }, / 点击扭蛋机 / eggPlay(){ let _this = this; _this.setData({ start: true, }) setTimeout(() => { _this.setData({ start: false, qiu: false, }) //球落下动画 var animation = wx.createAnimation({ duration: 1500, timingFunction: 'ease', }); animation.opacity(1).step() this.setData({ ani: animation.export() }) }, 3000); _this.setData({ qiu: true }) //将动画返回到开始位置 var animation = wx.createAnimation({ duration: 1500, timingFunction: 'ease', }); animation.opacity(0).step() this.setData({ ani: animation.export() }) },
这个动画有个小的bug,就是连续点击按钮动画时间会变快,如果有更好的方法可以留言交流。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指