vue組件封裝實(shí)現(xiàn)抽獎(jiǎng)隨機(jī)數(shù)
本文實(shí)例為大家分享了vue組件封裝實(shí)現(xiàn)抽獎(jiǎng)隨機(jī)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
一、子組件
<template> ? ? <div> ? ? ?<slot></slot> ? ? </div> </template> ? ? <script> ? ? ?export default { ? ? ? ? ?name:'countUp', ? ? ? ? ?props:{ ? ? ? ? ? ? ?lastSymbol:{ ? ? ? ? ? ? ? ? ?type:String, ? ? ? ? ? ? ? ? ?default:" 位" ? ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?value:{ //滾動(dòng)結(jié)束最終顯示數(shù)字 ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:100, ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?from:{ // 開(kāi)始時(shí)的數(shù)字 ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:0 ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?speed:{ // 總時(shí)間 ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:2000, ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?refreshInterval:{ // 刷新一次的時(shí)間 ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:10, ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?beforeSize:{ //小數(shù)點(diǎn)前最小顯示位數(shù),不足的話用0代替? ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:0 ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?decimals:{ // 小數(shù)點(diǎn)后的位數(shù),小數(shù)做四舍五入 ? ? ? ? ? ? ? ? ?type:[String,Number], ? ? ? ? ? ? ? ? ?default:2 ? ? ? ? ? ? ?}, ? ? ? ? ? ? ?isstart:{ //是否開(kāi)始滾動(dòng) ? ? ? ? ? ? ? ? ?type:Boolean, ? ? ? ? ? ? ? ? ?default:true ? ? ? ? ? ? ?} ? ? ? ? ?}, ? ? ? ? data(){ ? ? ? ? ?return{ ? ? ? ? ? ? ?loops:'', //刷新次數(shù) ? ? ? ? ? ? ?increment:'', //刷新一次增加的數(shù)值 ? ? ? ? ? ? ?loopCount:'', //記錄刷新的次數(shù) ? ? ? ? ? ? CurrentValue:'', ?//開(kāi)始時(shí)候的數(shù)字 ? ? ? ? ? ? interval:'', //定時(shí)器 ? ? ? ? ? ? sizeNum:'',//當(dāng)前數(shù)字的長(zhǎng)度 ? ? ? ? ? ? sizeNumBefore:'', //當(dāng)前數(shù)字小數(shù)點(diǎn)前的位數(shù) ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ? ? ?watch:{ ? ? ? ? ? ? isstart(val){ ? ? ? ? ? ? ? ? ? ? if(val){ ? ? ? ? ? ? ? ? ? this.start() ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? clearInterval(this.interval); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ?}, ? ? ? ? methods:{ ? ? ? ? ? ? start(){ ? ? ? ? ? ? ? ?this.loops = Math.ceil(this.speed / this.refreshInterval)//刷新次數(shù) ? ? ? ? ? ? ? ? ? this.increment = (this.value - this.from)/this.loops ?//(結(jié)束的數(shù)字-開(kāi)始的數(shù)字)/刷新次數(shù) ,刷新一次增加的數(shù)值 ? ? ? ? ? ? ? ? this.loopCount = 0 //記錄刷新的次數(shù) ? ? ? ? ? ? ? ? this.CurrentValue = this.from //開(kāi)始時(shí)候的數(shù)字 ? ? ? ? ? ? ? ? ? this.interval = setInterval(this.updateTimer,this.refreshInterval) //設(shè)置定時(shí)器,沒(méi)個(gè)一段時(shí)間就執(zhí)行 ? ? ? ? ? ? }, ? ? ? ? ? ? updateTimer(){ //刷新一次數(shù)值疊加 ? ? ? ? ? ? ?this.CurrentValue+=this.increment //當(dāng)前展示的值 ? ? ? ? ? ? ?this.loopCount++ //刷新次數(shù)+1 ? ? ? ? ? ? ? ? ? ? ? ? ? var tim = this.CurrentValue.toFixed(this.decimals) //對(duì)當(dāng)前值進(jìn)行四舍五入 ,tim四射物質(zhì)之后的當(dāng)前數(shù)值 ? ? ? ? ? ? ? ?this.sizeNum = String(tim).length; ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.sizeNumBefore = this.sizeNum - this.decimals - 1; ? ? ? ? ? ? ?? ? ? ? ? ? ? ?if(this.sizeNumBefore>=this.beforeSize){ //當(dāng)前數(shù)字的小數(shù)點(diǎn)位數(shù)》=要求的小數(shù)點(diǎn)前位數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.$emit('sendValue',tim + this.lastSymbol) ? ? ? ? ? ? ? ?}else{ ? ? ? ? ? ? ? ? ? ? tim = Array(this.beforeSize-this.sizeNumBefore + 1).join('0') + tim; ? ? ? ? ? ? ? ? ? this.$emit('sendValue',tim + this.lastSymbol) ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? if(Number(this.loopCount)>=Number(this.loops)){ //清楚定時(shí)器 ? ? ? ? ? ? ? ? ? ?clearInterval(this.interval); ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? } ? ? ? ? ? ?? ? ? ? ? }, ? ? ? ? ? ?} </script> <style scoped>? ? ?? </style>
二、父組件
<template> ? <div class="about marquee"> ?? ? ? ? <count-up value="99.99" decimals="0" :isstart="isstart" @sendValue="acceptValue"><span class="changeNum">{{currentNum}}</span></count-up> ? ? ? <button class="btn" @click="goChoujiang">是否開(kāi)始滾動(dòng)</button> ? </div> </template> ? <script> ? import countUp from '../../components/countUp/countUp.vue'; ?//下拉框帶popup蒙層 export default { ? ?name:'cecountUp', ? ?components: { //注冊(cè)組件 ? ? countUp ? }, ? ? data() { ? ? return { ? ? ?currentNum:"即將開(kāi)始抽獎(jiǎng)", //當(dāng)前數(shù)值 ? ? ?isstart:false, //是否開(kāi)始滾動(dòng)數(shù)值 ? ? }; ? }, ? methods: { ? ?acceptValue(val){ //接收當(dāng)前的數(shù)值展示到頁(yè)面 ? ? this.currentNum =val ? ?}, ? ?goChoujiang(){ //更改抽獎(jiǎng) ? ? ? ?this.isstart = !this.isstart ? ?} ?? ? }, ?} </script> ? ? <style scoped> ? .changeNum{ ? ? color:red; ? ? font-size: 32px; ? } ? .btn{ ? ? background-color: pink; ? ? box-shadow:0px 2px 4px 0px rgba(255,130,0,0.7) ? } </style>
三、效果展示
四、所用知識(shí)點(diǎn):
父子組件之間的傳值,定時(shí)器setInterval(),清楚定時(shí)器clearInterval(),tofixed()四舍五入方法
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 簡(jiǎn)單實(shí)現(xiàn)vue驗(yàn)證碼60秒倒計(jì)時(shí)功能
- Vue驗(yàn)證碼60秒倒計(jì)時(shí)功能簡(jiǎn)單實(shí)例代碼
- Vue通過(guò)for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例
- 基于vue實(shí)現(xiàn)圖片驗(yàn)證碼倒計(jì)時(shí)60s功能
- Vue實(shí)現(xiàn)手機(jī)號(hào)、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))
- vue中如何使用唯一標(biāo)識(shí)uuid(uuid.v1()-時(shí)間戳、uuid.v4()-隨機(jī)數(shù))
- Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼
相關(guān)文章
vue中如何實(shí)現(xiàn)pdf文件預(yù)覽的方法
這篇文章主要介紹了vue中如何實(shí)現(xiàn)pdf文件預(yù)覽的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方式
這篇文章給大家介紹了Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方法,使用watch和storeToRefs函數(shù),使用計(jì)算屬性computed和使用watchEffect函數(shù)這三種方法,文中通過(guò)代碼講解非常詳細(xì),需要的朋友可以參考下2024-01-01ArcGis?API?for?js在vue.js中的使用示例詳解
這篇文章主要為大家介紹了ArcGis?API?for?js在vue.js中的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08