滑动到当前位置,数字递增效果

  • A+
所属分类:Web前端
html:
<div class="flexW">
  <div class="num">350</div>
  <div class="num">323435</div>
  <div class="num">7</div>
  <div class="num">25</div>
</div>

css:
.flexW{display:flex;justify-content: space-between;}
.num{width:23%;}

js:

$.fn.numChange = function(){
var _this = this;
$(window).scroll(function(){
if($(window).scrollTop()>(_this.offset().top-$(window).height()-100)){
if(!_this.attr('data-change')){
_this.find('.num').each(function(){
let endNum = $(this).text()*100,
startNum = 0,
time = 100,
speed = 30,
addNum = Math.round(endNum/time),
_this = $(this);
_this.text(0);
var numChange = setInterval(function(){
startNum += addNum;
_this.text(Math.round(startNum/100));
if(startNum >= endNum){
clearInterval(numChange);
}
},speed)
});
}
_this.attr('data-change','true');
}
});

}
$('.flexW').numChange();
$(window).trigger('scroll');