我使用此代码将整个文档锁定在位置上(在单击事件上),它完全符合锁定滚动所需的内容,但现在我需要允许在一个div中滚动,而整个文档现在已锁定。如何使用此代码执行此操作?
document.addEventListener('touchmove', function(e) { e.preventDefault(); }, { passive: false });
注意:锁定的滚动位置不位于文档的顶部。
向要保持滚动的div添加一个类,并在处理程序中只检查目标div以查看它是否包含或在示例情况下不包含div,然后再阻止默认行为。
document.addEventListener('touchmove', function(e) {
if (!e.target.classList.contains('keep-moving')) e.preventDefault();
}, { passive: false });