最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Div offset inside another div - Stack Overflow

programmeradmin5浏览0评论

I have situation similar to the one on image below, where DIV1 have overflow-y: scroll and one of child divs have class active. I don't know his position nor the offset from top. How can I find position of child div inside DIV 1 and scroll so far that child with active class is visible.

I don't mind if you use jQuery or plain js. But I prefer jQuery.

I have situation similar to the one on image below, where DIV1 have overflow-y: scroll and one of child divs have class active. I don't know his position nor the offset from top. How can I find position of child div inside DIV 1 and scroll so far that child with active class is visible.

I don't mind if you use jQuery or plain js. But I prefer jQuery.

Share Improve this question edited Feb 25, 2013 at 20:54 keeg 3,9769 gold badges50 silver badges101 bronze badges asked Feb 25, 2013 at 20:51 ewooyewooy 2,7215 gold badges32 silver badges53 bronze badges 2
  • Do you have a list of child divs that you are trying to insert a box-like div around? – Codeman Commented Feb 25, 2013 at 20:56
  • @Pheonixblade9 Yes I do – ewooy Commented Feb 25, 2013 at 21:02
Add a ment  | 

1 Answer 1

Reset to default 5

You have to use .position() method of jQuery. http://api.jquery./position/

Providing your div1 has position: relative set this method will give you offset from the top-left corner of your div1, so

$('.div1').get(0).scrollTop($('.div1 div.active').position().top);

should do the trick.

发布评论

评论列表(0)

  1. 暂无评论