I have the requirement to scroll a certain element inside a div (not a direct child) into view.
Basically I need the same functionality as ScrollIntoView provides, but for a specified parent (only this parent should scroll).
Additionally it is not possible for me to use any 3rd party libraries.
I am not quite sure on how to approach this problem, as I do very limited JavaScript development. Is there someone that could help me out?
I found this code that would do exactly what I need, but unfortunately it requires JQuery and I was not able to translate it to plain JavaScript.
You can do some easy stuff like:
Basically
window.location.href
should help you.This functionality can be achieved in some few steps. First you get the position of the child using
childElement.getBoundingClientRect();
which will return the following valuesThen just position the child element according to the top left values into the parent element keeping child elements
position
asabsolute
. The parent Element'sposition
must berelative
type to place the child properly and get the effect ofScrollIntoView
.I think I have a start for you. When you think about this problem you think about getting the child div into the viewable area of the parent. One naive way is to use the child position on the page relative to the parent's position on the page. Then taking into account the scroll of the parent. Heres a possible implementation.
Just pass it the parent and the child node like this:
Added a demo using this function on the main element and even nested elements
https://jsfiddle.net/nex1oa9a/1/