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; }
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.
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.
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.
offset(). top; This will give you the computed offset (relative to document) of any object.
Use this to get the page scroll position.
var screenTop = $(document).scrollTop(); $('#content').css('top', screenTop);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With