How to programmatically disable page scrolling with jQuery

后端 未结 23 2122
滥情空心
滥情空心 2020-11-22 08:09

Using jQuery, I would like to disable scrolling of the body:

My idea is to:

  1. Set body{ overflow: hidden;}
  2. Capture the current
23条回答
  •  长发绾君心
    2020-11-22 08:40

    This is what I ended up doing:

    CoffeeScript:

        $("input").focus ->
            $("html, body").css "overflow-y","hidden"
            $(document).on "scroll.stopped touchmove.stopped mousewheel.stopped", (event) ->
                event.preventDefault()
    
        $("input").blur ->
            $("html, body").css "overflow-y","auto"
            $(document).off "scroll.stopped touchmove.stopped mousewheel.stopped"
    

    Javascript:

    $("input").focus(function() {
     $("html, body").css("overflow-y", "hidden");
     $(document).on("scroll.stopped touchmove.stopped mousewheel.stopped", function(event) {
       return event.preventDefault();
     });
    });
    
    $("input").blur(function() {
     $("html, body").css("overflow-y", "auto");
     $(document).off("scroll.stopped touchmove.stopped mousewheel.stopped");
    });
    

提交回复
热议问题