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

  • 支付宝怎么导出一个人的转账记录(支付宝怎么导出核酸检测报告)

    支付宝怎么导出一个人的转账记录(支付宝怎么导出核酸检测报告)

  • 哔哩哔哩怎么绑定微信QQ(哔哩哔哩怎么绑定邮箱)

    哔哩哔哩怎么绑定微信QQ(哔哩哔哩怎么绑定邮箱)

  • 苹果的圆点在哪设置(苹果的圆点在哪里找)

    苹果的圆点在哪设置(苹果的圆点在哪里找)

  • jkm-aloo华为手机什么型号(jkmaloo华为手机价格)

    jkm-aloo华为手机什么型号(jkmaloo华为手机价格)

  • 剪线键盘什么意思(剪线键盘是哪里来的)

    剪线键盘什么意思(剪线键盘是哪里来的)

  • word尺子怎么调出(word尺子怎么调出厘米)

    word尺子怎么调出(word尺子怎么调出厘米)

  • procreate支持的ipad型号(procreate支持的ipadmini4吗)

    procreate支持的ipad型号(procreate支持的ipadmini4吗)

  • mac地址通常储存在(MAC地址通常储存在计算机的)

    mac地址通常储存在(MAC地址通常储存在计算机的)

  • qqwifi在线是什么意思(qq上wifi在线是说明那个人在玩手机吗)

    qqwifi在线是什么意思(qq上wifi在线是说明那个人在玩手机吗)

  • qq字体怎么改成默认字体(qq字体怎么改)

    qq字体怎么改成默认字体(qq字体怎么改)

  • vivo设置个人来电铃声(vivo手机怎么设置一个人的来电铃声)

    vivo设置个人来电铃声(vivo手机怎么设置一个人的来电铃声)

  • 拼多多怎么取消全部收藏(拼多多怎么取消先用后付款)

    拼多多怎么取消全部收藏(拼多多怎么取消先用后付款)

  • p30微信照片在哪(华为p30微信照片保存在哪里)

    p30微信照片在哪(华为p30微信照片保存在哪里)

  • 恋爱记app怎么解除关系(恋爱记app怎么解除关系卡情侣)

    恋爱记app怎么解除关系(恋爱记app怎么解除关系卡情侣)

  • 获取手机imsi的方法(电话卡的imsi怎么获取)

    获取手机imsi的方法(电话卡的imsi怎么获取)

  • 什么是双频gps(什么是双频gps手机型号)

    什么是双频gps(什么是双频gps手机型号)

  • vivo关闭全局搜索(关闭vivo全局搜索)

    vivo关闭全局搜索(关闭vivo全局搜索)

  • wps ppt如何去掉图片底色(wpsppt如何去掉日期)

    wps ppt如何去掉图片底色(wpsppt如何去掉日期)

  • 抖音实名认证后能干嘛(抖音实名认证后注销了还能再注册吗)

    抖音实名认证后能干嘛(抖音实名认证后注销了还能再注册吗)

  • 苹果6s微信怎么更新(苹果6s微信怎么分身)

    苹果6s微信怎么更新(苹果6s微信怎么分身)

  • 手机b站缓存视频在哪里(手机b站缓存视频怎么导出)

    手机b站缓存视频在哪里(手机b站缓存视频怎么导出)

  • 野生动物保护区中的沙丘鹤和野鸭,美国新墨西哥州 (© Cathy & Gordon Illg/Jaynes Gallery/DanitaDelimont.com)(野生动物保护区有哪些)

    野生动物保护区中的沙丘鹤和野鸭,美国新墨西哥州 (© Cathy & Gordon Illg/Jaynes Gallery/DanitaDelimont.com)(野生动物保护区有哪些)

  • 【今日中秋】 (© VCG/VCG via Getty Images)(今天中秋节)

    【今日中秋】 (© VCG/VCG via Getty Images)(今天中秋节)

  • vue3项目实战中的接口调用方法(一)async/await用法 对axios二次封装 实现异步请求(vue3项目搭建)

    vue3项目实战中的接口调用方法(一)async/await用法 对axios二次封装 实现异步请求(vue3项目搭建)

  • 【node进阶】深入浅出websocket即时通讯(一)(node深入浅出pdf)

    【node进阶】深入浅出websocket即时通讯(一)(node深入浅出pdf)

  • 文化事业建设费减免政策
  • 评估增值对净利有影响吗
  • 取得研发样品收入
  • 小规模转一般纳税人需要什么条件
  • 当月有进项无销项月末怎么处理
  • 金蝶k3现金流量明细查询
  • 工会经费返还怎么使用
  • 新公司注册资金需要实缴吗
  • 增值税专用发票和普通发票的区别
  • 资产负债表存货为负数原因
  • 增值税普通发票怎么开
  • 没有及时取得发票可以入成本么
  • 境外企业提供国外服务
  • 出口退税退的是进口时的税吗
  • 12月份的收入1月份开具发票,报税时免税吗
  • 公司购买饮水机的申请
  • 重新建账要以前的期初余额吗
  • 开专票还需要提供营业执照?
  • 物业公司代收供暖费,可以开发票吗
  • 房地产企业如何计算增值税
  • 附加税减半征收计提和缴纳的会计分录
  • 融资租入固定资产的改建支出计入什么科目
  • 收到股东交的多余的股金怎么做账务处理
  • 保险公司的税收是多少
  • 利润表里的营业成本包括哪些
  • 公司生产的产品
  • 如何在win10中同步我的设置
  • 简易计税 科目
  • PHP编程中的__clone()方法使用详解
  • linux shell语句
  • widows11预览版
  • 企业无偿提供劳务
  • 销售商品的会计分录已收到钱
  • 北极光下的众神图片
  • 微信小程序登录后端
  • 企业和银行未达账项
  • SSD目标检测算法
  • php使用for循环实现乘法口诀表
  • python中模块的用法
  • 应交税费转出会计分录
  • 核销对哪些单据对应关系进行的操作
  • 增值税发票2年了还能开吗
  • 购买工业用地
  • 税控盘的作用是什么
  • 免征文化事业建设费条件的销售额标准
  • 公司首次申报个人所得税
  • 餐厅餐具如何使用
  • 以前年度损益调整怎么做账
  • 对外担保的效力
  • 买入返售金融资产属于金融资产吗
  • 建筑行业怎么确认收入
  • 代发工资怎么算税
  • 坏账确认无法收回
  • 出口报关金额怎么算
  • 借款跨年要交个税吗
  • 上月有留抵税额本月怎么申报
  • 没有公章的发票
  • 会计总账怎么登账
  • 仲裁是什么意思举个例子
  • 启用账簿时应在账簿上签名或盖章的是
  • sqlserver按时间段导出数据
  • mysim和innodb
  • Win10系统怎么进入控制面板
  • 系统审核策略配置
  • 电脑ems是什么意思啊
  • win8cp
  • win8电脑触摸屏没反应怎么办
  • macbookpro鼠标触控板
  • ghoststarttrayapp.exe是什么进程 有什么作用 ghoststarttrayapp进程查询
  • linux修改服务器ip地址
  • vim配置语法高亮
  • nodejs使用express如何跨域
  • python线程池最大数量
  • 详细解读了
  • 深入理解android卷1 pdf
  • 深入理解新发展理念,推进供给侧结构性改革 心得体会
  • 贵州省地方税务局房地产税收征收管理办法
  • 什么东西的海关不能寄
  • 国家税务总局辽宁省税务局
  • 江苏省纳税信息查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设