Vue中使用vue-count-to(數(shù)字滾動(dòng)插件)詳細(xì)教程
1. 簡單介紹
npm官網(wǎng):vue-count-to
vue-count-to 就是一個(gè)組件插件,咱們引入進(jìn)來,可以去打印一下,它就是一個(gè)組件實(shí)例,使用components注冊(cè)一下,就可以在模板中使用了,具體方式如下:
2. 安裝
npm install vue-count-to
3. 引入
import CountTo from 'vue-count-to'
4. 注冊(cè)
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. 效果

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

