gpt4 book ai didi

JavaScript/jQuery 不触发滚动事件

转载 作者:行者123 更新时间:2023-11-30 00:09:32 29 4
gpt4 key购买 nike

我正在尝试通过 jQuery 和 JavaScript 为我正在开发的 wordpress 网站触发滚动事件。

$(window).scroll(function() {
var hT = $('#target').offset().top;
hH = $('#target').outerHeight();
wH = $(window).height();
wS = $(this).scrollTop();
console.log((hT-wH) , wS);
if (wS > (hT+hH-wH)){
console.log('working');
}
});

控制台上没有任何反应。以下代码不起作用:

$(window).scroll(function() {
console.log('scrolling');
});

也不要这样做:

function scrollFunction() {
console.log('scrolling');
}

window.onscroll = scrollFunction;

我已经在其他非 wordpress 项目上测试了这些行,即使在 codepen 中也是如此,它们可以正常工作。我缺少什么?

我正在使用 jQuery 12.2.2。此外,在同一项目中出于同一目的,我无法使用 Waypoints.js,正如我在 this other question 中发布的那样.

非常感谢任何帮助,谢谢!

最佳答案

您的代码是否已包含在闭包中?也许其他一些 JavaScript 与 jQuery 冲突并使用 $ 符号。

例如像这样包装它以确保 $ 是 jQuery:

(function($){

$(window).scroll(function() {
var hT = $('#target').offset().top;
hH = $('#target').outerHeight();
wH = $(window).height();
wS = $(this).scrollTop();
console.log((hT-wH) , wS);
if (wS > (hT+hH-wH)){
console.log('working');
}
});

})(jQuery);

关于JavaScript/jQuery 不触发滚动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071364/

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