Adinnet-Web Adinnet-Web
首页
  • 技术沉淀

    • 技术沉淀
  • 前端文章

    • JavaScript
  • 前端笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 实用技巧
收藏
  • 分类
  • 标签
  • 归档
首页
  • 技术沉淀

    • 技术沉淀
  • 前端文章

    • JavaScript
  • 前端笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 实用技巧
收藏
  • 分类
  • 标签
  • 归档
  • JavaScript文章

  • 学习笔记

  • 技术沉淀

    • CSS矩形绘制
    • 仿微信通讯录
    • 微信小程序订阅消息
    • 保存海报
    • 微信小程序页面弹框
    • 微信小程序在指定距离范围内签到
    • h5嵌套u3d,使用iframe进行交互
    • 前端主导文件处理方法
    • 图片压缩方法封装
    • 金额大写函数
    • 前端主导的小程序一键导航功能
    • uniapp富文本组件(只支持H5)
    • uniapp二次确认弹框组件(依赖于uview)
    • 无缝滚动插件(vue3)
    • H5与unity通信(只针对unity嵌套H5)
    • 辅助触控
    • 关系图
    • h5&svg点赞动效
    • iframe高精地图文件预览
    • 自适应横向瀑布流布局
    • 考试功能
    • 仿小说翻页
    • 富文本
    • h5唤醒App
    • vue3 Count-to数字翻动
      • 小程序图片转base64
      • 第三方应用跳转微信小程序
      • 圆角TabBar
      • 大屏Header
      • Echarts中国地图
      • 使用computed拦截v-model
      • 消息订阅与发布
      • Html相对路径页面跳转
      • Vite打包本地访问
      • H5主题切换
      • 移动端列表分页
      • 每次进入页面都触发的功能(v3)
      • 智能导诊
      • js中几个优雅的运算符使用技巧
      • 其他CSS伪元素
      • 微信浏览器保存canvas海报
      • uniappH5手动拖拽缩放自定义绘制海报
      • uniapp小程序盒子拖拽排序
      • 微信小程序地图的使用
      • css静态漏斗绘制
      • 微信小程序图片流处理
      • ios时间问题
      • 文字围绕效果
      • 表格树动态计算
      • React中useEffect用法
      • React Hooks 陷阱
      • Naive UI修改默认样式
      • vue 根据div id 滚动到指定view到可视视图中
      • js将[1,2,3]排列组合成[1.12.123]
      • 移动端如何加载vite导出dist包
      • TS探索接口、泛型和自定义类型
      • WebSocket中加入Token
      • 幂方程指数衰减
      • pageSpy
      • 表格多条件搜索
      • axios get 请求 url 转码 空格转成+,导致请求失败
      • NaiveUI使用Message组件遇到的问题记录
      • 小程序多盒子拖拽排序
      • 对象快速筛key方法
      • 倒计时
      • nuxt3中的useAsyncData使用详解
      • nuxt3文件式路由
      • 播放amr格式音频
      • echarts瀑布图
      • vue中使用vueuse进行文件导出
      • 分享一个数据整理方法
      • naiveui表格shift多选
      • 数组递归转tree
      • 自定义指令图片懒加载
      • 将数组里面的对象转换成我们需要的键值对
      • 亚马逊云s3上传
      • 上拉加载更多封装
      • JS设计模式总结笔记
      • 适配移动端页面顶部安全区域
      • 【CryptoJS】使用介绍
      • 微信小程序富文本图片点击放大
      • 微信小程序省市区级联选择
      • uniapp拖动排序插件
      • element ui 的树形结构懒加载,局部更新
      • 表单搜索过滤空值
      • 判断设备类型
      • tsconfig配置打包问题
      • vant-uploader上传组件添加长按保存图片
      • BroadcastChannel跨页面通信
      • pnpm安装问题
      • echart阶梯图形绘制
      • js根据对象拼接参数
      • 集成免密登录
      • 输入框输入数字限制
      • js控制全局css变量
      • 流程图
      • 修改npm包
      • tsx在vue3中的应用
      • 109video播放视频流
      • echart默认展示某个点的tooltip
      • 大屏适配几种方案
      • 表单低代码自定义列表页
      • 表单低代码列表页配置端代码
      • 低代码平台使用的一些好用的js方法
      • vue3cookie的使用
      • 原生js下载文件
      • stompjs 创建socket,断线重连机制
      • iframe作为内嵌网页cookie设置
      • 前端对文件内容解析生成特定算法哈希值方法
      • vue3中前端自定义命名文件名称下载文件的方法
      • 前端项目中权限判断的方法配合路由守卫使用
      • 原生table:表格table中thead固定,tbody超出高度出现滚动条
      • vue中使用原生的方法
      • vue3中amchart的初步使用
      • base64图片加水印
      • 手写签名
      • base64转doc文件
    • 前端
    • 技术沉淀
    芦泓钢
    2023-07-28
    目录

    vue3 Count-to数字翻动

    # 效果

    参考链接 vue-count-to (opens new window)

    # 引入

    // 按需引入
    import CountTo from "@/components/CountTo.vue";
    // 全局引入main.js中
    import { createApp } from 'vue'
    import App from './App.vue'
    import CountTo from "@/components/CountTo.vue";
    const app = createApp(App)
    app.component('CountTo', CountTo)
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 使用

    <CountTo :start='0' :end="1000" :duration='2000'></CountTo>
    
    1

    # 组件源码

    <template>
      <span>{{ state.displayValue }}</span>
    </template>
    
    <script setup>
    import { onMounted, onUnmounted, reactive } from 'vue';
    import { watch, computed } from 'vue';
    import { requestAnimationFrame, cancelAnimationFrame } from '@/utils/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 => !isNaN(parseFloat(val));
    
    // 格式化数据,返回想要展示的数据格式
    const formatNumber = val => {
      val = val.toFixed(props.decimals);
      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
    });
    
    // 定义一个计算属性,当开始数字大于结束数字时返回true
    const stopCount = computed(() => 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>
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208

    # requestAnimationFrame.js

    /** CountTo数字滚动 **/
    let lastTime = 0
    const prefixes = 'webkit moz ms o'.split(' ') // 各浏览器前缀
    
    let requestAnimationFrame
    let 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 }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    上次更新: 2023/07/28, 11:17:55
    h5唤醒App
    小程序图片转base64

    ← h5唤醒App 小程序图片转base64→

    最近更新
    01
    base64转doc文件
    01-03
    02
    手写签名
    01-03
    03
    base64图片加水印
    01-03
    更多文章>
    Theme by Vdoing | Copyright © 2023-2025 Adinnet
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式