最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable scrolling and re-enable scrolling on a mobile touch screen device - Stack Overflow

programmeradmin2浏览0评论

This is not so much of a question as it is an solution to the question.

It was difficult find a solution until I stumbled across the answer in added by hallodom (How to disable scrolling temporarily?) which was responding to a slightly different problem.

I wanted to explicitly document an answer to the problem. Other solutions are most wele and would add to the conversation.

hallodom's solution was:

For mobile devices, you'll need to handle the touchmove event:

$('body').bind('touchmove', function(e){e.preventDefault()})

And unbind to re-enable scrolling. Tested in iOS6 and Android 2.3.3

$('body').unbind('touchmove')

I used hallodom's solution by simply attaching them to functions called when an object in my DOM was clicked:

    $('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');
    }

This is not so much of a question as it is an solution to the question.

It was difficult find a solution until I stumbled across the answer in added by hallodom (How to disable scrolling temporarily?) which was responding to a slightly different problem.

I wanted to explicitly document an answer to the problem. Other solutions are most wele and would add to the conversation.

hallodom's solution was:

For mobile devices, you'll need to handle the touchmove event:

$('body').bind('touchmove', function(e){e.preventDefault()})

And unbind to re-enable scrolling. Tested in iOS6 and Android 2.3.3

$('body').unbind('touchmove')

I used hallodom's solution by simply attaching them to functions called when an object in my DOM was clicked:

    $('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');
    }
Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked Jul 21, 2013 at 7:17 alutzalutz 1921 gold badge1 silver badge17 bronze badges 1
  • My answer could help you ? – Lucas Willems Commented Jul 21, 2013 at 8:04
Add a ment  | 

1 Answer 1

Reset to default 2

Try this code :

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);
    }
})
发布评论

评论列表(0)

  1. 暂无评论