巴山夜雨涨秋池 發表於 2019-7-8 15:57:00

JavaScript 数字滚动countup.js

<h2>1. 概述</h2>
<h3>1.1 说明</h3>
<p style="margin-left: 30px">在项目过程中,有时候需要动态的去展示一些数据的加载状态,如一个数字为10000,需要5秒时间滚动加载完成。此时使用countup.js就能够很方便的处理此类功能问题。</p>
<h3>1.2 countup.js</h3>
<p style="margin-left: 30px">countup.js是一个无依赖性、轻量级的javascript类,可用于快速创建动画,以更有趣的方式显示数字/数据。详见countup.js</p>
<h3>1.3 countup.js使用</h3>
<p style="margin-left: 30px">&nbsp;<span style="color: rgba(51, 102, 255, 1)"><strong>npm install countup</strong></span>&nbsp; 进行安装依赖</p>
<p style="margin-left: 30px"><span style="color: rgba(51, 102, 255, 1)"><strong>import CountUp from "countup"</strong></span>&nbsp; 在页面中引入</p>
<p style="margin-left: 30px">new CountUp(target, startVal, endVal, decimals, duration, options)</p>
<p style="margin-left: 30px">参数:</p>
<ul>
<li style="list-style-type: none">
<ul>
<li>target: 目标元素的id&nbsp; *必填</li>
<li>startVal:开始的值(从哪个值开始)&nbsp; *必填</li>
<li>endVal:结束的值(滚动到哪个值结束)&nbsp; *必填</li>
<li>decimals:小数位数,默认值为0&nbsp; *可选</li>
<li>duration:动画持续时间,单位为秒,默认值为2&nbsp; *可选</li>
<li>options:选项的可选对象&nbsp; *可选
<ul>
<li style="list-style-type: none">
<ul>
<li>useEasing:true&nbsp; --是否使用缓动动画,默认为缓动,可设置为false让其匀速</li>
<li>useGrouping:true --对数字进行分组,如12345,按三位一组变为类似12,345这样的</li>
<li>separator: ','&nbsp; --分组时使用的分隔符默认是逗号</li>
<li>decimal: '.'&nbsp; --小数点</li>
<li>prefix: ''&nbsp; --添加前缀如12345,变为¥12345</li>
<li>suffix: ''&nbsp; --添加后缀如12345 通过添加后缀变为12345$,12345元之类的</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p style="margin-left: 30px">&nbsp;方法:</p>
<ul>
<li style="list-style-type: none">
<ul>
<li>暂停/恢复&nbsp; &nbsp; &nbsp;&nbsp;pauseResume</li>
<li>重置动画&nbsp; &nbsp; &nbsp; &nbsp;reset</li>
<li>更新值&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;update(newVal)</li>
</ul>
</li>
</ul>
<h2>&nbsp;2. 代码</h2>
<h3>2.1 源代码</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> CountUp = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(target, startVal, endVal, decimals, duration, options) {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> self = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    self.version </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> "1.9.2"<span style="color: rgba(0, 0, 0, 1)">
    };
    self.options </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      useEasing: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      useGrouping: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      separator: </span>","<span style="color: rgba(0, 0, 0, 1)">,
      decimal: </span>"."<span style="color: rgba(0, 0, 0, 1)">,
      easingFn: easeOutExpo,
      formattingFn: formatNumber,
      prefix: </span>""<span style="color: rgba(0, 0, 0, 1)">,
      suffix: </span>""<span style="color: rgba(0, 0, 0, 1)">,
      numerals: []
    };
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (options &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">typeof</span> options === "object"<span style="color: rgba(0, 0, 0, 1)">) {
      </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> key <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> self.options) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (options.hasOwnProperty(key) &amp;&amp; options !== <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                self.options </span>=<span style="color: rgba(0, 0, 0, 1)"> options
            }
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (self.options.separator === ""<span style="color: rgba(0, 0, 0, 1)">) {
      self.options.useGrouping </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      self.options.separator </span>= "" +<span style="color: rgba(0, 0, 0, 1)"> self.options.separator
    }
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> lastTime = 0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> vendors = ["webkit", "moz", "ms", "o"<span style="color: rgba(0, 0, 0, 1)">];
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> x = 0; x &lt; vendors.length &amp;&amp; !window.requestAnimationFrame; ++<span style="color: rgba(0, 0, 0, 1)">x) {
      window.requestAnimationFrame </span>= window + "RequestAnimationFrame"<span style="color: rgba(0, 0, 0, 1)">];
      window.cancelAnimationFrame </span>= window + "CancelAnimationFrame"] || window + "CancelRequestAnimationFrame"<span style="color: rgba(0, 0, 0, 1)">]
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">window.requestAnimationFrame) {
      window.requestAnimationFrame </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(callback, element) {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> currTime = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime();
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> timeToCall = Math.max(0, 16 - (currTime -<span style="color: rgba(0, 0, 0, 1)"> lastTime));
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> id = window.setTimeout(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
                callback(currTime </span>+<span style="color: rgba(0, 0, 0, 1)"> timeToCall)
            },
            timeToCall);
            lastTime </span>= currTime +<span style="color: rgba(0, 0, 0, 1)"> timeToCall;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">window.cancelAnimationFrame) {
      window.cancelAnimationFrame </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(id) {
            clearTimeout(id)
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> formatNumber(num) {
      num </span>=<span style="color: rgba(0, 0, 0, 1)"> num.toFixed(self.decimals);
      num </span>+= ""<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> x, x1, x2, x3, i, l;
      x </span>= num.split("."<span style="color: rgba(0, 0, 0, 1)">);
      x1 </span>= x;
      x2 </span>= x.length &gt; 1 ? self.options.decimal + x : ""<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.options.useGrouping) {
            x3 </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0, l = x1.length; i &lt; l; ++<span style="color: rgba(0, 0, 0, 1)">i) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i !== 0 &amp;&amp; ((i % 3) === 0<span style="color: rgba(0, 0, 0, 1)">)) {
                  x3 </span>= self.options.separator +<span style="color: rgba(0, 0, 0, 1)"> x3
                }
                x3 </span>= x1 +<span style="color: rgba(0, 0, 0, 1)"> x3
            }
            x1 </span>=<span style="color: rgba(0, 0, 0, 1)"> x3
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.options.numerals.length) {
            x1 </span>= x1.replace(//<span style="color: rgba(0, 0, 0, 1)">g,
            </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(w) {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.options.numerals[ +<span style="color: rgba(0, 0, 0, 1)"> w]
            });
            x2 </span>= x2.replace(//<span style="color: rgba(0, 0, 0, 1)">g,
            </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(w) {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.options.numerals[ +<span style="color: rgba(0, 0, 0, 1)"> w]
            })
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> self.options.prefix + x1 + x2 +<span style="color: rgba(0, 0, 0, 1)"> self.options.suffix
    }
    </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> easeOutExpo(t, b, c, d) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> c * ( - Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 +<span style="color: rgba(0, 0, 0, 1)"> b
    }
    </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ensureNumber(n) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> (<span style="color: rgba(0, 0, 255, 1)">typeof</span> n === "number" &amp;&amp; !<span style="color: rgba(0, 0, 0, 1)">isNaN(n))
    }
    self.initialize </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.initialized) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      }
      self.error </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
      self.d </span>= (<span style="color: rgba(0, 0, 255, 1)">typeof</span> target === "string") ?<span style="color: rgba(0, 0, 0, 1)"> document.getElementById(target) : target;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">self.d) {
            self.error </span>= " target is null or undefined"<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
      }
      self.startVal </span>=<span style="color: rgba(0, 0, 0, 1)"> Number(startVal);
      self.endVal </span>=<span style="color: rgba(0, 0, 0, 1)"> Number(endVal);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ensureNumber(self.startVal) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> ensureNumber(self.endVal)) {
            self.decimals </span>= Math.max(0, decimals || 0<span style="color: rgba(0, 0, 0, 1)">);
            self.dec </span>= Math.pow(10<span style="color: rgba(0, 0, 0, 1)">, self.decimals);
            self.duration </span>= Number(duration) * 1000 || 2000<span style="color: rgba(0, 0, 0, 1)">;
            self.countDown </span>= (self.startVal &gt;<span style="color: rgba(0, 0, 0, 1)"> self.endVal);
            self.frameVal </span>=<span style="color: rgba(0, 0, 0, 1)"> self.startVal;
            self.initialized </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            self.error </span>= " startVal (" + startVal + ") or endVal (" + endVal + ") is not a number"<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
      }
    };
    self.printValue </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(value) {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> self.options.formattingFn(value);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (self.d.tagName === "INPUT"<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.d.value =<span style="color: rgba(0, 0, 0, 1)"> result
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (self.d.tagName === "text" || self.d.tagName === "tspan"<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.d.textContent =<span style="color: rgba(0, 0, 0, 1)"> result
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span>.d.innerHTML =<span style="color: rgba(0, 0, 0, 1)"> result
            }
      }
    };
    self.count </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(timestamp) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">self.startTime) {
            self.startTime </span>=<span style="color: rgba(0, 0, 0, 1)"> timestamp
      }
      self.timestamp </span>=<span style="color: rgba(0, 0, 0, 1)"> timestamp;
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> progress = timestamp -<span style="color: rgba(0, 0, 0, 1)"> self.startTime;
      self.remaining </span>= self.duration -<span style="color: rgba(0, 0, 0, 1)"> progress;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.options.useEasing) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.countDown) {
                self.frameVal </span>= self.startVal - self.options.easingFn(progress, 0, self.startVal -<span style="color: rgba(0, 0, 0, 1)"> self.endVal, self.duration)
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                self.frameVal </span>= self.options.easingFn(progress, self.startVal, self.endVal -<span style="color: rgba(0, 0, 0, 1)"> self.startVal, self.duration)
            }
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.countDown) {
                self.frameVal </span>= self.startVal - ((self.startVal - self.endVal) * (progress /<span style="color: rgba(0, 0, 0, 1)"> self.duration))
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                self.frameVal </span>= self.startVal + (self.endVal - self.startVal) * (progress /<span style="color: rgba(0, 0, 0, 1)"> self.duration)
            }
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.countDown) {
            self.frameVal </span>= (self.frameVal &lt; self.endVal) ?<span style="color: rgba(0, 0, 0, 1)"> self.endVal: self.frameVal
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            self.frameVal </span>= (self.frameVal &gt; self.endVal) ?<span style="color: rgba(0, 0, 0, 1)"> self.endVal: self.frameVal
      }
      self.frameVal </span>= Math.round(self.frameVal * self.dec) /<span style="color: rgba(0, 0, 0, 1)"> self.dec;
      self.printValue(self.frameVal);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (progress &lt;<span style="color: rgba(0, 0, 0, 1)"> self.duration) {
            self.rAF </span>=<span style="color: rgba(0, 0, 0, 1)"> requestAnimationFrame(self.count)
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.callback) {
                self.callback()
            }
      }
    };
    self.start </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(callback) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">self.initialize()) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      }
      self.callback </span>=<span style="color: rgba(0, 0, 0, 1)"> callback;
      self.rAF </span>=<span style="color: rgba(0, 0, 0, 1)"> requestAnimationFrame(self.count)
    };
    self.pauseResume </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">self.paused) {
            self.paused </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            cancelAnimationFrame(self.rAF)
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            self.paused </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)"> self.startTime;
            self.duration </span>=<span style="color: rgba(0, 0, 0, 1)"> self.remaining;
            self.startVal </span>=<span style="color: rgba(0, 0, 0, 1)"> self.frameVal;
            requestAnimationFrame(self.count)
      }
    };
    self.reset </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      self.paused </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)"> self.startTime;
      self.initialized </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.initialize()) {
            cancelAnimationFrame(self.rAF);
            self.printValue(self.startVal)
      }
    };
    self.update </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(newEndVal) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">self.initialize()) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      }
      newEndVal </span>=<span style="color: rgba(0, 0, 0, 1)"> Number(newEndVal);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">ensureNumber(newEndVal)) {
            self.error </span>= " update() - new endVal is not a number: " +<span style="color: rgba(0, 0, 0, 1)"> newEndVal;
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      }
      self.error </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (newEndVal ===<span style="color: rgba(0, 0, 0, 1)"> self.frameVal) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      }
      cancelAnimationFrame(self.rAF);
      self.paused </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)"> self.startTime;
      self.startVal </span>=<span style="color: rgba(0, 0, 0, 1)"> self.frameVal;
      self.endVal </span>=<span style="color: rgba(0, 0, 0, 1)"> newEndVal;
      self.countDown </span>= (self.startVal &gt;<span style="color: rgba(0, 0, 0, 1)"> self.endVal);
      self.rAF </span>=<span style="color: rgba(0, 0, 0, 1)"> requestAnimationFrame(self.count)
    };
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (self.initialize()) {
      self.printValue(self.startVal)
    }
};</span></pre>
</div>
<h3>2.1 代码示例</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 数字滚动 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="numScroll"</span><span style="color: rgba(255, 0, 0, 1)"> style</span><span style="color: rgba(0, 0, 255, 1)">="width: 200px;height: 200px;font-size: 30px;font-weight: bold;"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">
import CountUp from </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">countup</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">
export </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">default</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
    name: </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">numberScroll.vue</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">,
    mounted() {
      </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">this</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">.numberScroll()
    },
    methods: {
      numberScroll() {
      let count </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">new</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> CountUp(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">numScroll</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">0</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">56565</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">0</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">5</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, {duration: </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">5</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">, useEasing: </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">false</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">})
      </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">if</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> (</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">!</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">count.error) {
          count.start()
      } </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">else</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> {
          console.log(count.error)
      }
      }
    }
}
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>  </p>
<p>  </p>
<p>&nbsp;</p>
<p>  </p><br><br>
来源:https://www.cnblogs.com/ajuan/p/11151749.html
頁: [1]
查看完整版本: JavaScript 数字滚动countup.js