gpt4 book ai didi

javascript - 当它从底部显示 100px 时如何在滚动上触发 jquery 函数

转载 作者:行者123 更新时间:2023-11-28 08:38:42 25 4
gpt4 key购买 nike

我的网站上有“有趣的事实”计数器,还有一个动画计数的 jQuery 函数有趣的事实 div 中的数字。现在它在页面加载时触发。我想在用户向下滚动到它时启动它。例如,当 div 出现在距屏幕底部 100px 处时。

这是我的 jQuery 函数,它在页面加载时开始计数:

(function($) {
"use strict";
function count($this){
var current = parseInt($this.html(), 10);
current = current + 10; /* Where 50 is increment */
$this.html(++current);
if(current > $this.data('count')){
$this.html($this.data('count'));
} else {
setTimeout(function(){count($this)}, 50);
}
}
$(".stat-count").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
})(jQuery);

这是我尝试在用户向下滚动到它时让它工作:

(function($) {
"use strict";
function count($this){
var current = parseInt($this.html(), 10);
current = current + 10; /* Where 50 is increment */
$this.html(++current);
if(current > $this.data('count')){
$this.html($this.data('count'));
} else {
setTimeout(function(){count($this)}, 50);
}
}




$(window).scroll(function() {
$(".stat-count").each(function() {


var imagePos = $(this).offset().top;

var topOfWindow = $(window).scrollTop();
if (imagePos < topOfWindow+400) {

$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));

}

}
}

})(jQuery);

谢谢! :)

最佳答案

可能是这样的:

$(window).on('scroll', function(event) {
if(document.body.offsetHeight - event.pageY <= 100 ) {
//onscoll effect
}

}

关于javascript - 当它从底部显示 100px 时如何在滚动上触发 jquery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27907271/

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