Let's imagine an example of some content that dynamically inserted upon data fetch from server. The purpose is to display it like a bunch of items in a scroll-snapped container.
The problem is browser somehow randomly tries to scroll to some element but I want the container to stay at the first element no matter how many elements are inserted. The only exception is user that manually scrolls the container.
It works fine unless I add scroll-snap
property.
Here is the code snippet
const startingIdx = 3;
const batchSize = 10;
const delay = 500;
const root = document.querySelector('.pills');
const appendBatch = (from) => {
for(let i = 0; i < batchSize; i++){
const element = document.createElement('div');
element.classList.add('pill');
element.innerText = String(from + i);
root.appendChild(element);
}
}
for(let i = 0; i < 3; i++){
setTimeout(() => appendBatch(startingIdx + i*batchSize), i*delay);
}
.pills {
display: flex;
gap: 20px;
width: 100%;
overflow: auto;
scroll-snap-type: x mandatory;
}
.pill {
width: 120px;
height: 40px;
background: rgba(92, 138, 255, 0.15);
display: flex;
align-items: center;
justify-content: center;
border-radius: 20px;
flex-shrink: 0;
scroll-snap-align: start;
}
<main>
<div class="pills">
<div class="pill">1</div>
<div class="pill">2</div>
</div>
</main>
And the demo where you can reproduce it yourself: https://977940.playcode.io/
This is what I'm talking about