位置: 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如何使用)

  • vivox70pro怎么分屏(vivox70pro怎么分辨真假)

    vivox70pro怎么分屏(vivox70pro怎么分辨真假)

  • vivo手机老年模式怎么开启(vivo手机老年模式怎么打开设置)

    vivo手机老年模式怎么开启(vivo手机老年模式怎么打开设置)

  • 腾讯会议操作过于频繁,请稍后再试(腾讯会议操作过于)

    腾讯会议操作过于频繁,请稍后再试(腾讯会议操作过于)

  • 视频删除了怎么恢复回来(视频删除了怎么免费找回来)

    视频删除了怎么恢复回来(视频删除了怎么免费找回来)

  • 微博下载的安装包怎么删除(微博下载的安装怎么删除)

    微博下载的安装包怎么删除(微博下载的安装怎么删除)

  • 华为荣耀10青春版怎么录屏(华为荣耀10青春版有红外线功能吗)

    华为荣耀10青春版怎么录屏(华为荣耀10青春版有红外线功能吗)

  • qq共同好友1个是自己吗

    qq共同好友1个是自己吗

  • 微信运动每天几点更新(微信运动每天几点推送消息)

    微信运动每天几点更新(微信运动每天几点推送消息)

  • 魅族m813q是什么型号(魅族m871q是什么手机)

    魅族m813q是什么型号(魅族m871q是什么手机)

  • 大写锁定已打开是什么意思(大写锁定已打开卡在桌面)

    大写锁定已打开是什么意思(大写锁定已打开卡在桌面)

  • 2560x1600分辨率高吗(2560x1600分辨率高吗笔记本)

    2560x1600分辨率高吗(2560x1600分辨率高吗笔记本)

  • 开发者选项长期开启会怎样(开发者选项长期开启有什么危害)

    开发者选项长期开启会怎样(开发者选项长期开启有什么危害)

  • 电脑无信号开不了机(电脑无信号开不了机主机还在运转)

    电脑无信号开不了机(电脑无信号开不了机主机还在运转)

  • 集成运算放大器中间级的主要特点是(集成运算放大器的特点)

    集成运算放大器中间级的主要特点是(集成运算放大器的特点)

  • 微信逻辑错误登录不了怎么处理(微信号登录显示逻辑错误)

    微信逻辑错误登录不了怎么处理(微信号登录显示逻辑错误)

  • 苹果11听筒也是扬声器吗(苹果11的通病听筒)

    苹果11听筒也是扬声器吗(苹果11的通病听筒)

  • ipad a1701是什么型号(ipad a1707是什么型号)

    ipad a1701是什么型号(ipad a1707是什么型号)

  • hdq什么意思(网络语hdq是什么意思)

    hdq什么意思(网络语hdq是什么意思)

  • 如何找回删除的视频和照片(如何找回删除的文件)

    如何找回删除的视频和照片(如何找回删除的文件)

  • 苹果x用的什么基带(苹果x用的什么处理器)

    苹果x用的什么基带(苹果x用的什么处理器)

  • 16d57是什么型号(16d57是什么版本)

    16d57是什么型号(16d57是什么版本)

  • 手机qq下载的视频在哪个文件夹(手机QQ下载的视频在哪个文件夹)

    手机qq下载的视频在哪个文件夹(手机QQ下载的视频在哪个文件夹)

  • 多种方法解决前后端报出的SyntaxError: xxx is not valid JSON的问题,比如“[object Object]“ is not valid JSON(有什么办法解决前进中的问题)

    多种方法解决前后端报出的SyntaxError: xxx is not valid JSON的问题,比如“[object Object]“ is not valid JSON(有什么办法解决前进中的问题)

  • css 实现虚线效果的3种方式详解(cssborder虚线边框)

    css 实现虚线效果的3种方式详解(cssborder虚线边框)

  • dedecms调用当前文章所属栏目名(dedecms进入数据库)

    dedecms调用当前文章所属栏目名(dedecms进入数据库)

  • 什么是保函业务?如何进行核算?
  • 建设工程劳务分包的规定
  • 货物运输费用怎么算
  • 办税人员绑定企业审核谁审核
  • 销项税普票
  • 收到银行转来的进账通知单,上月的销货款
  • 认缴注册资本的风险
  • 二手车销售统一专票图片
  • 税收的三个基本要素是
  • 股权转让是把公司卖了吗
  • 收外汇需要提供什么
  • 自查增值税补缴怎么处理
  • 当地预缴2%什么时候缴纳
  • 小规模外贸公司
  • 定额发票领用日期
  • 继承的房产出售要交20%是全额还是差额
  • 税控盘开票显示操作未授权
  • 购买健身器材需要注意什么
  • 股东借款利息计入利润表哪个科目
  • 劳务报酬和个人工资的区别
  • 苹果手机14pro max
  • 小规模计提增值税的会计科目
  • 微软发布新的免费 Win11 虚拟机 (2302)
  • 进项税和销项税怎么理解
  • 在win10中显示我的电脑
  • Win11 Build 22449.1000更新里哪些内容?Win11 Build 22449更新介绍与安装方法
  • 现在我们来看看windows中的新增内容
  • 投标保证金退回是什么意思
  • kpk是什么文件
  • 乌鲁米耶湖春季湖水更深
  • php动态页面实例
  • 借入长期借款的利息
  • 免购车税政策
  • 科罗拉多州位置
  • 如何开启framework 3.5
  • 推荐国内免费使用的电影
  • 定额发票累计领用金额怎么填
  • 会计期初余额和期末余额计算公式
  • 2021前端面试大全
  • 前端手撕代码
  • 定期定额征收超出3万怎么办
  • 采购的原材料无保质期
  • 会计实务中的计提是什么意思
  • 文化事业建设费的征收范围
  • 会计怎样审核报销凭证
  • mysqljoin和where哪个好
  • php安装不上
  • 资产负债表应交税费为负数
  • 投资款未备注
  • 收到政府扶贫款如何做分录
  • 企业研发费用资本化相关公司
  • 月销售额10万以下一般纳税人免征增值税
  • 固定资产折旧指标有哪些
  • 交强险和车船税在哪里买
  • 应付职工薪酬的工资是实发工资还是应发工资
  • 什么费用可以列入研发费用
  • 以前年度损益调整结转到本年利润吗
  • 餐饮打包盒 标准
  • 企业收到待清算商户款项做什么分录
  • 进口料件内销的关税和增值税怎么计算
  • 企业筹建期间开办费计入
  • sql server233错误
  • win8安装虚拟机的步骤
  • win10屏幕显示
  • win7电脑怎么设置
  • win8系统怎样关机
  • mac查看地址
  • win10玩上古世纪闪退
  • css行与行之间的间距怎么调
  • python模块和类和方法
  • JavaScript的setter与getter方法
  • Linux删除大量文件
  • linux中tar
  • unity教程完整版
  • js 单击弹出对话框
  • python抢红包
  • python运行出现none
  • jquery解析XML及获取XML节点名称的实现代码
  • 纳税申报过了申报期未申报怎么办
  • 上海房产税2021征收对象
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设