Vue金融數(shù)字格式化(并保留小數(shù))數(shù)字滾動效果實現(xiàn)
Vue金融數(shù)字格式化(并保留小數(shù)) 數(shù)字滾動
提示
我選用的是Vue 過濾器使用,個人覺得比較方便,不過過濾器不支持Vue3,你可以封裝成方法嘛都行,下面我以過濾器的形式展示出來
filters: { // 截取字符串 subStringText(value, index) { const str = String(value); if (!value) return 0; return str.length > index ? str.substring(0, index) + '...' : str; }, // 格式化數(shù)字 formatNumber(num,decimals) { num = num.toFixed(decimals); num += ''; const x = num.split('.'); let x1 = x[0]; const x2 = x.length > 1 ? '.' + x[1] : ''; const rgx = /(\d+)(\d{3})/; if (',' && isNaN(parseFloat(','))) { while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } } return x1 + x2; }, },
使用:
有個插件 -- 可以輕松實現(xiàn)數(shù)字滾動并且數(shù)字格式化,推薦vue-count-to ,非常友好,
補充:vue做數(shù)字滾動效果
vue實現(xiàn)數(shù)字滾動效果
近期在做項目的時候,產(chǎn)品要求實現(xiàn)數(shù)字滾動效果如下:
用jquery實現(xiàn)
html: <div class="develop"> <!--滾動的數(shù)字--> <p><span class="shuzi">3000000</span></p> <p><span class="shuzi">60000</span></p> </div>
js: $(".navigation_right li").click(function () { $(this).siblings('li').removeClass("yanse"); }); let arr = $(".develop>p>.shuzi"); arr.each(function(e, a){ let num = $(a).text() let i = 0; let count = parseInt(num /500); let timer = setInterval(function(){ $(a).text(i) i += count; if (i > num) window.clearInterval(timer) }, 5) })
這樣做有一個問題,只能和500取余且為整數(shù),而且滾動的時間也沒發(fā)控制,顯然是不滿足我們的業(yè)務場景的。
用vue-countTo實現(xiàn)
vue-countTo是一個無依賴,輕量級的vue組件,可以自行覆蓋easingFn。
安裝使用
npm install vue-count-to
例子
<template> <countTo :startVal='startVal' :endVal='endVal' :duration='3000'></countTo> </template> <script> //直接引入組件'vue-count-to' import countTo from 'vue-count-to'; export default { //注冊組件 components: { countTo }, data () { return { //數(shù)字開始 startVal: 0, //數(shù)字結束 endVal: 50000 } } } </script>
其中:startVal為開始數(shù)字,startVal為結束數(shù)字,duration為滾動時長, decimal:保留小數(shù)點后幾位
Property | Description | type | default |
---|---|---|---|
startVal | 開始值 | Number | 0 |
endVal | 結束值 | Number | 2017 |
duration | 持續(xù)時間,以毫秒為單位 | Number | 3000 |
autoplay | 自動播放 | Boolean | true |
decimals | 要顯示的小數(shù)位數(shù) | Number | 0 |
decimal | 十進制分割 | String | . |
separator | 分隔符 | String | , |
prefix | 前綴 | String | '' |
suffix | 后綴 | String | '' |
useEasing | 使用緩和功能 | Boolean | true |
easingFn | 緩和回調(diào) | Function | — |
** 注意:當autoplay:true時,它將在startVal或endVal更改時自動啟動**
Function Name | Description |
---|---|
mountedCallback | 掛載以后返回回調(diào) |
start | 開始計數(shù) |
pause | 暫停計數(shù) |
reset | 重置countTo |
到此這篇關于Vue金融數(shù)字格式化(并保留小數(shù)) 數(shù)字滾動的文章就介紹到這了,更多相關vue數(shù)字滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue-cli解決IE瀏覽器sockjs-client錯誤方法
這篇文章主要為大家介紹了vue-cli解決IE瀏覽器sockjs-client錯誤方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08el-select自定義指令實現(xiàn)觸底加載分頁請求options數(shù)據(jù)(完整代碼和接口可直接用)
某些情況下,下拉框需要做觸底加載,發(fā)請求,獲取option的數(shù)據(jù),下面給大家分享el-select自定義指令實現(xiàn)觸底加載分頁請求options數(shù)據(jù)(附上完整代碼和接口可直接用),感興趣的朋友參考下吧2024-02-02vue3項目vite.config.js配置代理、端口、打包名以及圖片壓縮
這篇文章主要給大家介紹了關于vue3項目vite.config.js配置代理、端口、打包名以及圖片壓縮的相關資料,因為3.0版本中vue已經(jīng)內(nèi)置了很多關于webpack的配置,一般情況下開箱即用,需要修改則可以在vue.config.js文件中完成,需要的朋友可以參考下2023-12-12