You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB

  1. /*!
  2. * jquery.numscroll.js -- 数字滚动累加动画插件 (Digital rolling cumulative animation)
  3. * version 1.0.0
  4. * 2018-09-22
  5. * author: KevinTseng < 921435247@qq.com@qq.com >
  6. * 文档: https://github.com/chaorenzeng/jquery.numscroll.js.git
  7. * QQ交流群: 739574382
  8. */
  9. (function($) {
  10. function isInt(num) {
  11. //作用:是否为整数
  12. //返回:true是 false否
  13. var res = false;
  14. try {
  15. if(String(num).indexOf(".") == -1 && String(num).indexOf(",") == -1) {
  16. res = parseInt(num) % 1 === 0 ? true : false;
  17. }
  18. } catch(e) {
  19. res = false;
  20. }
  21. return res;
  22. }
  23. function isFloat(num) {
  24. //作用:是否为小数
  25. //返回:小数位数(-1不是小数)
  26. var res = -1;
  27. try {
  28. if(String(num).indexOf(".") != -1) {
  29. var index = String(num).indexOf(".") + 1; //获取小数点的位置
  30. var count = String(num).length - index; //获取小数点后的个数
  31. if(index > 0) {
  32. res = count;
  33. }
  34. }
  35. } catch(e) {
  36. res = -1;
  37. }
  38. return res;
  39. }
  40. $.fn.numScroll = function(options) {
  41. var settings = $.extend({
  42. 'time': 1500,
  43. 'delay': 0
  44. }, options);
  45. return this.each(function() {
  46. var $this = $(this);
  47. var $settings = settings;
  48. var num = $this.attr("data-num") || $this.text(); //实际值
  49. var temp = 0; //初始值
  50. $this.text(temp);
  51. var numIsInt = isInt(num);
  52. var numIsFloat = isFloat(num);
  53. var step = (num / $settings.time) * 10; //步长
  54. setTimeout(function() {
  55. var numScroll = setInterval(function() {
  56. if(numIsInt) {
  57. $this.text(Math.floor(temp));
  58. } else if(numIsFloat != -1) {
  59. $this.text(temp.toFixed(numIsFloat));
  60. } else {
  61. $this.text(num);
  62. clearInterval(numScroll);
  63. return;
  64. }
  65. temp += step;
  66. if(temp > num) {
  67. $this.text(num);
  68. clearInterval(numScroll);
  69. }
  70. }, 1);
  71. }, $settings.delay);
  72. });
  73. };
  74. })(jQuery);