当有人滚动到 ID 时,用 JQuery 启动动画数字计数器
Start animated number counter with JQuery when someone scrolls to ID
有谁知道当您滚动到特定父 ID 时如何启动数字计数器动画,例如 #counter-container
?
$(window).scroll(startCounter);
function startCounter() {
if ($(window).scrollTop() > 200) {
$(window).off("scroll", startCounter);
$('.count').each(function() {
var $this = $(this);
jQuery({
Counter: 0
}).animate({
Counter: $this.text().replace(/,/g, '')
}, {
duration: 1000,
easing: 'swing',
step: function() {
$this.text(commaSeparateNumber(Math.floor(this.Counter)));
},
complete: function() {
$this.text(commaSeparateNumber(this.Counter));
//alert('finished');
}
});
});
function commaSeparateNumber(val) {
while (/(\d+)(\d{3})/.test(val.toString())) {
val = val.toString().replace(/(\d+)(\d{3})/, '' + ',' + '');
}
return val;
}
}
}
#counter-container {
margin-top: 1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Scroll down...
<div id="counter-container">
<span class="count">6,000</span>
<span class="count">600</span>
<span class="count">60</span>
</div>
要确定滚动是否经过某个元素,您可以将 window 滚动位置 + 高度与目标元素的垂直偏移进行比较。试试这个:
$(window).scroll(startCounter);
function startCounter() {
let scrollY = (window.pageYOffset || document.documentElement.scrollTop) + window.innerHeight;
let divPos = document.querySelector('#counter-container').offsetTop;
if (scrollY > divPos) {
$(window).off("scroll", startCounter);
$('.count').each(function() {
var $this = $(this);
jQuery({
Counter: 0
}).animate({
Counter: $this.text().replace(/,/g, '')
}, {
duration: 1000,
easing: 'swing',
step: function() {
$this.text(commaSeparateNumber(Math.floor(this.Counter)));
},
complete: function() {
$this.text(commaSeparateNumber(this.Counter));
//alert('finished');
}
});
});
function commaSeparateNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
}
#spacer {
height: 1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Scroll down...
<div id="spacer"></div>
<div id="counter-container">
<span class="count">6,000</span>
<span class="count">600</span>
<span class="count">60</span>
</div>
另请注意,我实施了一个更强大的号码分组逻辑版本,取自 this answer。
有谁知道当您滚动到特定父 ID 时如何启动数字计数器动画,例如 #counter-container
?
$(window).scroll(startCounter);
function startCounter() {
if ($(window).scrollTop() > 200) {
$(window).off("scroll", startCounter);
$('.count').each(function() {
var $this = $(this);
jQuery({
Counter: 0
}).animate({
Counter: $this.text().replace(/,/g, '')
}, {
duration: 1000,
easing: 'swing',
step: function() {
$this.text(commaSeparateNumber(Math.floor(this.Counter)));
},
complete: function() {
$this.text(commaSeparateNumber(this.Counter));
//alert('finished');
}
});
});
function commaSeparateNumber(val) {
while (/(\d+)(\d{3})/.test(val.toString())) {
val = val.toString().replace(/(\d+)(\d{3})/, '' + ',' + '');
}
return val;
}
}
}
#counter-container {
margin-top: 1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Scroll down...
<div id="counter-container">
<span class="count">6,000</span>
<span class="count">600</span>
<span class="count">60</span>
</div>
要确定滚动是否经过某个元素,您可以将 window 滚动位置 + 高度与目标元素的垂直偏移进行比较。试试这个:
$(window).scroll(startCounter);
function startCounter() {
let scrollY = (window.pageYOffset || document.documentElement.scrollTop) + window.innerHeight;
let divPos = document.querySelector('#counter-container').offsetTop;
if (scrollY > divPos) {
$(window).off("scroll", startCounter);
$('.count').each(function() {
var $this = $(this);
jQuery({
Counter: 0
}).animate({
Counter: $this.text().replace(/,/g, '')
}, {
duration: 1000,
easing: 'swing',
step: function() {
$this.text(commaSeparateNumber(Math.floor(this.Counter)));
},
complete: function() {
$this.text(commaSeparateNumber(this.Counter));
//alert('finished');
}
});
});
function commaSeparateNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
}
#spacer {
height: 1000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Scroll down...
<div id="spacer"></div>
<div id="counter-container">
<span class="count">6,000</span>
<span class="count">600</span>
<span class="count">60</span>
</div>
另请注意,我实施了一个更强大的号码分组逻辑版本,取自 this answer。