Vue中使用vue-count-to(數(shù)字滾動插件)詳細(xì)教程
1. 簡單介紹
npm官網(wǎng):vue-count-to
vue-count-to 就是一個組件插件,咱們引入進來,可以去打印一下,它就是一個組件實例,使用components注冊一下,就可以在模板中使用了,具體方式如下:
2. 安裝
npm install vue-count-to
3. 引入
import CountTo from 'vue-count-to'
4. 注冊
components: {
CountTo
},5. 模板中使用
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
6. 測試完整代碼
<template>
<div class="vue-count-to">
<div class="count-to">
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
<div>
<CountTo :startVal='startVal' :endVal='endVal' :duration='duration' />
</div>
</div>
</div>
</template>
<script>
import CountTo from 'vue-count-to'
export default {
data() {
return {
startVal: 0,
endVal: 100,
duration: 3000,
timer: null
}
},
components: {
CountTo
},
mounted(){
this.timer = setInterval(() => {
this.endVal = this.endVal * 2
}, 4000)
},
destroyed() {
clearInterval(this.timer)
},
}
</script>
<style scoped>
.vue-count-to {
width: 100%;
height: 100%;
}
.count-to {
width: 300px;
height: 300px;
margin: 100px 0 0 100px;
border: 1px solid red;
}
.count-to span {
font-size: 30px;
font-weight: 700;
font-family: 'YJSZ';
}
.count-to > div:nth-of-type(1) > span {
color: red;
}
.count-to > div:nth-of-type(2) > span {
color: blue;
}
.count-to > div:nth-of-type(3) > span {
color: pink;
}
.count-to > div:nth-of-type(4) > span {
color: yellow;
}
.count-to > div:nth-of-type(5) > span {
color: green;
}
.count-to > div:nth-of-type(6) > span {
color: orange;
}
.count-to > div:nth-of-type(7) > span {
color: cyan;
}
.count-to > div:nth-of-type(8) > span {
color: purple;
}
</style>7. 效果

實際滾動的是很流暢的哈;可能是我這個工具的問題
需要了解的是:
vue-count-to實際編譯出來的就是個span標(biāo)簽所以我們在給其寫樣式的時候可以直接用span標(biāo)簽;應(yīng)該也可以直接在上面寫class類名(我當(dāng)時沒試過這種方式寫樣式)
注意:
① vue-count-to只能適用 Vue2,并不適用于Vue3;
② 對于Vue3還有個vue3-count-to,但是這個好像用不了,我當(dāng)時試了,并沒有加載出來,而且也沒報錯,還有待研究
另外除了這個vue-count-to這個插件組件外,還有個數(shù)字翻牌器,鏈接附上:
這個也可以實現(xiàn),這個就類似個組件庫,包比較大,還有其他的一些可視化效果(圖表、動態(tài)換圖、邊框等等)
如果僅僅是實現(xiàn)這個數(shù)字滾動的話,還是使用vue-count-to方便一些,按需選擇吧
總結(jié)
到此這篇關(guān)于Vue中使用vue-count-to(數(shù)字滾動插件)的文章就介紹到這了,更多相關(guān)Vue使用vue-count-to內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中使用echarts繪制雙Y軸圖表時刻度沒有對齊的兩種解決方法(最新方案)
這篇文章主要介紹了vue中使用echarts繪制雙Y軸圖表時,刻度沒有對齊的兩種解決方法,主要原因是因為刻度在顯示時,分割段數(shù)不一樣,導(dǎo)致左右的刻度線不一致,不能重合在一起,下面給大家分享解決方法,需要的朋友可以參考下2024-03-03
一步步教你用Vue.js創(chuàng)建一個組件(附代碼示例)
組件(Component)是Vue.js最強大的功能之一,組件可以擴展HTML元素,封裝可重用的代碼,下面這篇文章主要給大家介紹了關(guān)于如何一步步用Vue.js創(chuàng)建一個組件的相關(guān)資料,需要的朋友可以參考下2022-12-12
Vue使用pages構(gòu)建多頁應(yīng)用的實現(xiàn)步驟
在大部分實際場景中,我們都可以構(gòu)建單頁應(yīng)用來進行項目的開發(fā)和迭代,然而對于項目復(fù)雜度過高或者頁面模塊之間差異化較大的項目,我們可以選擇構(gòu)建多頁應(yīng)用來實現(xiàn),那么什么是多頁應(yīng)用,本文就給大家介紹了Vue使用pages構(gòu)建多頁應(yīng)用的實現(xiàn)步驟2024-12-12
elementPlus?的el-select在提示框關(guān)閉時自動彈出的問題解決
這篇文章主要介紹了elementPlus?的el-select在提示框關(guān)閉時自動彈出閉時自動彈出的問題,主要問題就是因為filterable屬性,根本解決方案是選中的時候讓他失去焦點?el-select有一個visible-change事件,下拉框出現(xiàn)/隱藏時觸發(fā),感興趣的朋友跟隨小編一起看看吧2024-01-01

