vue實(shí)現(xiàn)簡單轉(zhuǎn)盤抽獎功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)簡單轉(zhuǎn)盤抽獎的具體代碼,供大家參考,具體內(nèi)容如下
樣式請大家忽略(自己調(diào)),主要看JS代碼實(shí)現(xiàn),點(diǎn)擊按鈕后調(diào)用start方法,判斷是否在轉(zhuǎn)動狀態(tài),如果沒轉(zhuǎn)動則調(diào)用go方法,go方法主要封裝了一次性定時器,是個遞歸函數(shù),調(diào)用go函數(shù)后即可實(shí)現(xiàn)抽獎轉(zhuǎn)盤的效果了,詳細(xì)代碼如下:
注釋清晰哦
<template> ? <div class="home"> ? ? <button @click="start">開始</button> ? ? <div ? ? ? class="grid" ? ? ? v-for="(item, i) in arr" ? ? ? :key="i" ? ? ? :class="[bg == i + 1 ? 'active' : null]" ? ? ></div> ? </div> </template> <script> export default { ? name: "Home", ? data() { ? ? return { ? ? ? // 標(biāo)記轉(zhuǎn)動的位置 ? ? ? bg: 1, ? ? ? // 循環(huán)的獎品數(shù)組 ? ? ? arr: [1, 2, 3, 4, 5], ? ? ? // 是否正在轉(zhuǎn)動的標(biāo)記 ? ? ? isSport: false, ? ? ? // 轉(zhuǎn)動速度減慢 ? ? ? reduce: 10, ? ? ? // 轉(zhuǎn)動間隔時間 ? ? ? startTime: 30, ? ? ? area:'' ? ? }; ? }, ? methods: { ? ? start() { ? ? ? if (this.isSport == false) { ? ? ? ? this.isSport = true; ? ? ? } else { ? ? ? ? // 正在轉(zhuǎn)動時點(diǎn)擊按鈕無效 ? ? ? ? return; ? ? ? } ? ? ? // 模擬的設(shè)定轉(zhuǎn)動的位置 ? ? ? this.area= parseInt(Math.random()*4+1); ? ? ? this.go(); ? ? }, ? ? go() { ? ? ? setTimeout(() => { ? ? ? ? // 讓轉(zhuǎn)動速度減慢 ? ? ? ? this.startTime = this.startTime + this.reduce; ? ? ? ? this.bg = (this.bg % 5) + 1;//這個%時求余的意識哦 ? ? ? ? // 如果達(dá)到這個條件則停止跳動 ? ? ? ? if (this.startTime >= 300 && this.bg == this.area) { ? ? ? ? ? // 標(biāo)記是否轉(zhuǎn)動狀態(tài) ? ? ? ? ? this.isSport = false; ? ? ? ? ? // 重置轉(zhuǎn)動間隔時間 ? ? ? ? ? this.startTime = 30; ? ? ? ? ? return; ? ? ? ? } ? ? ? ? this.go(); ? ? ? }, this.startTime); ? ? }, ? }, }; </script> <style scoped> .grid { ? width: 50px; ? height: 50px; ? background: red; ? margin: 10px; } .active { ? background: blue; } </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)大轉(zhuǎn)盤抽獎功能
- Vue組件實(shí)現(xiàn)數(shù)字滾動抽獎效果
- vue實(shí)現(xiàn)宮格輪轉(zhuǎn)抽獎
- VUE實(shí)現(xiàn)大轉(zhuǎn)盤抽獎
- vue簡單實(shí)現(xiàn)轉(zhuǎn)盤抽獎
- vue組件實(shí)現(xiàn)移動端九宮格轉(zhuǎn)盤抽獎
- Vue.js實(shí)現(xiàn)大轉(zhuǎn)盤抽獎總結(jié)及實(shí)現(xiàn)思路
- 基于VUE實(shí)現(xiàn)的九宮格抽獎功能
- vue實(shí)現(xiàn)手機(jī)號碼抽獎上下滾動動畫示例
- vue3實(shí)現(xiàn)抽獎模板設(shè)置
相關(guān)文章
Vue3實(shí)現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
在開發(fā)工作過程中,我們會遇到各種各樣的表格數(shù)據(jù)導(dǎo)入,動態(tài)數(shù)據(jù)導(dǎo)入可以減少人為操作,減少出錯。本文為大家介紹了Vue3實(shí)現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法,需要的可以參考一下2022-11-11vue子組件封裝彈框只能執(zhí)行一次的mounted問題及解決
這篇文章主要介紹了vue子組件封裝彈框只能執(zhí)行一次的mounted問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03vue實(shí)現(xiàn)帶小數(shù)點(diǎn)的星星評分
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)帶小數(shù)點(diǎn)的星星評分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09vue?require.context()的用法實(shí)例詳解
require.context是webpack提供的一個api,通常用于批量注冊組件,下面這篇文章主要給大家介紹了關(guān)于vue?require.context()用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04