位置: IT常识 - 正文

vue3 | 数据可视化实现数字滚动特效(vue数据可视化大屏布局)

编辑:rootadmin
vue3 | 数据可视化实现数字滚动特效 前言

推荐整理分享vue3 | 数据可视化实现数字滚动特效(vue数据可视化大屏布局),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue可视化创建项目,vue cli可视化,vue cli可视化,vue实现数据可视化,vue可视化创建项目,vue数据可视化大屏布局,vue数据可视化大屏布局,vue实现数据可视化,内容如对您有帮助,希望把文章链接给更多的朋友!

vue3不支持vue-count-to插件,无法使用vue-count-to实现数字动效,数字自动分割,vue-count-to主要针对vue2使用,vue3按照会报错: TypeError: Cannot read properties of undefined (reading '_c') 的错误信息。这个时候我们只能自己封装一个CountTo组件实现数字动效。先来看效果图:

思路

使用Vue.component定义公共组件,使用window.requestAnimationFrame(首选,次选setTimeout)来循环数字动画,window.cancelAnimationFrame取消数字动画效果,封装一个requestAnimationFrame.js公共文件,CountTo.vue组件,入口导出文件index.js。

文件目录

使用示例<CountTo :start="0" // 从数字多少开始 :end="endCount" // 到数字多少结束 :autoPlay="true" // 自动播放 :duration="3000" // 过渡时间 prefix="¥" // 前缀符号 suffix="rmb" // 后缀符号 />入口文件index.jsconst UILib = { install(Vue) { Vue.component('CountTo', CountTo) }}export default UILibmain.js使用import CountTo from './components/count-to/index';app.use(CountTo)requestAnimationFrame.js思路先判断是不是浏览器还是其他环境如果是浏览器判断浏览器内核类型如果浏览器不支持requestAnimationFrame,cancelAnimationFrame方法,改写setTimeout定时器导出两个方法 requestAnimationFrame, cancelAnimationFrame各个浏览器前缀:let prefixes = 'webkit moz ms o';判断是不是浏览器:let isServe = typeof window == 'undefined';增加各个浏览器前缀: let prefix;let requestAnimationFrame;let cancelAnimationFrame;// 通过遍历各浏览器前缀,来得到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'] } //不支持使用setTimeout方式替换:模拟60帧的效果 // 如果当前浏览器不支持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(() => { callback(currTime + timeToCall) }, timeToCall) lastTime = currTime + timeToCall return id } cancelAnimationFrame = function (id) { window.clearTimeout(id) } }完整代码:

requestAnimationFrame.js

let lastTime = 0const prefixes = 'webkit moz ms o'.split(' ') // 各浏览器前缀let requestAnimationFramelet cancelAnimationFrame// 判断是否是服务器环境const isServer = typeof window === 'undefined'if (isServer) { requestAnimationFrame = function () { return } cancelAnimationFrame = function () { return }} 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(() => { callback(currTime + timeToCall) }, timeToCall) lastTime = currTime + timeToCall return id } cancelAnimationFrame = function (id) { window.clearTimeout(id) } }}export { requestAnimationFrame, cancelAnimationFrame }CountTo.vue组件思路vue3 | 数据可视化实现数字滚动特效(vue数据可视化大屏布局)

首先引入requestAnimationFrame.js,使用requestAnimationFrame方法接受count函数,还需要格式化数字,进行正则表达式转换,返回我们想要的数据格式。

引入 import { requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame.js'

需要接受的参数:

const props = defineProps({ start: { type: Number, required: false, default: 0 }, end: { type: Number, required: false, default: 0 }, duration: { type: Number, required: false, default: 5000 }, 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; } }})

启动数字动效

const startCount = () => { state.localStart = props.start state.startTime = null state.localDuration = props.duration state.paused = false state.rAF = requestAnimationFrame(count)}

核心函数,对数字进行转动

