//鏁板瓧婊氬姩 ; (function($, window, document) { "use strict"; var defaults = { value: 0, //浼犲叆鍊 className: 'rollnum', //鏍峰紡鍚嶇О digit: '' //榛樿鏄剧ず鍑犱綅鏁板瓧 }; function rollNum(obj, options) { this.obj = obj; this.options = $.extend(defaults, options); this.init = function() { this.initHtml(obj, defaults); } } rollNum.prototype = { initHtml: function(obj, options) { var strHtml = ''; //'
'; obj.html(strHtml); } obj.addClass(options.className) this.scroNum(obj, options); }, scroNum: function(obj, options) { var number = options.value; var $num_item = $(obj).find('.nums'); //.find('.' + options.className).find('.nums'); var h = $(obj).find('.numItem').height(); $num_item.css('transition', 'all 2s ease-in-out'); var numberStr = number.toString(); if (numberStr.length <= $num_item.length - 1) { var tempStr = ''; for (var a = 0; a < $num_item.length - numberStr.length; a++) { tempStr += '0'; } numberStr = tempStr + numberStr; } var numberArr = numberStr.split(''); $num_item.each(function(i, item) { setTimeout(function() { $num_item.eq(i).css('top', (-parseInt(numberArr[i]) * h - h * 10) + 'px'); }, i * 100) }); } } $.fn.rollNum = function(num, dtime, simple) { var obj = this; if (simple) { obj.animate({ count: num }, { duration: dtime || 2000, step: function() { obj.text(parseInt(this.count)); }, done: function() { obj.text(num); //瀹屾垚 //obj.stop(); } }); } else { var rollNumObj = new rollNum(obj, { value: num }); rollNumObj.init(); } }; })(jQuery, window, document);