与其说这是一个问题,不如说这是一个问题的解决方案。
很难找到解决方案,直到我偶然发现了 Hallodom ( How to disable scrolling temporarily? ) 添加的答案,它响应了一个稍微不同的问题。
我想明确记录问题的答案。其他解决方案是最受欢迎的,并且会增加对话。
hallodom 的解决方案是:
对于移动设备,您需要处理touchmove
事件:
$('body').bind('touchmove', function(e){e.preventDefault()})
并取消绑定(bind)以重新启用滚动。在iOS6和Android 2.3.3中测试
$('body').unbind('touchmove')
我使用了 Hallodom 的解决方案,只需将它们附加到单击 DOM 中的对象时调用的函数即可:
$('body').on('click', 'button', function(e){
if($(this).prop('checked')){
disable_scroll();
}else{
enable_scroll();
}
});
function disable_scroll() {
$('body').bind('touchmove', function(e){e.preventDefault()});
}
function enable_scroll() {
$('body').unbind('touchmove');
}
最佳答案
试试这个代码:
var scroll = true
$(document).bind('touchmove', function(){
scroll = false
}).unbind('touchmove', function(){
scroll = true
})
$(window).scroll(function() {
if ($('button').is(':checked') && scroll == false) {
$(document).scrollTop(0);
}
})
关于javascript - 如何在移动触摸屏设备上禁用滚动和重新启用滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17769990/