一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - vue.js - vue3使用vue-count-to組件的實現

vue3使用vue-count-to組件的實現

2021-12-20 16:00TwoKe vue.js

這篇文章主要介紹了vue3使用vue-count-to組件的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

項目場景:

數據可視化大屏開發的過程中,需要實現一種滾動數字的效果,在使用vue2時,使用vue-count-to完全沒有問題,功能也比較完善(滾動時長,開始值,結束值,前綴,后綴,千分隔符,小數分隔符等等),但是在vue3中使用會出現問題。

<template>
 <div id="nav">
 <router-link to="/">Home</router-link> |
 <router-link to="/about">About</router-link>
 </div>
 <count-to :startVal="0" :endVal="2045" :duration="4000"></count-to>
 <router-view/>
</template>

展示的效果

vue3使用vue-count-to組件的實現

問題描述:

出現的錯誤時 == Cannot read property ‘_c" of undefined== 這是一個_c的屬性沒有找到,具體的情況也不是很清楚。在vue-count-to打包后的源碼中可以大致看出來,這是在render函數中出現的錯誤。但是還是沒法下手。

vue3使用vue-count-to組件的實現

解決方案:

采用的方法是直接復制node_modules下vue-count-to的源文件(src下),到自己項目的components下。如圖

vue3使用vue-count-to組件的實現

然后根據eslint的檢查,修改代碼,直到不報錯,且記刪除package.json下剛剛引入的vue-count-to的依賴。如圖

vue3使用vue-count-to組件的實現

最后重啟項目。

vue-count-to源碼

let lastTime = 0
const prefixes = "webkit moz ms o".split(" ") // 各瀏覽器前綴

let requestAnimationFrame
let cancelAnimationFrame

const isServer = typeof window === "undefined"
if (isServer) {
 requestAnimationFrame = function () {
 }
 cancelAnimationFrame = function () {
 }
} else {
 requestAnimationFrame = window.requestAnimationFrame
 cancelAnimationFrame = window.cancelAnimationFrame
 let prefix
 // 通過遍歷各瀏覽器前綴,來得到requestAnimationFrame和cancelAnimationFrame在當前瀏覽器的實現形式
 for (let i = 0; i < prefixes.length; i++) {
 if (requestAnimationFrame && cancelAnimationFrame) { break }
 prefix = prefixes[i]
 requestAnimationFrame = requestAnimationFrame || window[prefix + "RequestAnimationFrame"]
 cancelAnimationFrame = cancelAnimationFrame || window[prefix + "CancelAnimationFrame"] || window[prefix + "CancelRequestAnimationFrame"]
 }

 // 如果當前瀏覽器不支持requestAnimationFrame和cancelAnimationFrame,則會退到setTimeout
 if (!requestAnimationFrame || !cancelAnimationFrame) {
 requestAnimationFrame = function (callback) {
 const currTime = new Date().getTime()
 // 為了使setTimteout的盡可能的接近每秒60幀的效果
 const timeToCall = Math.max(0, 16 - (currTime - lastTime))
 const id = window.setTimeout(() => {
 const time = currTime + timeToCall
 callback(time)
 }, timeToCall)
 lastTime = currTime + timeToCall
 return id
 }

 cancelAnimationFrame = function (id) {
 window.clearTimeout(id)
 }
 }
}

export { requestAnimationFrame, cancelAnimationFrame }
<template>
 <span>
 {{displayValue}}
 </span>
</template>
<script>
import { requestAnimationFrame, cancelAnimationFrame } from "./requestAnimationFrame.js"
export default {
 props: {
 startVal: {
 type: Number,
 required: false,
 default: 0
 },
 endVal: {
 type: Number,
 required: false,
 default: 2017
 },
 duration: {
 type: Number,
 required: false,
 default: 3000
 },
 autoplay: {
 type: Boolean,
 required: false,
 default: true
 },
 decimals: {
 type: Number,
 required: false,
 default: 0,
 validator (value) {
 return value >= 0
 }
 },
 decimal: {
 type: String,
 required: false,
 default: "."
 },
 separator: {
 type: String,
 required: false,
 default: ","
 },
 prefix: {
 type: String,
 required: false,
 default: ""
 },
 suffix: {
 type: String,
 required: false,
 default: ""
 },
 useEasing: {
 type: Boolean,
 required: false,
 default: true
 },
 easingFn: {
 type: Function,
 default (t, b, c, d) {
 return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b
 }
 }
 },
 data () {
 return {
 localStartVal: this.startVal,
 displayValue: this.formatNumber(this.startVal),
 printVal: null,
 paused: false,
 localDuration: this.duration,
 startTime: null,
 timestamp: null,
 remaining: null,
 rAF: null
 }
 },
 computed: {
 countDown () {
 return this.startVal > this.endVal
 }
 },
 watch: {
 startVal () {
 if (this.autoplay) {
 this.start()
 }
 },
 endVal () {
 if (this.autoplay) {
 this.start()
 }
 }
 },
 mounted () {
 if (this.autoplay) {
 this.start()
 }
 this.$emit("mountedCallback")
 },
 methods: {
 start () {
 this.localStartVal = this.startVal
 this.startTime = null
 this.localDuration = this.duration
 this.paused = false
 this.rAF = requestAnimationFrame(this.count)
 },
 pauseResume () {
 if (this.paused) {
 this.resume()
 this.paused = false
 } else {
 this.pause()
 this.paused = true
 }
 },
 pause () {
 cancelAnimationFrame(this.rAF)
 },
 resume () {
 this.startTime = null
 this.localDuration = +this.remaining
 this.localStartVal = +this.printVal
 requestAnimationFrame(this.count)
 },
 reset () {
 this.startTime = null
 cancelAnimationFrame(this.rAF)
 this.displayValue = this.formatNumber(this.startVal)
 },
 count (timestamp) {
 if (!this.startTime) this.startTime = timestamp
 this.timestamp = timestamp
 const progress = timestamp - this.startTime
 this.remaining = this.localDuration - progress

 if (this.useEasing) {
 if (this.countDown) {
 this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.endVal, this.localDuration)
 } else {
 this.printVal = this.easingFn(progress, this.localStartVal, this.endVal - this.localStartVal, this.localDuration)
 }
 } else {
 if (this.countDown) {
 this.printVal = this.localStartVal - ((this.localStartVal - this.endVal) * (progress / this.localDuration))
 } else {
 this.printVal = this.localStartVal + (this.endVal - this.localStartVal) * (progress / this.localDuration)
 }
 }
 if (this.countDown) {
 this.printVal = this.printVal < this.endVal ? this.endVal : this.printVal
 } else {
 this.printVal = this.printVal > this.endVal ? this.endVal : this.printVal
 }

 this.displayValue = this.formatNumber(this.printVal)
 if (progress < this.localDuration) {
 this.rAF = requestAnimationFrame(this.count)
 } else {
 this.$emit("callback")
 }
 },
 isNumber (val) {
 return !isNaN(parseFloat(val))
 },
 formatNumber (num) {
 num = num.toFixed(this.decimals)
 num += ""
 const x = num.split(".")
 let x1 = x[0]
 const x2 = x.length > 1 ? this.decimal + x[1] : ""
 const rgx = /(d+)(d{3})/
 if (this.separator && !this.isNumber(this.separator)) {
 while (rgx.test(x1)) {
 x1 = x1.replace(rgx, "$1" + this.separator + "$2")
 }
 }
 return this.prefix + x1 + x2 + this.suffix
 }
 },
 unmounted () {
 cancelAnimationFrame(this.rAF)
 }
}
</script>

