简体   繁体   English

如何滚动到可滚动div中的元素?

[英]How do you scroll to an element that is within a scrollable div?

I've seen scrollTo used for an entire page, but I'm looking to scroll content within just a div. 我已经看到scrollTo用于整个页面,但我希望在div中滚动内容。

在此输入图像描述

In the image, I have an element I want to scroll to. 在图像中,我有一个我想滚动的元素。 How to I bring the scroll to the top of the containing div (which already scrolls, I just want it to be automatic). 如何将滚动条带到包含div的顶部(已经滚动,我只是希望它是自动的)。

There is plenty of text below the highlighted element to facility scrolling. 在突出显示的元素下面有大量文本来设置滚动。

You can set the scrollTop of the element which contains that element. 您可以设置包含该元素的元素的scrollTop And the value to set is actually the top position of that element which can be obtained using $(element).position().top . 并且要设置的值实际上是该元素的顶部位置,可以使用$(element).position().top

So, the code you need is: 所以,你需要的代码是:

$j(scrollable_div).scrollTop($j(other_element).position().top)

NOTE: you made need to find the offset between other_element's containing element and the top of the page for it to scroll accurately. 注意:您需要找到other_element包含元素和页面顶部之间的偏移量,以便准确滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM