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

  • 苹果手机怎么更新ios15系统(苹果手机怎么更新软件)

    苹果手机怎么更新ios15系统(苹果手机怎么更新软件)

  • 微信二维码支付设置密码的方法是什么(微信二维码支付限额多少)

    微信二维码支付设置密码的方法是什么(微信二维码支付限额多少)

  • 苹果11铃声如何设置自己的歌(苹果11铃声如何设置渐强)

    苹果11铃声如何设置自己的歌(苹果11铃声如何设置渐强)

  • 微信显示变成一个通知怎么办(微信显示一横是什么意思)

    微信显示变成一个通知怎么办(微信显示一横是什么意思)

  • 全民k歌听不到自己唱(全民K歌听不到伴奏)

    全民k歌听不到自己唱(全民K歌听不到伴奏)

  • 王卡超级会员退订不了(王卡超级会员退了能重开吗)

    王卡超级会员退订不了(王卡超级会员退了能重开吗)

  • 抖音亲密值怎么涨得快(抖音亲密值在哪里)

    抖音亲密值怎么涨得快(抖音亲密值在哪里)

  • 抖音视频置顶有什么作用(抖音视频置顶有什么技巧吗)

    抖音视频置顶有什么作用(抖音视频置顶有什么技巧吗)

  • 辽事通能定位吗

    辽事通能定位吗

  • 陌陌几天可以视频(陌陌几天可以视频聊天)

    陌陌几天可以视频(陌陌几天可以视频聊天)

  • 手机ui未兼容什么意思(上海代办宠物检疫证明)

    手机ui未兼容什么意思(上海代办宠物检疫证明)

  • vivo恢复出厂设置后,激活需要密码怎么办(vivo恢复出厂设置在哪)

    vivo恢复出厂设置后,激活需要密码怎么办(vivo恢复出厂设置在哪)

  • 为何苹果不能下载迅雷(为何苹果不能下载微信)

    为何苹果不能下载迅雷(为何苹果不能下载微信)

  • iphone6电池容量(iphone6电池容量多大)

    iphone6电池容量(iphone6电池容量多大)

  • 文档中字间距怎么调整(文档字间距怎么统一)

    文档中字间距怎么调整(文档字间距怎么统一)

  • ipadair1和air2区别(ipadair2跟air1的区别)

    ipadair1和air2区别(ipadair2跟air1的区别)

  • 抖音如何设置不让合拍(抖音如何设置不把自己的好友推荐给别人)

    抖音如何设置不让合拍(抖音如何设置不把自己的好友推荐给别人)

  • iphone11怎么开通电信volte(iPhone11怎么开通公交卡)

    iphone11怎么开通电信volte(iPhone11怎么开通公交卡)

  • ios13来电闪光灯在哪里(苹果13系统设置来电闪光灯)

    ios13来电闪光灯在哪里(苹果13系统设置来电闪光灯)

  • airpods的触点在哪(airpods 触点)

    airpods的触点在哪(airpods 触点)

  • 新苹果11如何使用(新苹果11怎样使用)

    新苹果11如何使用(新苹果11怎样使用)

  • 华为手机日历在哪里设置(华为手机日历在桌面显示)

    华为手机日历在哪里设置(华为手机日历在桌面显示)

  • 8p基带是什么(8p基带芯片在哪)

    8p基带是什么(8p基带芯片在哪)

  • iphone无线耳机改名字(苹果耳机改蓝牙教程)

    iphone无线耳机改名字(苹果耳机改蓝牙教程)

  • phpcms 验证码验证失败怎么办(php手机验证码验证)

    phpcms 验证码验证失败怎么办(php手机验证码验证)

  • 出租房屋转为投资房屋
  • 实验耗材计入什么科目
  • 限定性净资产账务处理
  • 收不回来的装修钱怎么办
  • 年终奖12月份计提少了
  • 模具发票如何入账
  • 金蝶专业版二级科目设置
  • 完税证明必须本人办理吗
  • 企业清算业务程序
  • 增值税认证逾期怎么处理
  • 补提折旧调整分录
  • 应付账款借方余额在资产负债表中怎么列示
  • 事业单位为职工代扣代缴个人所得税
  • 更正申报所得税流程
  • 固定资产抵扣期限
  • 车间房屋维修费属于什么科目
  • 已付款未收到发票怎么做分录
  • 个人账户付款可以开公司发票吗
  • 企业预缴所得税的比例要求
  • 有限合伙企业无限合伙企业
  • 分包与转包的区别 法院
  • 报税显示未进行抄报税
  • 个人出租住房增值税免税政策
  • 公司账上没钱还假发工资违法吗
  • 公司购买床垫怎样入账
  • 签订旅游合同的意义
  • 股份支付的会计处理?
  • 房地产商铺增值税税率是多少
  • 转账和电汇哪个便宜
  • 金针菜的养殖方法和技术
  • kb4586863更新
  • 苹果系统公测版
  • 代购机票骗局
  • 其他应付款转营业外收入需要交增值税吗
  • 承兑托收费用
  • 银行卡透支怎么还
  • 法定盈余公积金达到注册资本的多少时不再提取
  • laravel 分层
  • 以权益结算的股票
  • 在Yii2特定页面如何禁用调试工具栏Debug Toolbar详解
  • thinkphp 多数据库
  • cobit框架
  • 小规模纳税人减免增值税会计处理
  • 冲红的销项税怎么结转
  • yolov8训练自己的数据集 Windows
  • 开源代码网站github
  • php判断字符串是否为ip地址
  • blat命令
  • 金税盘到上传截止日期
  • 企业日常费用支出明细表
  • 进货该怎么进
  • antd pro v5
  • 罚款可以直接在12123
  • 捐赠支出汇算清缴需要调增吗
  • sql server遇到的主要问题及解决方法
  • 销售送客户礼物
  • 加工费发票可以抵扣吗
  • 计提待摊费用分录
  • 外币资本金入账汇率怎样选择
  • 承兑汇票需要做账吗
  • 从业人数和资产总额指标,应按企业
  • sql server2019数据库
  • 如何获取数据库的sid
  • sql注入式攻击中单引号的作用
  • Windows Server 2008如何设置自动获取ip?设置自动获取ip教程
  • 简述linux几种主流发行版本及其特点
  • linux谷歌浏览器安装指令
  • centos6.5mini安装教程
  • win8如何使用
  • RedHatLinux AS3中APACHE+SendMail+OpenWebMail整合
  • win7不能自动启动
  • cocos2dx drawcall优化
  • 手机背光面板
  • unity如何成一组
  • javascript实现3D切换焦点图
  • 用python做一个贪吃蛇
  • 青岛的红叶什么时候红
  • 税务注销相关文件
  • 电子税务局开发票流程
  • 徐州交社保有户口限制吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设