;(function($){ // 防抖 function debounce(func, wait, ctx) { var timeout; return function () { var context = ctx; var args = arguments; if (timeout) cleartimeout(timeout); timeout = settimeout(function () { func.apply(context, args) }, wait); } } // 数字滚动的类 function scrollnum(dom, delay, num, dir, trigger){ this.dom = dom; this.delay = delay; this.num = num; this.dir = dir; this.trigger = trigger; } scrollnum.prototype.listen = function () { // 手动 if (this.trigger == 'manual') { this.dom.data('done', true) this.dom.parent().parent().data('done', true) this.beginmove() } else { // 自动 var vm = this; settimeout(function(){ vm.positionadjust(); }, 1000) // $(window).on('scroll', function () { // vm.positionadjust() // }) $(window).on('scroll', debounce(vm.positionadjust, 100, vm)) } } scrollnum.prototype.positionadjust = function (){ if (this.dom.data('done')) { return; } if (this.dom.offset().top - $(document).scrolltop() < $(window).height() - 50) { this.dom.data('done', true) this.dom.parent().parent().data('done', true) this.beginmove() } } scrollnum.prototype.init = function () { var numheight = math.floor(this.dom.parent().siblings('.c-num__original').height()); // 一个数字的高度 this.dom.parent().css({ 'line-height': numheight + 'px', 'height': numheight + 'px' }) } scrollnum.prototype.beginmove = function () { var vm = this; this.init() settimeout(function(){ vm.move() }, this.delay * 200) } scrollnum.prototype.move = function (){ var _this = this; var round = 1; var targetnum = _this.num; // 显示数字7 var numheight = math.floor(_this.dom.parent().siblings('.c-num__original').height()); // 一个数字的高度 var desheight = targetnum * numheight; // 最后一圈的位置 var oneroundheight = numheight * 10; var curpos = _this.dir == 'up' ? 0 : oneroundheight; // 初始位置 var roundcount = 0; var duration = 2000; var interval = 15; // 循环间隔 var increment = 1; if (_this.dir == 'up') { increment = (round * oneroundheight + desheight) / (duration / interval); } else { increment = (round * oneroundheight + oneroundheight - desheight) / (duration / interval); } if (increment < 1) { increment = 1 } $(window).resize(function() { clearinterval(timer) settimeout(function(){ numheight = math.floor(_this.dom.parent().siblings('.c-num__original').height()); _this.dom.parent().css({ 'line-height': numheight + 'px', 'height': numheight + 'px' }) desheight = targetnum * numheight; // 最后一圈的位置 _this.dom.css('transform', 'translatey(' + -1 * desheight + 'px)') }, 500) // _this.dom.parent().parent().css('min-width', _this.dom.parent().width()) }) // 设置初始位置 _this.dom.css('transform', 'translatey(' + curpos + 'px)') // 开始滚动 var timer = setinterval(function(){ if (_this.dir == 'up') { curpos += increment; if (curpos >= oneroundheight) { roundcount++; curpos = 0; } } else { curpos -= increment; if (curpos <= 0) { roundcount++; curpos = oneroundheight; } } _this.dom.css('transform', 'translatey(' + -1 * curpos + 'px)') if (_this.dir == 'up') { if (roundcount == round && curpos >= desheight) { clearinterval(timer) _this.dom.css('transform', 'translatey(' + -1 * desheight + 'px)') } } else { if (roundcount == round && curpos <= desheight) { clearinterval(timer) _this.dom.css('transform', 'translatey(' + -1 * desheight + 'px)') } } }, interval) } $.fn.numscroll = function(options){ options = options || {} var $this = this; var dir = 'up'; if (options.dir == 'up' || options.dir == 'down') { // 有效值 dir = options.dir } $this.each(function(i, el){ if ($(el).data('done')) { return; } else { $(el).data('done', true) } var nums = $(el).text().split('').map(function(item){ if (item === ' ') { return item } var temp = number(item) if (isnan(temp)) { return item } else { return temp } }) if (nums.every(function(item){ // 所有的字都不是数字,就返回 return isnan(item) })){ console.log('格式错误'); return } if (isnan(nums[0])) { // 第一个字不是数字类型,就返回 return } var $original = $('

' + $(el).text() + '

') var $num_wrap = $('
') $(el).html('').append($original).append($num_wrap) var numheight = math.floor($original.height()); // 一个数字的高度 $num_wrap.css({ display: 'flex', overflow: 'hidden', position: 'absolute', left: 0, top: '50%', transform: 'translatey(-50%)', 'text-align': 'center', 'line-height': numheight + 'px', 'height': numheight + 'px' }) for (var ii = 0; ii < nums.length; ii++) { if (isnan(nums[ii]) || nums[ii] === ' ') { // 不是数字,就直接原样显示,主要是兼容小数点符号 $num_wrap.append($('
' + nums[ii] + '
')) } else { var $num_item = $('
') // 加每列数字 for (var j = 0; j < 11; j++) { var $num = $('

') $num.text(j.tostring().slice(-1)) $num_item.append($num) } $num_wrap.append($num_item) new scrollnum($num_item, ii, nums[ii], dir, options.trigger).listen() } } settimeout(function(){ $(el).css({ position: 'relative', // 'min-width': $num_wrap.width() }) }, 100) }) } })(jquery);