if (!state.startTime) state.startTime = timestamp state.timestamp = timestamp const progress = timestamp - state.startTime state.remaining = state.localDuration - progress // 是否使用速度变化曲线 if (props.useEasing) { if (stopCount.value) { state.printVal = state.localStart - props.easingFn(progress, 0, state.localStart - props.end, state.localDuration) } else { state.printVal = props.easingFn(progress, state.localStart, props.end - state.localStart, state.localDuration) } } else { if (stopCount.value) { state.printVal = state.localStart - ((state.localStart - props.end) * (progress / state.localDuration)) } else { state.printVal = state.localStart + (props.end - state.localStart) * (progress / state.localDuration) } } if (stopCount.value) { state.printVal = state.printVal < props.end ? props.end : state.printVal } else { state.printVal = state.printVal > props.end ? props.end : state.printVal } state.displayValue = formatNumber(state.printVal) if (progress < state.localDuration) { state.rAF = requestAnimationFrame(count) } else { emits('callback') }}// 格式化数据,返回想要展示的数据格式const formatNumber = (val) => { val = val.toFixed(props.default) val += '' const x = val.split('.') let x1 = x[0] const x2 = x.length > 1 ? props.decimal + x[1] : '' const rgx = /(\d+)(\d{3})/ if (props.separator && !isNumber(props.separator)) { while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + props.separator + '$2') } } return props.prefix + x1 + x2 + props.suffix}

取消动效

// 组件销毁时取消动画onUnmounted(() => { cancelAnimationFrame(state.rAF)})

完整代码

<template> {{ state.displayValue }}</template><script setup> // vue3.2新的语法糖, 编写代码更加简洁高效import { onMounted, onUnmounted, reactive } from "@vue/runtime-core";import { watch, computed } from 'vue';import { requestAnimationFrame, cancelAnimationFrame } from './requestAnimationFrame.js'// 定义父组件传递的参数const props = defineProps({ start: { type: Number, required: false, default: 0 }, end: { type: Number, required: false, default: 0 }, duration: { type: Number, required: false, default: 5000 }, 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; } }})const isNumber = (val) => { return !isNaN(parseFloat(val))}// 格式化数据,返回想要展示的数据格式const formatNumber = (val) => { val = val.toFixed(props.default) val += '' const x = val.split('.') let x1 = x[0] const x2 = x.length > 1 ? props.decimal + x[1] : '' const rgx = /(\d+)(\d{3})/ if (props.separator && !isNumber(props.separator)) { while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + props.separator + '$2') } } return props.prefix + x1 + x2 + props.suffix}// 相当于vue2中的data中所定义的变量部分const state = reactive({ localStart: props.start, displayValue: formatNumber(props.start), printVal: null, paused: false, localDuration: props.duration, startTime: null, timestamp: null, remaining: null, rAF: null})// 定义一个计算属性,当开始数字大于结束数字时返回trueconst stopCount = computed(() => { return props.start > props.end})// 定义父组件的自定义事件,子组件以触发父组件的自定义事件const emits = defineEmits(['onMountedcallback', 'callback'])const startCount = () => { state.localStart = props.start state.startTime = null state.localDuration = props.duration state.paused = false state.rAF = requestAnimationFrame(count)}watch(() => props.start, () => { if (props.autoPlay) { startCount() }})watch(() => props.end, () => { if (props.autoPlay) { startCount() }})// dom挂在完成后执行一些操作onMounted(() => { if (props.autoPlay) { startCount() } emits('onMountedcallback')})// 暂停计数const pause = () => { cancelAnimationFrame(state.rAF)}// 恢复计数const resume = () => { state.startTime = null state.localDuration = +state.remaining state.localStart = +state.printVal requestAnimationFrame(count)}const pauseResume = () => { if (state.paused) { resume() state.paused = false } else { pause() state.paused = true }}const reset = () => { state.startTime = null cancelAnimationFrame(state.rAF) state.displayValue = formatNumber(props.start)}const count = (timestamp) => { if (!state.startTime) state.startTime = timestamp state.timestamp = timestamp const progress = timestamp - state.startTime state.remaining = state.localDuration - progress // 是否使用速度变化曲线 if (props.useEasing) { if (stopCount.value) { state.printVal = state.localStart - props.easingFn(progress, 0, state.localStart - props.end, state.localDuration) } else { state.printVal = props.easingFn(progress, state.localStart, props.end - state.localStart, state.localDuration) } } else { if (stopCount.value) { state.printVal = state.localStart - ((state.localStart - props.end) * (progress / state.localDuration)) } else { state.printVal = state.localStart + (props.end - state.localStart) * (progress / state.localDuration) } } if (stopCount.value) { state.printVal = state.printVal < props.end ? props.end : state.printVal } else { state.printVal = state.printVal > props.end ? props.end : state.printVal } state.displayValue = formatNumber(state.printVal) if (progress < state.localDuration) { state.rAF = requestAnimationFrame(count) } else { emits('callback') }}// 组件销毁时取消动画onUnmounted(() => { cancelAnimationFrame(state.rAF)})</script>总结

