微信小程序制作扭蛋機(jī)代碼實(shí)例
公司要制作活動(dòng)小程序,其中有一個(gè)扭蛋機(jī)的效果實(shí)現(xiàn)抽獎(jiǎng)的功能。在網(wǎng)上找了好久竟沒有找到(不知道是不是我找代碼的方式有問題)。最后還是自己做一個(gè)吧- _ - ,效果如下:

1.wxml
當(dāng)然我這里沒有用wx:for遍歷
<!-- 扭蛋機(jī) -->
<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
這一步比較麻煩,需要調(diào)試扭蛋的位置和動(dòng)畫路徑
/* 扭蛋機(jī) */
.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要是要簡(jiǎn)單的多,點(diǎn)擊使動(dòng)畫動(dòng)起來,調(diào)用api接口獲取獎(jiǎng)品就可以了
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
imgUrl: app.data.imgUrl,
start : false,
qiu: true,
},
/**
* 點(diǎn)擊扭蛋機(jī)
*/
eggPlay(){
let _this = this;
_this.setData({
start: true,
})
setTimeout(() => {
_this.setData({
start: false,
qiu: false,
})
//球落下動(dòng)畫
var animation = wx.createAnimation({
duration: 1500,
timingFunction: 'ease',
});
animation.opacity(1).step()
this.setData({
ani: animation.export()
})
}, 3000);
_this.setData({
qiu: true
})
//將動(dòng)畫返回到開始位置
var animation = wx.createAnimation({
duration: 1500,
timingFunction: 'ease',
});
animation.opacity(0).step()
this.setData({
ani: animation.export()
})
},
這個(gè)動(dòng)畫有個(gè)小的bug,就是連續(xù)點(diǎn)擊按鈕動(dòng)畫時(shí)間會(huì)變快,如果有更好的方法可以留言交流。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
chrome瀏覽器當(dāng)表單自動(dòng)填充時(shí)如何去除瀏覽器自動(dòng)添加的默認(rèn)樣式
很多朋友都遇到這個(gè)問題:當(dāng)使用chrome瀏覽器表單自動(dòng)填充時(shí)都會(huì)自動(dòng)添加默認(rèn)的樣式,該如何去除默認(rèn)樣式呢?看看小編是怎么去除的,需要的朋友一起學(xué)習(xí)吧2015-10-10
javascript實(shí)現(xiàn)文字無縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)文字無縫滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Js 獲取當(dāng)前函數(shù)參數(shù)對(duì)象的實(shí)現(xiàn)代碼
這篇文章主要介紹了Js 獲取當(dāng)前函數(shù)參數(shù)對(duì)象的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-06-06
JavaScript實(shí)現(xiàn)Java中StringBuffer的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)Java中StringBuffer的方法,實(shí)例分析了StringBuffer類的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-02-02
JS實(shí)現(xiàn)表格響應(yīng)式布局技巧
這篇文章主要為大家介紹了JS實(shí)現(xiàn)表格響應(yīng)式布局技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
js實(shí)現(xiàn)百度聯(lián)盟中一款不錯(cuò)的圖片切換效果完整實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)百度聯(lián)盟中一款不錯(cuò)的圖片切換效果的方法,以完整實(shí)例形式分析了javascript操作圖片切換的技巧,需要的朋友可以參考下2015-03-03

