I've tried everything in ScrollIntoView() causing the whole page to move and Using scrollIntoView with a fixed position header, but I still end up with a weird behavior when using scrollIntoView
.
I basically have a scrollable container with a sticky action button at the bottom of it. Within that container I want to use scrollIntoView
on children elements.
The function works well but when I scroll back up to the first item, it is not fully visible anymore and the parent container feels like it moved.
From many tests, HTML restructuring etc. I'm now quite sure that the issue comes from the sticky action container height, the scrollable container seems to be shifted by that height.
Any insights about that behavior? Is the HTML structure wrong for the use case?
Minimal reproduction: (codepen if you wanna play with it)
const scrollButton = document.querySelector('button');
scrollButton.onclick = () => {
const item = document.getElementById('3');
item.scrollIntoView({behavior:'smooth', block:'center'});
}
.main-container {
flex: 1 1 auto;
overflow: hidden;
height: 300px;
width: 50%;
background-color: lightpink;
padding: 16px;
}
.scrollable-container {
overflow-y: auto;
height: 100%;
width: 100%;
}
.item {
min-height: 200px;
background-color: lightblue;
margin-bottom: 8px;
}
.stick-action {
padding: 48px 0px 12px 0px;
pointer-events: none;
z-index: 1;
bottom: 0;
position: sticky;
text-align: center;
background: linear-gradient(
180deg,
rgba(255, 255, 255, 0) 19.3%,
rgba(255, 255, 255, 0.981337) 77.3%
);
}
<button>scroll 3</button>
<div class="main-container">
<div class="scrollable-container">
<div id="1" class="item">First item</div>
<div id="2" class="item">Second item</div>
<div id="3" class="item">Third item</div>
</div>
<div class="stick-action">action<div>
</div>