自己封装数字动态效果需要注意各个浏览器直接的差异,手动pollyfill,暴露出去的props参数需要有默认值,数据的格式化可以才有正则表达式的方式,组件的驱动必须是数据变化,根据数据来驱动页面渲染,防止页面出现卡顿,不要强行操作dom,引入的组件可以全局配置,后续组件可以服用,码字不易,请各位看官大佬多多支持,一键三连了~❤️❤️❤️

demo演示

后续的线上demo演示会放在 demo演示 完整代码会放在 个人主页

希望对vue开发者有所帮助~

个人简介:承吾工作年限:5年前端地区:上海个人宣言:立志出好文,传播我所会的,有好东西就及时与大家共享!
本文链接地址:https://www.jiuchutong.com/zhishi/299054.html 转载请保留说明!

上一篇:2023年前端开发趋势未来可期(2023年前端开发找工作好找吗)

下一篇:如何通过nodejs快速搭建一个服务器(nodejs如何使用)

  • 怎样分析检查你的网站是否健康(怎样分析检查你是否怀孕)

    怎样分析检查你的网站是否健康(怎样分析检查你是否怀孕)

  • 如何清理手机(如何清理手机内存隐藏垃圾)

    如何清理手机(如何清理手机内存隐藏垃圾)

  • 华为nova7pro支持wifi6吗(华为nova7pro支持无线充电吗)

    华为nova7pro支持wifi6吗(华为nova7pro支持无线充电吗)

  • iphone xs max屏幕发黄怎么办(iphonexsmax屏幕死机)

    iphone xs max屏幕发黄怎么办(iphonexsmax屏幕死机)

  • 微博主页没有相册选项(微博个人主页看不到相册)

    微博主页没有相册选项(微博个人主页看不到相册)

  • 华为mate30pro充电指示灯(华为mate30pro充电不稳定)

    华为mate30pro充电指示灯(华为mate30pro充电不稳定)

  • lofter加载不出来(lofter突然加载不出来)

    lofter加载不出来(lofter突然加载不出来)

  • 充电宝膨胀了怎么处理(充电宝膨胀了怎么扔)

    充电宝膨胀了怎么处理(充电宝膨胀了怎么扔)

  • 苹果se2电池容量多大(苹果SE2电池容量多少毫安)

    苹果se2电池容量多大(苹果SE2电池容量多少毫安)

  • 华为闹钟图标怎么取消(华为闹钟图标怎么改)

    华为闹钟图标怎么取消(华为闹钟图标怎么改)

  • anc主动降噪什么意思(主动降噪anc enc)

    anc主动降噪什么意思(主动降噪anc enc)

  • b站怎么修改实名认证(b站实名认证)

    b站怎么修改实名认证(b站实名认证)

  • 手机快手直播怎么清屏弹幕(手机快手直播怎么投屏到电视上)

    手机快手直播怎么清屏弹幕(手机快手直播怎么投屏到电视上)

  • iphone如何拒接电话(iphone怎么拒接电话按钮)

    iphone如何拒接电话(iphone怎么拒接电话按钮)

  • 惠普1007用什么硒鼓(惠普1007用什么反骨)

    惠普1007用什么硒鼓(惠普1007用什么反骨)

  • word文档里字间距怎么调(word文档里字间距怎么对齐)

    word文档里字间距怎么调(word文档里字间距怎么对齐)

  • 手机ps怎么换背景(怎么用手机ps换背景)

    手机ps怎么换背景(怎么用手机ps换背景)

  • 手机qq看点里怎么发视频(手机qq看点怎么看文章)

    手机qq看点里怎么发视频(手机qq看点怎么看文章)

  • 苹果x拨号声音怎么关(苹果拨号声音怎么调大)

    苹果x拨号声音怎么关(苹果拨号声音怎么调大)

  • 华为nova5是什么系统(华为nova5i多少钱)

    华为nova5是什么系统(华为nova5i多少钱)

  • 华为手环3pro使用方法(华为手环3pro使用时间长)

    华为手环3pro使用方法(华为手环3pro使用时间长)

  • 笔记本一开机正在休眠(笔记本开机正在扫描和修复驱动器)

    笔记本一开机正在休眠(笔记本开机正在扫描和修复驱动器)

  • 西瓜视频如何直播游戏(西瓜视频如何直播)

    西瓜视频如何直播游戏(西瓜视频如何直播)

  • Windows 10关闭相机访问权限(win10怎么关闭相机)

    Windows 10关闭相机访问权限(win10怎么关闭相机)

  • 利用LSTM实现预测时间序列(股票预测)(lstm输出多个预测值)

    利用LSTM实现预测时间序列(股票预测)(lstm输出多个预测值)

  • 北京增值税发票勾选认证平台
  • 未取得房屋产权证租赁
  • 税务uk开票人显示是管理员怎么改
  • 以前年度损益调整账务处理分录
  • 百旺税控盘汇总表怎么看
  • 季末资产总额填错了要紧吗
  • 银行存款一直没动会怎样
  • 贸易公司退税怎么做账
  • 2016年营改增后18个税种,第一大税种是()
  • 一张合同分三次收款怎么开票?
  • 企业哪些费用属于重要费用
  • 理财产品增值税纳税人
  • 印花税在什么情况下需要申报
  • 前期差错更正处理方法
  • 异地施工预缴税款会计分录
  • 存货盘亏计入什么科目批准后
  • 小公司发工资怎么做账
  • 总包劳务工资发什么科目
  • 资本公积转增股本会计处理
  • 退货入库流程图
  • 个人独资企业出资额是注册资本吗
  • 百旺税控服务器管理系统
  • 行政单位无形资产入账标准
  • 外籍人员取得数月奖金怎么交税
  • 企业所得税中的资产总额怎么填
  • 上级拨入资金计入什么科目
  • 网件R6400路由器怎么样?网件R6400上网与传输评测的教程
  • windows hosts文件在哪
  • 增资后持股比例怎么算
  • 几种方法解决一个问题的架构图怎么画
  • u启动怎么装机
  • awk命令怎么用
  • 中投公司投资的股票
  • PHP:ftp_rename()的用法_FTP函数
  • PHP:http_build_query()的用法_url函数
  • php composer自动加载
  • php图片拼接
  • latex希腊字母斜体
  • 支付税控服务费的账务处理
  • vue treegrid
  • tf fans club
  • 持续ping ip的命令
  • 在建工程减值准备借贷方向
  • 建筑企业其他应付款
  • 不具备独立核算条件的行政单位
  • 织梦官方网站
  • 企业发行债券的交易费用计入
  • 什么时候过路费减半收费
  • 一般纳税人技术服务费几个点
  • 以旧换新有发票抵扣吗
  • 初始化mysql命令
  • 个体工商户税务注销流程
  • 固定资产盘亏所得税清算时怎么处理
  • 收到第三方补助怎么做账
  • 汽车属于固定资产类吗
  • 工业企业出售产品应交的消费税额,应计入营业成本
  • 给员工发工资发多少合适?
  • 事业单位工会经费账务处理
  • 收购农产品没有发票
  • 怎样计算存款利息?
  • sql中case when的用法
  • windows7无法进入睡眠
  • solaris root密码过期
  • 去掉快捷功能
  • 详细介绍的英文
  • win10安装 升级
  • linux安装php7.3
  • 电脑导航阻止怎么办
  • javascript基础教程教材答案
  • javascript教程推荐知乎
  • 个人简历
  • perl @inc
  • rapidjson使用
  • listview点击获取内容
  • php开机启动
  • NGUI学习笔记汇总
  • md5加密python
  • 中国有多少人2022
  • 法人没有实名认证,现在要变更,还需要实名认证吗
  • 海淀九所税务局电话
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设