vue 數字遞增(滾動從0到)

使用

html
<Incremental :startVal="0" :endVal="1000" :duration="500" />js:
import Incremental from '@/utils/num/numView'```

js

let lastTime = 0
const prefixes = 'webkit moz ms o'.split(' ') // 各瀏覽器前綴let requestAnimationFrame
let cancelAnimationFrameconst isServer = typeof window === 'undefined'
if (isServer) {requestAnimationFrame = function () {}cancelAnimationFrame = function () {}
} else {requestAnimationFrame = window.requestAnimationFramecancelAnimationFrame = window.cancelAnimationFramelet 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,則會退到setTimeoutif (!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 + timeToCallcallback(time)}, timeToCall)lastTime = currTime + timeToCallreturn id}cancelAnimationFrame = function (id) {window.clearTimeout(id)}}
}export { requestAnimationFrame, cancelAnimationFrame }

封裝vue模塊 (需要時倒入)

<template><span>{{ displayValue }}</span>
</template>
<script>
import { requestAnimationFrame, cancelAnimationFrame } from './num.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.startValthis.startTime = nullthis.localDuration = this.durationthis.paused = falsethis.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 = nullthis.localDuration = +this.remainingthis.localStartVal = +this.printValrequestAnimationFrame(this.count)},reset() {this.startTime = nullcancelAnimationFrame(this.rAF)this.displayValue = this.formatNumber(this.startVal)},count(timestamp) {if (!this.startTime) this.startTime = timestampthis.timestamp = timestampconst progress = timestamp - this.startTimethis.remaining = this.localDuration - progressif (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>

在這里插入圖片描述
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/40510.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/40510.shtml
英文地址,請注明出處:http://en.pswp.cn/news/40510.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[C++] string類的介紹與構造的模擬實現,進來看吧,里面有空調

文章目錄 1、string類的出現1.1 C語言中的字符串 2、標準庫中的string類2.1 string類 3、string類的常見接口說明及模擬實現3.1 string的常見構造3.2 string的構造函數3.3 string的拷貝構造3.4 string的賦值構造 4、完整代碼 1、string類的出現 1.1 C語言中的字符串 C語言中&…

「Qt」文件讀寫操作

0、引言 我們知道 C 和 C 都提供了文件讀寫的類庫&#xff0c;不過 Qt 也有一套自己的文件讀寫操作&#xff1b;本文主要介紹 Qt 中進行文件讀寫操作的類 —— QFile。 1、QFileDialog 文件對話框 一般的桌面應用程序&#xff0c;當我們想要打開一個文件時&#xff0c;通常會彈…

php+echarts實現數據可視化實例

效果&#xff1a; 代碼&#xff1a; php <?php include(includes/session.inc); include(includes/SQL_CommonFunctions.inc); ?> <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv&quo…

OpenLayers入門,OpenLayers加載google街景地圖

專欄目錄: OpenLayers入門教程匯總目錄 前言 本章講解OpenLayers加載google街景地圖,無需科學上網,也可以正常訪問瓦片。 二、依賴和使用 "ol": "^6.15.1"使用npm安裝依賴npm install ol@6.15.1使用Yarn安裝依賴yarn add olvue中如何使用: vue項…

FastApi-1-結合sql 增/查demo

目錄 FastAPI學習記錄項目結構部分接口/代碼展示感受全部代碼 FastAPI學習記錄 fastapi已經學習有一段時間&#xff0c;今天抽時間簡單整理下。 官網介紹&#xff1a; FastAPI 是一個用于構建 API 的現代、快速&#xff08;高性能&#xff09;的 web 框架&#xff0c;使用 Py…

SpringBoot的配置文件以及日志設置

在使用SpringBoot開發的過程中我們通常會用到配置文件來設置配置信息 以及使用日志來進行記錄我們的操作&#xff0c;方便我們對錯誤的定位 配置文件的作用在于&#xff1a;設置端口&#xff0c;設置數據庫連接信息&#xff0c;設置日志等等 在SpringBoot中&#xff0c;配置…

Linux系統編程:通過System V共享內存實現進程間通信

目錄 一. 共享內存實現進程間通信的原理 二. 共享內存相關函數 2.1 共享內存的獲取 shmget / ftok 2.2 共享內存與進程地址空間相關聯 shmat 2.3 取消共享內存與進程地址空間的關聯 shmdt 2.4 刪除共享內存 shmctl 2.5 通信雙方創建共享內存代碼 三. 共享內存實現進程間…

承接各種設計

小弟985研究生畢業&#xff0c;目前攻讀讀博士&#xff0c;可做各種設計&#xff0c;包括但不限于Matlab 電力電子/電氣工程&#xff0c;matlab/simulink 電氣專業仿真MATLAB 電氣工程專業&#xff0c;matlab建模 電力電子&#xff0c;電氣工程&#xff0c;電力系統&#xff0c…

vue echarts macd指標 完整代碼

1 邏輯 給指定的series兩個對象 兩個對象有相同的xAxisIndex: 2,yAxisIndex: 2, 不同的data {name: "",type: "line",data: data1,xAxisIndex: 2,yAxisIndex: 2,},{name: "",type: "bar",data: data2,xAxisIndex: 2,yAxisIndex: 2,},…

Mac M2 Pro安裝使用Cocoapods

Mac Pro M2安裝使用Cocoapods 在新公司要做iOS開發&#xff0c;所以在新電腦上安裝Cocoapods 在升級gem&#xff0c;sudo gem update --system&#xff0c;和安裝cocoapods時都遇到如下的提示&#xff1a; ERROR: While executing gem ... (Errno::EPERM)Operation not per…

Linux下安裝nodejs

1、下載nodejs 點擊前往&#xff1a;Download | Node.js 2、解壓 tar -xvf node-v18.16.0-linux-x64.tar.xz mv node-v18.16.0-linux-x64/ /usr/local/nodejs 3、 建立軟鏈接 此時的bin文件夾中已經存在node以及npm&#xff0c;如果你進入到對應文件的中執行命令行一點問題…

現代C++:使用 shared_from_this 防止 this 提前被釋放

首先概括一下shared_from_this的作用&#xff1a;可以在類的成員函數中直接通過this得到指向當前所在對象的shared_ptr的智能指針&#xff0c;具體操作如下。 使用方法 設需要提供shared_from_this方法的類為C0定義為類&#xff0c;首先需要將C0定義為 std::enable_shared_fr…

mysql 02 數據庫的約束

為防止錯誤的數據被插入到數據表&#xff0c;MySQL中定義了一些維護數據庫完整性的規則&#xff1b;這些規則常稱為表的約束。常見約束如下&#xff1a; 主鍵約束 主鍵約束即primary key用于唯一的標識表中的每一行。被標識為主鍵的數據在表中是唯一的且其值不能為空。這點類似…

前后端分離------后端創建筆記(10)用戶修改

本文章轉載于【SpringBootVue】全網最簡單但實用的前后端分離項目實戰筆記 - 前端_大菜007的博客-CSDN博客 僅用于學習和討論&#xff0c;如有侵權請聯系 源碼&#xff1a;https://gitee.com/green_vegetables/x-admin-project.git 素材&#xff1a;https://pan.baidu.com/s/…

Spring Boot實現第一次啟動時自動初始化數據庫流程詳解

隨著互聯網的發展項目中的業務功能越來越復雜&#xff0c;有一些基礎服務我們不可避免的會去調用一些第三方的接口或者公司內其他項目中提供的服務&#xff0c;但是遠程服務的健壯性和網絡穩定性都是不可控因素。 在測試階段可能沒有什么異常情況&#xff0c;但上線后可能會出…

https證書獲取的方法及好處

我們常說的https證書其實就是ssl證書&#xff0c;眼下為網站部署https證書是保障網站安全必不可少的一步。而https證書該如何獲取呢&#xff1f;下面就簡單介紹一下https證書獲取的方法。 https證書獲取途徑有兩種&#xff1a;自己簽發和由受信任的CA機構簽發。 自己給自己簽…

全國三網優惠話費充值接口開發指南

一、文檔綜述 近期想做項目的看過來~三網&#xff08;全國移動、聯通、電信&#xff09;話費、電費充值接口能夠實現將接口接入到小程序或者app上面&#xff0c;通過接口提交號碼和金額進行充值&#xff0c;可以幫助相關人員快速完成接口對接與聯調&#xff0c;平臺用戶可以通…

設計HTML5文本

網頁文本內容豐富、形式多樣&#xff0c;通過不同的版式顯示在頁面中&#xff0c;為用戶提供最直接、最豐富的信息。HTML5新增了很多文本標簽&#xff0c;它們都有特殊的語義&#xff0c;正確使用這些標簽&#xff0c;可以讓網頁文本更嚴謹、更符合語義。 1、通用文本 1.1、標…

算法競賽備賽之搜索與圖論訓練提升,暑期集訓營培訓

目錄 1.DFS和BFS 1.1.DFS深度優先搜索 1.2.BFS廣度優先搜索 2.樹與圖的遍歷&#xff1a;拓撲排序 3.最短路 3.1.迪杰斯特拉算法 3.2.貝爾曼算法 3.3.SPFA算法 3.4.多源匯最短路Floy算法 4.最小生成樹 4.1.普利姆算法 4.2.克魯斯卡爾算法 5.二分圖&#xff1a;染色法…

7. CSS(四)

目錄 一、浮動 &#xff08;一&#xff09;傳統網頁布局的三種方式 &#xff08;二&#xff09;標準流&#xff08;普通流/文檔流&#xff09; &#xff08;三&#xff09;為什么需要浮動&#xff1f; &#xff08;四&#xff09;什么是浮動 &#xff08;五&#xff09;浮…