How do I disable a jquery-ui draggable?

后端 未结 9 1450
萌比男神i
萌比男神i 2020-11-29 04:46

How do I disable a jQuery draggable, e.g. during an UpdatePanel postback?

相关标签:
9条回答
  • 2020-11-29 05:18

    I have a simpler and elegant solution that doesn't mess up with classes, styles, opacities and stuff.

    For the draggable element - you add 'start' event which will execute every time you try to move the element somewhere. You will have a condition which move is not legal. For the moves that are illegal - prevent them with 'e.preventDefault();' like in the code below.

        $(".disc").draggable({
            revert: "invalid", 
            cursor: "move",
            start: function(e, ui){                
                console.log("element is moving");
    
                if(SOME_CONDITION_FOR_ILLEGAL_MOVE){
                    console.log("illegal move");
                    //This will prevent moving the element from it's position
                    e.preventDefault();
                }    
    
            }
        });
    

    You are welcome :)

    0 讨论(0)
  • 2020-11-29 05:19

    It took me a little while to figure out how to disable draggable on drop—use ui.draggable to reference the object being dragged from inside the drop function:

    $("#drop-target").droppable({
        drop: function(event, ui) {
            ui.draggable.draggable("disable", 1); // *not* ui.draggable("disable", 1);
            …
        }
    });
    

    HTH someone

    0 讨论(0)
  • 2020-11-29 05:22

    Change draggable attribute from

    <span draggable="true">Label</span>
    

    to

    <span draggable="false">Label</span>
    
    0 讨论(0)
  • 2020-11-29 05:26

    To temporarily disable the draggable behavior use:

    $('#item-id').draggable( "disable" )
    

    To remove the draggable behavior permanently use:

    $('#item-id').draggable( "destroy" )
    
    0 讨论(0)
  • 2020-11-29 05:31

    Could create a DisableDrag(myObject) and a EnableDrag(myObject) function

    myObject.draggable( 'disable' )
    

    Then

    myObject.draggable( 'enable' )
    
    0 讨论(0)
  • 2020-11-29 05:31

    The following is what this would look like inside of .draggable({});

    $("#yourDraggable").draggable({
        revert: "invalid" ,
        start: function(){ 
            $(this).css("opacity",0.3);
        },
        stop: function(){ 
            $(this).draggable( 'disable' )
        },
        opacity: 0.7,
        helper: function () { 
            $copy = $(this).clone(); 
            $copy.css({
                "list-style":"none",
                "width":$(this).outerWidth()
            }); 
            return $copy; 
        },
        appendTo: 'body',
        scroll: false
    });
    
    0 讨论(0)
提交回复
热议问题