When using an infinite list with scroll-snapping, the scroll position seems to jump (the screen flashes) when adding new elements at the bottom while swipe-scrolling in Chrome Mobile or with the mobile mode activated in Chrome for Windows. The effect is especially visible when holding down the finger or mouse while swipe-scrolling down until a new element is loaded. The scroll position jumps up one element.
Example:
element = document.querySelector('.scroll-snap-container')
element.addEventListener('scroll', function (e) {
if (element.scrollHeight -
element.scrollTop -
2 * element.getBoundingClientRect().height < 1700) {
let newChild = document.createElement('div')
newChild.className = "scroll-snap-item"
newChild.style.backgroundColor = getRandomColor()
element.appendChild(newChild)
}
})
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
.scroll-snap-container {
overflow: scroll;
width: 550px;
height: 800px;
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
}
.scroll-snap-item {
scroll-snap-align: start;
scroll-snap-stop: always;
height: 100%;
width: 100%;
}
<div class="scroll-snap-container">
<div class="scroll-snap-item" style="background-color: red"></div>
<div class="scroll-snap-item" style="background-color: green"></div>
</div>