Scrollintoview Breaks The Overflow Scroll
I have a nested child container and when I'm trying to scrollIntoView it breaks the parent container. I'm not able to understand why it's acting like this. Please help me out in th
Solution 1:
The whole problem is that scrollIntoView()
is moving the window. But since the #parent
overflow is hidden, when the window is moved, this element itself breaks. I could suggest setting a position: fixed
for the #parent
, which will solve your problem, but it can harm the layout in general.
Use the scroll()
method. The scrolling mechanism itself is:
scroller.scroll(0, child.offsetTop - 55);
child.offsetTop - top element;
55 - distance from the top of the #parent
to the top #scroller
.
The transition animation must be set to css, in selector #scroller
. Like that:
#scroller {
...
scroll-behavior: smooth;
}
functionmoveToTop() {
console.log('MOVE TO TOP::');
const child = document.getElementById('child');
const scroller = document.getElementById('scroller');
scroller.scroll(0, child.offsetTop - 55);
}
#parent {
background-color: blue;
padding: 10px;
height: 500px;
overflow: hidden;
display: flex;
flex-direction: column;
}
#scroller {
overflow: auto;
padding: 10px;
background-color: red;
flex-grow: 1;
scroll-behavior: smooth;
}
#child {
height: 10000px;
background-color: green;
}
body {
overflow: hidden;
color: #fff;
font-weight: bold;
}
button {
position: fixed;
bottom: 20px;
width: 140px;
left: 20%;
right: 0;
}
<divid="parent">
PARENT
<divid="something">Something</div><divid="scroller">
CHILD
<divid="child">
GRAND CHILD
<buttononclick="moveToTop()">Top</button></div></div></div>
Post a Comment for "Scrollintoview Breaks The Overflow Scroll"