微信小程序制作扭蛋机代码实例

公司要制作活动小程序,其中有一个扭蛋机的效果实现抽奖的功能。在网上找了好久竟没有找到(不知道是不是我找代码的方式有问题)。最后还是自己做一个吧- _ - ,效果如下:

微信小程序制作扭蛋机代码实例

1.wxml

当然我这里没有用wx:for遍历

<!-- 扭蛋机 --> <view> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_nd.png" mode="widthFix"></image> <image bindtap="eggPlay" src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_eggplay.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg1.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg2.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg3.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg4.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg5.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg6.png" mode="widthFix"></image> <image src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg7.png" mode="widthFix"></image> <image hidden="{{qiu}}" animation="{{ani}}" src="https://www.jb51.net/{{imgUrl}}small_program/game/game_luck_draw_egg7.png" mode="widthFix" ></image> </view>

2.wxss

这一步比较麻烦,需要调试扭蛋的位置和动画路径

/* 扭蛋机 */ .egg{ width: 100%; position: absolute; z-index: 3; top: 260rpx; } .egg .egg_ji{ width: 70%; margin-left: 15%; z-index: 3; } .egg .play{ width: 80rpx; position: absolute; z-index: 4; top: 405rpx; left: 275rpx; } .egg .ball{ width: 75rpx; position: absolute; z-index: 2; } .egg .ball_1{ top: 290rpx; left: 300rpx; } .egg .ball_2{ top: 295rpx; left: 360rpx; } .egg .ball_3{ top: 260rpx; left: 240rpx; } .egg .ball_4{ top: 260rpx; left: 420rpx; } .egg .ball_5{ top: 230rpx; left: 280rpx; } .egg .ball_6{ top: 230rpx; left: 340rpx; } .egg .ball_7{ top: 220rpx; left: 390rpx; } .egg .ball_end{ top: 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接口获取奖品就可以了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9f2655eeabe739adc747aca1edbc5b13.html