Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.position returning different values for the same selector inside an scrolling div

I am using the $.position over the same selector and I am getting different results for the top value.

The problem might be in getting the position of the selector inside the scrolling div. That's why I am using $.position instead of $.offset, but it seems I am doing something wrong.

You can try it clicking multiple times on the the text "Get Position" at my fiddle: http://jsfiddle.net/FgftM/1/

Once the div has scrolled to look for the selector, clicking on the "Get Position" again shouldn't do anything because I am already on the selector position.

The resulting position values showed at the bottom.

To clarify the structure of the page, here's an sketch: enter image description here

Original fiddle: http://jsfiddle.net/BtZQE/16/

like image 590
Alvaro Avatar asked Apr 24 '26 14:04

Alvaro


1 Answers

I believe what you want to do is set position: relative on #content. That way the position is calculated in relation to the container instead of the overall page.

http://jsfiddle.net/FgftM/6/

EDIT: After some clarification, you still need the position: relative but you also need to add in the current scrollTop value:

http://jsfiddle.net/FgftM/8/

var top = dest.top + $('#content').scrollTop();
like image 51
James Montagne Avatar answered Apr 26 '26 03:04

James Montagne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!