[英]JavaScript/jQuery not triggering scroll event
我正在嘗試通過 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,正如我在另一個問題中發布的那樣。
任何幫助都非常感謝,謝謝!
你有你的代碼在一個閉包中嗎? 也許其他一些 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);
你試過這個嗎?
<script>
window.onscroll = function() {scrolling()};
function scrolling() {
console.log("Scrolling");
}
</script>
試試下面的代碼... :)
替換了$(window).scroll(function(){});
到$(window).on('scroll', function(){})
$(window).on('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');
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.