我希望在浏览器窗口超出特定点时调用函数
(例如,用户将窗口从顶部向下滚动超过200px
是否有可以绑定的事件,然后我如何检查从浏览器顶部到页面顶部的偏移量?
所以在我的网站上,我在网站的最顶部有一个静态标题 - 它没有固定在视口的顶部.但是,我想做的是一旦用户滚过这个div的底部,就会出现一个固定的导航栏.我的代码几乎可以工作,但只触发div的顶部偏移量,这是页面的最顶层.这是我的代码:
$("#header-2").hide(); // hide the fixed navbar initially
var topofDiv = $("#header-container").offset().top; //gets offset of header
$(window).scroll(function(){
if($(window).scrollTop() > topofDiv){
$("#header-2").show();
}
else{
$("#header-2").hide();
}
});
Run Code Online (Sandbox Code Playgroud)
同样,我需要在用户滚过底部时触发显示固定的导航栏#header-container,而不是像现在一样滚动顶部.救命?