在移动设备上,你可以"拉"出可滚动的内容,它会自动移动一段时间.如何检测这种运动何时停止?
ontouchmove
事件在内容自行移动时不会被触发.
这里是一个例子,我需要删除"更多内容"横幅时,用户已向左滚动到最后.
我正在阅读使用ontouchmove
事件留下多少可滚动区域.但由于该事件仅在用户实际正在主动touch 移动内容时触发,因此无法读取还剩下多少可滚动区域.
如果我在按下内容的同时滚动到最后,一切都很好:
如果我快速"拉"它,并且它自己不停地滚动,则在到达结尾处时onouch移动事件不会触发,因此不可能检测到到达结尾处:
function onTouchMove() {
const node = document.getElementById("scroll-container");
const overflownOnRight = Math.ceil(node.scrollLeft + node.offsetWidth) < node.scrollWidth;
if (!overflownOnRight) {
document.getElementById("info-banner").style.display = "none";
} else {
document.getElementById("info-banner").style.display = "block";
}
}
.scroll-container {
margin-top: 100px;
width: 100%;
height: 40px;
overflow-y: auto;
&::-webkit-scrollbar {
height: 0;
width: 0;
display: none;
}
}
.reference-point {
height: 30px;
width: 100%;
border: 5px dotted pink;
}
.scrollable {
width: 1200px;
height: 100%;
background: blue;
display: flex;
}
<div id="scroll-container" ontouchmove="onTouchMove(event)" class="scroll-container shadowed">
<div class="scrollable">
<div class="reference-point">
</div>
</div>
</div>
<div id="info-banner">
Scroll Right For More Content
</div>