在 ipad pro 上,我尝试在弹出窗口打开时禁用滚动,并在弹出窗口关闭时启用向后滚动。 我找到了一个完美运行的解决方案,但它也阻止了内部元素滚动。
function preventDefault(e){
e.preventDefault();
}
function disableScroll() {
document.body.addEventListener('touchmove', preventDefault, { passive: false });
}
function enableScroll() {
document.body.removeEventListener('touchmove', preventDefault);
}
在这里,我在弹出窗口中有一个可滚动的元素,并且具有类名称window-content。上述解决方案阻止了整个页面的滚动,这意味着它也阻止了弹出窗口内部内容的滚动。 我已尝试使用下面的代码来允许在弹出窗口内滚动,但它不起作用。
function disableScroll() {
document.body.addEventListener('touchmove', preventDefault, { passive: false });
document.getElementsByClassName('window')[0].getElementsByClassName('window-content')[0].removeEventListener('touchmove', preventDefault);
}
我想允许在弹出窗口内滚动,并且它应该只对正文禁用。谢谢。
最佳答案
我更改了 PreventDefault 函数以避免特定元素。
preventDefault: function(e) {
if($(e.target).closest(".k-window").length == 0) {
e.preventDefault();
}
},
现已修复。
关于javascript - 如何避免特定元素滚动被禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725816/