0

As we can get offset top of an element by using like this:

$(selector).offset().top;

But how can we get the top value of parent offset div?

I've tried like this but doesn't work?

$(selector).offsetParent().top;
Derek 朕會功夫
  • 88,688
  • 41
  • 174
  • 241
Navin Rauniyar
  • 9,427
  • 12
  • 41
  • 67

3 Answers3

1

Try to use parent() like,

$(selector).parent().offset().top;

Read Get position/offset of element relative to a parent container?

Community
  • 1
  • 1
Rohan Kumar
  • 39,838
  • 11
  • 73
  • 103
1

Um, you can use like this:

$(selector).offsetParent().offset().top;

see documentation on offsetParent

offsetParent doesn't get offset value but it just select the closest positioned parent div. You can get the offset/position by using offset and position.

demo

Bhojendra Rauniyar
  • 78,842
  • 31
  • 152
  • 211
0

Don't forget you have to go get offsets all the way to the topmost parent. If you have enough nested content it's probably worth writing a recursive function to get after it.

Zach Babb
  • 488
  • 3
  • 10