Adding a function to jQuery draggable revert “event”

后端 未结 2 1359
孤独总比滥情好
孤独总比滥情好 2020-12-03 13:43

What I have:

I have a draggable div with a revert parameter set as \"invalid\".

What I need:

When the revert occurs I want to trigger a CSS chang

相关标签:
2条回答
  • 2020-12-03 14:27

    You want to do something like this:

     $('#peg_icon').draggable({
            revert: function (socketObj) {
            if (socketObj === true) {
                // success
                return false;
            }
            else {
                // reverting
                return true;
            }
        },
            scroll: false,
            stack: "#peg_icon",
            drag: function(event, ui) {
                $('#peg_icon').css('background-image','url(images/peg-icon-when-dragged.png)');
            }
        });
    

    DEMO: http://jsfiddle.net/yTMwu/35/

    Hope this helps!

    0 讨论(0)
  • 2020-12-03 14:30

    It turns out that revert can accept a method. See this for a complete example: http://jsfiddle.net/mori57/Xpscw/

    Specifically:

    $(function() {
        $("#ducky").draggable({
            revert: function(is_valid_drop){
                console.log("is_valid_drop = " + is_valid_drop);
                // when you're done, you need to remove the "dragging" class
                // and yes, I'm sure this can be refactored better, but I'm 
                // out of time for today! :)
                if(!is_valid_drop){
                   console.log("revert triggered");
                    $(".pond").addClass("green");
                    $(this).removeClass("inmotion");
                   return true;
                } else {
                    $(".pond").removeClass("green");
                    $(this).removeClass("inmotion");
                }
            },
            drag: function(){
                // as you drag, add your "dragging" class, like so:
                $(this).addClass("inmotion");
            }
        });
        $("#boat").droppable({
          drop: function( event, ui ) {
            $(this)
              .addClass("ui-state-highlight");
          }
        });
      });
    

    Hope this helps... I'm guessing that whatever you were trying to modify was the issue, not the attempt to use revert with a function call. Take a look again at what CSS you were trying to set, and see if maybe your issue was in there.

    0 讨论(0)
提交回复
热议问题