44

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

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.

So just put the current screen top position to:

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

And

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

2 Answers 2

73

Use this to get the page scroll position.

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

$('#content').css('top', screenTop);
1
  • Perfect solution
    – Linga
    Commented May 11, 2018 at 13:14
51
var top = $('html').offset().top;

should do it.

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

6
  • 3
    This will not give what OP is looking for. It will give the offset top of the html element which will be a negative value if we have scrolled the page. Commented Aug 11, 2011 at 0:00
  • 4
    As noted in the edit, this is the negative of $(document).scrollTop(), which yes, is more appropriate in this case. But the html offset * -1 is effectively the same thing. Commented Aug 11, 2011 at 0:05
  • 1
    You are right, but $(document).scrollTop() gives what you want then why use $('html').offset().top and multiply by -1. Commented Aug 11, 2011 at 0:12
  • 1
    @ShankarSangoli I acknowledged that your answer is more appropriate, which is why I edited mine. The answer should go to you, yes. Still, isn't it always better to learn 2 different ways of accomplishing a task, if you start out knowing 0? Commented Aug 11, 2011 at 0:14
  • 1
    Debugging in IE11: ?$(document).scrollTop() 593 ?$("html").scrollTop() 593 ?$("html").offset().top -0.44000244140625 593 was the right answer. Similar result in chrome, except the last one reported zero. Commented Nov 7, 2018 at 15:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.