javascript 如何检查用户是否在 html 页面的顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17216846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how check if user is at the top of the html page?
提问by Lir An
I have a dropdown menu which I like to close automaticly if the user is at the top of the html page,how this can be done with javascript/jquery?
我有一个下拉菜单,如果用户位于 html 页面的顶部,我喜欢自动关闭它,如何使用 javascript/jquery 完成此操作?
回答by Morgan T.
You can easily do this with checking the scrollTopmethod from jQuery, on the window object:
您可以通过检查jQuery 中 window 对象的scrollTop方法轻松做到这一点:
$(window).scrollTop()
Just handle the scroll event and within the function, check $(window).scrollTop() === 0 and you will know if the user is scrolled to the top
只需处理滚动事件并在函数内,检查 $(window).scrollTop() === 0 你就会知道用户是否滚动到顶部
$(document).scroll(function() {
if($(window).scrollTop() === 0) {
$(".menu").hide();
}
});