您好,登錄后才能下訂單哦!
小編給大家分享一下vue如何實現宮格輪轉抽獎,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
vue實現宮格輪轉抽獎(類似穿越火線的xx輪回),供大家參考,具體內容如下
不做過多的解說,直接上代碼啦。關鍵的代碼都寫了注釋,很容易理解。直接復制即可使用!
另外css部分依賴 node-sass、sass-loader,沒有安裝的安裝一下,已有的小伙伴直接跳過~~
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
<template> <div class="home"> <div class="home-container"> <div class="home-container-line"> <div class="home-container-line-box" v-for="item in list.slice(0, 5)" :key="item.index" :class="{ selected: item.active }" > {{ item.label }} </div> </div> <div class="home-container-line"> <div class="home-container-line-box" v-for="item in list.slice(11, 12)" :key="item.index" :class="{ selected: item.active }" > {{ item.label }} </div> <div class="home-container-line-btn" @click="handleClick" :disable="isAnimate"></div> <div class="home-container-line-box" v-for="item in list.slice(5, 6)" :key="item.index" :class="{ selected: item.active }" > {{ item.label }} </div> </div> <div class="home-container-line"> <div class="home-container-line-box" v-for="item in Array.prototype.reverse.call(list.slice(6, 11))" :key="item.index" :class="{ selected: item.active }" > {{ item.label }} </div> </div> </div> </div> </template> <script> export default { name: "Luck", data() { return { list: [ { label: "未中獎", val: 1, img: "", index: 0, active: false }, { label: "大保健", val: 2, img: "", index: 1, active: false }, { label: "iPhone13", val: 3, img: "", index: 2, active: false }, { label: "MacBook Pro", val: 4, img: "", index: 3, active: false }, { label: "MacBook Air", val: 5, img: "", index: 4, active: false }, { label: "1積分", val: 6, img: "", index: 5, active: false }, { label: "5積分", val: 7, img: "", index: 6, active: false }, { label: "10積分", val: 8, img: "", index: 7, active: false }, { label: "小愛同學", val: 9, img: "", index: 8, active: false }, { label: "安慕希酸奶", val: 10, img: "", index: 9, active: false }, { label: "清空購物車", val: 11, img: "", index: 10, active: false }, { label: "50元話費", val: 12, img: "", index: 11, active: false }, ], isAnimate: false, // 為 true時代表正在抽獎,當前抽獎未結束時點擊抽獎按鈕無效 jumpIndex: Math.floor(Math.random() * 12), // 抽獎輪跳時的索引 jumpingTime: 0, // 正在輪跳的時間 jumpingTotalTime: 0, // 輪跳的時間總量,基于 duration 變量加上一個 0~1000 之間的隨機數組成 jumpingChange: 0, // 輪跳速率峰值,基于 velocity 變量加上一個 0~3 之間的隨機數組成 duration: 4500, // 持續時間 velocity: 300, // 速率 }; }, methods: { handleClick() { if(this.isAnimate) return; this.jumpingTime = 0; this.jumpingTotalTime = Math.random() * 1000 + this.duration; this.jumpingChange = Math.random() * 3 + this.velocity; this.animateRound(this.jumpIndex); }, animateRound(index) { this.isAnimate = true; if(this.jumpIndex < this.list.length - 1 ) this.jumpIndex ++; if(this.jumpIndex >= this.list.length - 1 ) this.jumpIndex = 0; this.jumpingTime += 100; // 每一幀執行 setTimeout 方法所消耗的時間 // 如果當前時間大于時間總量后, 退出動畫, 清算獎品 if(this.jumpingTime >= this.jumpingTotalTime){ this.isAnimate = false; if(index == 0) { alert(`很遺憾沒有抽到獎品,再接再厲哦~`); } else{ alert(`恭喜您抽到了:${this.list[index].label}`) } return } // 輪訓動畫 if (index >= this.list.length-1) { index = 0; this.list[11].active = false; this.list[index].active = true; index -= 1; } else { this.list[index].active = false; this.list[index + 1].active = true; } setTimeout(() => { this.animateRound(index + 1); }, this.easeOut(this.jumpingTime, 0, this.jumpingChange, this.jumpingTotalTime)); }, /** * 緩動函數,由快到慢 * @param {Num} t 當前時間 * @param {Num} b 初始值 * @param {Num} c 變化值 * @param {Num} d 持續時間 */ easeOut(t, b, c, d) { if ((t /= d / 2) < 1) return (c / 2) * t * t + b; return (-c / 2) * (--t * (t - 2) - 1) + b; }, }, }; </script> <style lang="scss" scoped> .center { display: flex; justify-content: center; align-items: center; } .home { padding: 0; margin: 0; width: 100%; height: calc(100vh - 16px); background-image: linear-gradient(25deg, #30007c, #464995, #4d83ad, #41bfc4); @extend .center; &-container { width: 1000px; height: 600px; &-line { width: 100%; height: 198px; display: flex; &-box { flex: 1; overflow: hidden; margin: 5px 3px 5px 3px; @extend .center; background: #fff; transition: all .3s; } &-btn { position: relative; flex: 3; overflow: hidden; margin: 5px 3px 3px 3px; @extend .center; box-shadow: 0 1px 10px 0px #cf5531; background-image: linear-gradient( 25deg, #cf5531, #d0853a, #cdaf43, #c4d84d ); cursor: pointer; &:active { background-image: linear-gradient( 25deg, #3f3e41, #6d6340, #9a8b39, #c9b629 ); } &::before { position: absolute; content: "點擊抽獎"; font-size: 2.5rem; color: #fff; font-weight: bold; } } } } } .selected { background: rgba($color: #f6e58d, $alpha: 0.5); animation-name: twinkle; animation-duration: 3s; animation-iteration-count: infinite; } @keyframes twinkle { 0% {background:#ffbe76;} 100% {background:#f6e58d;} } </style>
效果圖:
看完了這篇文章,相信你對“vue如何實現宮格輪轉抽獎”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。