vue組件封裝實(shí)現(xiàn)抽獎隨機(jī)數(shù)
更新時(shí)間:2022年03月09日 14:36:17 作者:12歲零120個月的孩子
這篇文章主要為大家詳細(xì)介紹了vue組件封裝實(shí)現(xiàn)抽獎隨機(jī)數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue組件封裝實(shí)現(xiàn)抽獎隨機(jī)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
一、子組件
<template>
? ? <div>
? ? ?<slot></slot>
? ? </div>
</template>
?
?
<script>
? ? ?export default {
? ? ? ? ?name:'countUp',
? ? ? ? ?props:{
? ? ? ? ? ? ?lastSymbol:{
? ? ? ? ? ? ? ? ?type:String,
? ? ? ? ? ? ? ? ?default:" 位" ?
? ? ? ? ? ? ?},
? ? ? ? ? ? ?value:{ //滾動結(jié)束最終顯示數(shù)字
? ? ? ? ? ? ? ? ?type:[String,Number],
? ? ? ? ? ? ? ? ?default:100,
? ? ? ? ? ? ?},
? ? ? ? ? ? ?from:{ // 開始時(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:{ //是否開始滾動
? ? ? ? ? ? ? ? ?type:Boolean,
? ? ? ? ? ? ? ? ?default:true
? ? ? ? ? ? ?}
? ? ? ? ?},
? ? ? ? data(){
? ? ? ? ?return{
? ? ? ? ? ? ?loops:'', //刷新次數(shù)
? ? ? ? ? ? ?increment:'', //刷新一次增加的數(shù)值
? ? ? ? ? ? ?loopCount:'', //記錄刷新的次數(shù)
? ? ? ? ? ? CurrentValue:'', ?//開始時(shí)候的數(shù)字
? ? ? ? ? ? interval:'', //定時(shí)器
? ? ? ? ? ? sizeNum:'',//當(dāng)前數(shù)字的長度
? ? ? ? ? ? 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ù)字-開始的數(shù)字)/刷新次數(shù) ,刷新一次增加的數(shù)值
? ? ? ? ? ? ? ? this.loopCount = 0 //記錄刷新的次數(shù)
? ? ? ? ? ? ? ? this.CurrentValue = this.from //開始時(shí)候的數(shù)字
?
? ? ? ? ? ? ? ? this.interval = setInterval(this.updateTimer,this.refreshInterval) //設(shè)置定時(shí)器,沒個一段時(shí)間就執(zhí)行
? ? ? ? ? ? },
? ? ? ? ? ? updateTimer(){ //刷新一次數(shù)值疊加
? ? ? ? ? ? ?this.CurrentValue+=this.increment //當(dāng)前展示的值
? ? ? ? ? ? ?this.loopCount++ //刷新次數(shù)+1
? ? ? ? ? ? ?
? ? ? ? ? ? var tim = this.CurrentValue.toFixed(this.decimals) //對當(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">是否開始滾動</button>
? </div>
</template>
?
<script>
?
import countUp from '../../components/countUp/countUp.vue'; ?//下拉框帶popup蒙層
export default {
? ?name:'cecountUp',
? ?components: { //注冊組件
? ? countUp
? },
?
? data() {
? ? return {
? ? ?currentNum:"即將開始抽獎", //當(dāng)前數(shù)值
? ? ?isstart:false, //是否開始滾動數(shù)值
? ? };
? },
? methods: {
? ?acceptValue(val){ //接收當(dāng)前的數(shù)值展示到頁面
? ? this.currentNum =val
? ?},
? ?goChoujiang(){ //更改抽獎
? ? ? ?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>三、效果展示

四、所用知識點(diǎn):
父子組件之間的傳值,定時(shí)器setInterval(),清楚定時(shí)器clearInterval(),tofixed()四舍五入方法
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 簡單實(shí)現(xiàn)vue驗(yàn)證碼60秒倒計(jì)時(shí)功能
- Vue驗(yàn)證碼60秒倒計(jì)時(shí)功能簡單實(shí)例代碼
- Vue通過for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例
- 基于vue實(shí)現(xiàn)圖片驗(yàn)證碼倒計(jì)時(shí)60s功能
- Vue實(shí)現(xiàn)手機(jī)號、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))
- vue中如何使用唯一標(biāo)識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ù)覽的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Vue3監(jiān)聽store中數(shù)據(jù)變化的三種方式
這篇文章給大家介紹了Vue3監(jiān)聽store中數(shù)據(jù)變化的三種方法,使用watch和storeToRefs函數(shù),使用計(jì)算屬性computed和使用watchEffect函數(shù)這三種方法,文中通過代碼講解非常詳細(xì),需要的朋友可以參考下2024-01-01
ArcGis?API?for?js在vue.js中的使用示例詳解
這篇文章主要為大家介紹了ArcGis?API?for?js在vue.js中的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

