window.onload = function () { var wrapTop = $(".youshi").offset().top; var istrue = true; $(window).on("scroll", function () { var s = $(window).scrollTop(); if (s > wrapTop - 820 && istrue) { $(".timer").each(count); function count(a) { var b = $(this); a = $.extend({}, a || {}, b.data("countToOptions") || {}); b.countTo(a) } istrue = false; } }); //璁剧疆璁℃暟 $.fn.countTo = function (options) { options = options || {}; return $(this).each(function () { //褰撳墠鍏冪礌鐨勯€夐」 var settings = $.extend({}, $.fn.countTo.defaults, { from: $(this).data('from'), to: $(this).data('to'), speed: $(this).data('speed'), refreshInterval: $(this).data('refresh-interval'), decimals: $(this).data('decimals') }, options); //鏇存柊鍊 var loops = Math.ceil(settings.speed / settings.refreshInterval), increment = (settings.to - settings.from) / loops; //鏇存敼搴旂敤鍜屽彉閲 var self = this, $self = $(this), loopCount = 0, value = settings.from, data = $self.data('countTo') || {}; $self.data('countTo', data); //濡傛灉鏈夐棿鏂紝鎵惧埌骞舵竻闄 if (data.interval) { clearInterval(data.interval); } ; data.interval = setInterval(updateTimer, settings.refreshInterval); //鍒濆鍖栬捣濮嬪€ render(value); function updateTimer() { value += increment; loopCount++; render(value); if (typeof(settings.onUpdate) == 'function') { settings.onUpdate.call(self, value); } if (loopCount >= loops) { //绉诲嚭闂撮殧 $self.removeData('countTo'); clearInterval(data.interval); value = settings.to; if (typeof(settings.onComplete) == 'function') { settings.onComplete.call(self, value); } } } function render(value) { var formattedValue = settings.formatter.call(self, value, settings); $self.html(formattedValue); } }); }; $.fn.countTo.defaults = { from: 0, //鏁板瓧寮€濮嬬殑鍊 to: 0, //鏁板瓧缁撴潫鐨勫€ speed: 1000, //璁剧疆姝ラ暱鐨勬椂闂 refreshInterval: 100, //闅旈棿鍊 decimals: 0, //鏄剧ず灏忎綅鏁 formatter: formatter, //娓叉煋涔嬪墠鏍煎紡鍖 onUpdate: null, //姣忔鏇存柊鍓嶇殑鍥炶皟鏂规硶 onComplete: null //瀹屾垚鏇存柊鐨勫洖璋冩柟娉 }; function formatter(value, settings) { return value.toFixed(settings.decimals); } //鑷畾涔夋牸寮 $('#count-number').data('countToOptions', { formmatter: function (value, options) { return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ','); } }); //瀹氭椂鍣 $('.timer').each(count); function count(options) { var $this = $(this); options = $.extend({}, options || {}, $this.data('countToOptions') || {}); $this.countTo(options); } };