gpt4 book ai didi

javascript - 计算卷轴上的数字

转载 作者:可可西里 更新时间:2023-11-01 13:20:21 25 4
gpt4 key购买 nike

如果我向上滚动到数字,如何重新开始计数?我有一个计数。我再向下滚动页面,然后再次返回页面。我想再次从零开始计数,这可能吗?

function count($this) {
var current = parseInt($this.html(), 10);
current = current + 1;
$this.html(++current);
if (current > $this.data('count')) {
$this.html($this.data('count'));
} else {
setTimeout(function() {
count($this)
}, 1);
}
}

$(".stat-count").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
#counter {
margin-bottom: 90%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="statscounts" id="counter">
<div class="statwrap">
<span class="stat-count ">400</span>
<small>number 1</small>
</div>
<div class="statwrap">
<span class="stat-count">105</span>
<small>number 2</small>
</div>
<div class="statwrap">
<span class="stat-count">321</span>
<small>number 3</small>
</div>
</div>

最佳答案

您可以使用 jQuery 检测滚动位置。一旦到达零位置,您将重新运行允许计数器的脚本。滚动到零位置后,最好将 each 放在一个函数中,方法是调用它。

$(window).scroll(function (event) {
var scroll = $(window).scrollTop();

if(scroll == 0)
setTimeout(function(){reInitCount();},500);
});

function reInitCount(){
$(".stat-count").each(function() {
$(this).html('0');
count($(this));
});
}

function reInitCount(){
$(".stat-count").each(function() {
$(this).html('0');
count($(this));
});
}

function count($this) {
var current = parseInt($this.html(), 10);
current = current + 1;
$this.html(++current);
if (current > $this.data('count')) {
$this.html($this.data('count'));
} else {
setTimeout(function() {
count($this)
}, 1);
}
}

reInitCount();


$(window).scroll(function (event) {
var scroll = $(window).scrollTop();
if(scroll == 0)
setTimeout(function(){reInitCount();},500);


});
#counter {
margin-bottom: 90%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="statscounts" id="counter">
<div class="statwrap">
<!-- it's better to initialize your data-count on the HTML side -->
<span class="stat-count" data-count="400">0</span>
<small>number 1</small>
</div>
<div class="statwrap">
<span class="stat-count" data-count="105">0</span>
<small>number 2</small>
</div>
<div class="statwrap">
<span class="stat-count" data-count="321">0</span>
<small>number 3</small>
</div>
</div>
<p>
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />
......<br />

</p>

关于javascript - 计算卷轴上的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com