使用带有滚动捕捉的无限列表时,在 Chrome Mobile 中滑动滚动或在 Chrome for Windows 中激活移动模式时,在底部添加新元素时,滚动位置似乎会跳跃(屏幕闪烁)。当按住手指或鼠标同时向下滑动滚动直到加载新元素时,效果尤其明显。滚动位置向上跳一个元素。
例子:
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>