• 动手吧,vue数字动画


    数字动画,有数字的地方都能用上,拿去吧!

    效果:

    1、template部分

    1. <template>
    2. <div class="v-count-up">{{ dispVlaue }}div>
    3. template>

    2、js部分

    1. export default {
    2. data() {
    3. return {
    4. timer: "",
    5. calcValue: 0,
    6. startTime: 0,
    7. remaining: 0,
    8. raf: "",
    9. };
    10. },
    11. props: {
    12. value: Number,
    13. startValue: {
    14. type: Number,
    15. default: 0,
    16. },
    17. time: {
    18. type: Number,
    19. default: 5000,
    20. },
    21. useease: {
    22. type: Boolean,
    23. default: true,
    24. },
    25. tofixed: {
    26. type: Number,
    27. default: 0,
    28. },
    29. separator: {
    30. type: Boolean,
    31. default: false,
    32. },
    33. reset: {
    34. type: Boolean,
    35. default: false,
    36. },
    37. },
    38. computed: {
    39. dispVlaue() {
    40. const calcValue = (this.calcValue || 0).toFixed(this.tofixed);
    41. return this.separator ? this.formatNumber(calcValue) : calcValue;
    42. },
    43. startVal() {
    44. return this.startValue > this.value ? this.value : this.startValue;
    45. },
    46. },
    47. created() {
    48. this.start();
    49. },
    50. watch: {
    51. reset() {
    52. this._reset();
    53. },
    54. },
    55. methods: {
    56. count(timestamp) {
    57. if (!this.startTime) {
    58. this.startTime = timestamp;
    59. }
    60. let progress = timestamp - this.startTime;
    61. this.remaining = this.time - progress;
    62. if (this.useease) {
    63. this.calcValue = this.easeOutExpo(
    64. progress,
    65. this.startVal,
    66. this.value - this.startVal,
    67. this.time
    68. );
    69. } else {
    70. this.calcValue =
    71. this.startVal + (this.value - this.startVal) * (progress / this.time);
    72. }
    73. if (this.calcValue > this.value) {
    74. this.calcValue = this.value;
    75. }
    76. if (progress < this.time) {
    77. this.raf = requestAnimationFrame(this.count);
    78. } else {
    79. this.$emit("end");
    80. cancelAnimationFrame(this.raf);
    81. }
    82. },
    83. start() {
    84. if (this.time > 0) {
    85. this.raf = requestAnimationFrame(this.count);
    86. } else {
    87. this.printValue();
    88. }
    89. },
    90. _reset() {
    91. this.startTime = 0;
    92. this.calcValue = 0;
    93. this.remaining = 0;
    94. this.start();
    95. },
    96. easeOutExpo(t, b, c, d) {
    97. return (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b;
    98. },
    99. printValue() {
    100. this.calcValue = this.value;
    101. },
    102. formatNumber(num) {
    103. var result, x, x1, x2, x3;
    104. result = String(num);
    105. x = result.split(".");
    106. x1 = x[0];
    107. x2 = x.length > 1 ? "." + x[1] : "";
    108. if (this.separator) {
    109. x3 = "";
    110. for (var i = 0, len = x1.length; i < len; ++i) {
    111. if (i !== 0 && i % 3 === 0) {
    112. x3 = "," + x3;
    113. }
    114. x3 = x1[len - i - 1] + x3;
    115. }
    116. x1 = x3;
    117. }
    118. return x1 + x2;
    119. },
    120. },
    121. destroyed() {
    122. cancelAnimationFrame(this.raf);
    123. },
    124. };

  • 相关阅读:
    Spring事务和事务传播机制
    开源LC3编解码器测试Demo
    全球浪涌保护和雷击保护装置行业总体规模、主要厂商及IPO上市调研报告,2023-2029
    Java 基础之锁
    极验--一键通过模式逆向分析
    基于ssm实验室管理系统
    记录配置VS,使用opencv与Eigen
    redis的原理和源码-数据过期expire的介绍
    Mysql主从搭建
    UG\NX二次开发 关闭显示,隐藏创建过程,提高运行效率
  • 原文地址:https://blog.csdn.net/weixin_44708870/article/details/133160353