Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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; } 
like image 575
cj333 Avatar asked Aug 10 '11 23:08

cj333


People also ask

How do you get to the top of the page in JavaScript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.

What is offset () Top?

Definition and Usage The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.

What is offset () in jQuery?

jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

How do I get the top position of a div?

offset(). top; This will give you the computed offset (relative to document) of any object.


1 Answers

Use this to get the page scroll position.

var screenTop = $(document).scrollTop();  $('#content').css('top', screenTop); 
like image 170
ShankarSangoli Avatar answered Sep 21 '22 01:09

ShankarSangoli