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

发布时间:2020-08-13 22:00:19发布者:Mr.Zhang阅读(55)

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');




本文转自博客园,原文地址:https://www.cnblogs.com/myyt/p/13404094.html

自己做的一款记账小程序,请多多支持