How to disable page scroll while dragging draggable in jquery?

后端 未结 3 1458
天涯浪人
天涯浪人 2021-01-03 01:21

I have a draggable defined this way:

$(\"#drag_area a\").live(\"mouseup\", function() {

    var object = $(this);
    var class_array = $(this).attr(\"class         


        
相关标签:
3条回答
  • 2021-01-03 01:34

    You'll be using all your draggable elements without any container, i.e. why when you Drag-n-Drop your elements the whole page scrolls due to dragging.

    Instead of that do as:

    <div class="dragWrapper">
       <!-- Place all your draggable elements here -->
    </div>
    

    set a max-height and overflow of the dragWrapper class as:

    .dragWrapper {
       max-height: 400px;
       overflow: auto;
    }
    

    Now when you Drag-n-Drop your elements, instead of your body, scroll will be inside the container only.

    Hope that will do the trick for you(which already did ;).

    0 讨论(0)
  • 2021-01-03 01:46

    I solved that problem with one line of css on dragging element touch-action: none;

    0 讨论(0)
  • 2021-01-03 01:54

    Take a look at this jsfiddle.
    I've made it based on this SO question: How to disable scrolling temporarily?

    Here is an excerpt:

    function disableScroll() {
        if (window.addEventListener) // older FF
        window.addEventListener('DOMMouseScroll', preventDefault, false);
        window.onwheel = preventDefault; // modern standard
        window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
        window.ontouchmove = preventDefault; // mobile
        document.onkeydown = preventDefaultForScrollKeys;
    }
    
    $("#draggable").draggable({
        start: function () {
            disableScroll();
        }
    });
    
    0 讨论(0)
提交回复
热议问题