Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在移动触摸屏设备上禁用滚动和重新启用滚动_Javascript_Jquery_Jquery Mobile_Mobile_Scroll - Fatal编程技术网

Javascript 如何在移动触摸屏设备上禁用滚动和重新启用滚动

Javascript 如何在移动触摸屏设备上禁用滚动和重新启用滚动,javascript,jquery,jquery-mobile,mobile,scroll,Javascript,Jquery,Jquery Mobile,Mobile,Scroll,这与其说是一个问题,不如说是一个解决问题的办法 很难找到一个解决方案,直到我在中偶然发现了答案,由hallodom()添加,它对一个稍微不同的问题做出了响应 我想明确地记录这个问题的答案。其他解决方案是最受欢迎的,并将增加对话 hallodom的解决方案是: 对于移动设备,您需要处理触摸移动事件: $('body').bind('touchmove', function(e){e.preventDefault()}) 并取消绑定以重新启用滚动。在iOS6和安卓2.3.3中测试 $('body'

这与其说是一个问题,不如说是一个解决问题的办法

很难找到一个解决方案,直到我在中偶然发现了答案,由hallodom()添加,它对一个稍微不同的问题做出了响应

我想明确地记录这个问题的答案。其他解决方案是最受欢迎的,并将增加对话

hallodom的解决方案是:

对于移动设备,您需要处理
触摸移动
事件:

$('body').bind('touchmove', function(e){e.preventDefault()})
并取消绑定以重新启用滚动。在iOS6和安卓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);
    }
})

啊,抱歉,没有看到您的更改。您的示例运行得很好(尽管它会引起一些不安)。您如何将其绑定到复选框,以打开和关闭它,如我展示的示例中所示?我的答案可以帮助您吗?