jquery 如何获取页面的当前屏幕顶部位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7019353/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:42:55  来源:igfitidea点击:

jquery how to get the page's current screen top position?

jquery

提问by cj333

jquery how to get the page's current screen top position?

jquery 如何获取页面当前的屏幕顶部位置?

If I scroll my mouse wheel to some part of the page, how do I get the page's current top position?

如果我将鼠标滚轮滚动到页面的某个部分,如何获得页面的当前顶部位置?

I want click one element in my page, then open a div which top is to current screen top.

我想单击页面中的一个元素,然后打开一个顶部到当前屏幕顶部的 div。

So just put the current screen top position to:

所以只需将当前屏幕顶部位置设置为:

$('#content').css('top','current position');

And

#content
{
position:absolute;
left:100px;
}

回答by ShankarSangoli

Use this to get the page scroll position.

使用它来获取页面滚动位置。

var screenTop = $(document).scrollTop();

$('#content').css('top', screenTop);

回答by numbers1311407

var top = $('html').offset().top;

should do it.

应该这样做。

edit: this is the negative of $(document).scrollTop()

编辑:这是否定的 $(document).scrollTop()