var offset = $(selector).offset();
The values of offset variable changes if we scroll the page up and down, i want the exact and fixed offset values while keeping the position of the "selector" default(static).How can i do this?
Definition and Usage. The pageXOffset property returns the pixels a document has scrolled from the upper left corner of the window. The pageXOffset property is equal to the scrollX property.
jQuery offset() MethodThe 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.
jQuery. scrollable manages animated scrolling in windows, scrollable elements and iframes. It frees you from handling gotchas and edge cases and offers convenient, flexible options for animation. If you are a happy user of this project already, you can support its development by donating to it.
The scrollTop() method sets or returns the vertical scrollbar position for the selected elements.
You could always calculate the offset, factoring in the scroll position:
var offset_t = $(selector).offset().top - $(window).scrollTop(); var offset_l = $(selector).offset().left - $(window).scrollLeft();
One other potential cause is if your <body>
happens to have CSS that sets overflow-x: hidden;
- that completely breaks jQuery's offset()
method.
In that case, $(window).scrollTop()
is always 0, so the accepted answer does not work.
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