到此這篇關于vue3使用vue-count-to組件的文章就介紹到這了,更多相關vue3 vue-count-to組件內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/qq_38330707/article/details/111514478

延伸 · 閱讀

精彩推薦
  • vue.jsVue項目中實現帶參跳轉功能

    Vue項目中實現帶參跳轉功能

    最近做了一個手機端系統,其中遇到了父頁面需要攜帶參數跳轉至子頁面的問題,現已解決,下面分享一下實現過程,感興趣的朋友一起看看吧...

    YiluRen丶4302022-03-03
  • vue.js梳理一下vue中的生命周期

    梳理一下vue中的生命周期

    看過很多人講vue的生命周期,但總是被繞的云里霧里,尤其是自學的同學,可能js的基礎也不是太牢固,聽起來更是吃力,那我就已個人之淺見,以大白話...

    CRMEB技術團隊7992021-12-22
  • vue.jsVue2.x 項目性能優化之代碼優化的實現

    Vue2.x 項目性能優化之代碼優化的實現

    這篇文章主要介紹了Vue2.x 項目性能優化之代碼優化的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋...

    優小U9632022-02-21
  • vue.jsVue2.x-使用防抖以及節流的示例

    Vue2.x-使用防抖以及節流的示例

    這篇文章主要介紹了Vue2.x-使用防抖以及節流的示例,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下...

    Kyara6372022-01-25
  • vue.js詳解vue 表單綁定與組件

    詳解vue 表單綁定與組件

    這篇文章主要介紹了vue 表單綁定與組件的相關資料,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下...

    Latteitcjz6432022-02-12
  • vue.jsVue中引入svg圖標的兩種方式

    Vue中引入svg圖標的兩種方式

    這篇文章主要給大家介紹了關于Vue中引入svg圖標的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的...

    十里不故夢10222021-12-31
  • vue.jsVue多選列表組件深入詳解

    Vue多選列表組件深入詳解

    這篇文章主要介紹了Vue多選列表組件深入詳解,這個是vue的基本組件,有需要的同學可以研究下...

    yukiwu6752022-01-25
  • vue.js用vite搭建vue3應用的實現方法

    用vite搭建vue3應用的實現方法

    這篇文章主要介紹了用vite搭建vue3應用的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下...

    Asiter7912022-01-22
主站蜘蛛池模板: 国产精品天天在线 | 欧美性受xxxx88喷潮 | a天堂中文在线 | 2023最新伦理片 | 2019国产精品| 国产一区二区三区福利 | 四虎永久在线精品波多野结衣 | 北岛玲在线播放 | 热久久天天拍天天拍热久久2018 | 国产亚洲精品日韩香蕉网 | 亚洲 欧美 日韩 国产 视频 | 国产伊人久久 | 日本黄大片影院一区二区 | v视影院 | 小仙夜晚慰自催眠mp3护士篇 | 国产精品天天看特色大片不卡 | 日本花季传媒2020旧版安卓 | 午夜福利体验免费体验区 | 国产成人影院一区二区 | 波多野结衣两女调教 | 欧美一区二区三区在线观看不卡 | 亚洲男人天堂a | 国产麻豆成91 | 动漫美女隐私尿口图片 | 非洲黑人bbwbbwbbw | yellow视频在线观看免费 | 亚洲一区二区成人 | 久久婷婷五月综合色丁香花 | yy111111影院理论大片 | 欧美色精品天天在线观看视频 | 国产草| 亚洲一区二区精品视频 | 国产精品污双胞胎在线观看 | 日本视频在线观看播放 | 日本一区二区三区在线 观看网站 | 美女被绑着吸下部的故事 | 草莓香蕉榴莲丝瓜秋葵绿巨人在线看 | 国产思妍小仙女一二区 | 久久久久久久电影 | 肥胖女性大bbbbbb视频女厕 | 香蕉久草在线 |