How do I disable a jquery-ui draggable?

后端 未结 9 1458
萌比男神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 :)

提交回复
热议问题