How do you scroll an element inside a scrollable div?

I have seen scrollTo for the whole page, but I am only looking for scrolling content in a div.

enter image description here

In the image, I have an element that I want to scroll. How to move the scroll to the top of the containing div (which is already scrolling, I just want it to be automatic).

There is a lot of text under the selected item to scroll through an object.

+4
source share
1 answer

You can set the scrollTop element that contains this element. And the installation value is actually the top position of this element, which can be obtained using $(element).position().top .

So you need the code:

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

NOTE. You needed to find the offset between the element containing the other_element and the top of the page so that it could scroll precisely.

+3
source

Source: https://habr.com/ru/post/1391708/


All Articles