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 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

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
  • 40,431
  • 11
  • 76
  • 106
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
  • 83,432
  • 35
  • 168
  • 231
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
  • 588
  • 1
  • 3
  • 12