How do I highlight a droppable area on hover using jquery ui draggable

前端 未结 3 1780
我寻月下人不归
我寻月下人不归 2020-12-29 19:43

I actually have two questions, the on in the title being the main one. I have multiple div elements on the page marked as droppable. Inside these div elements I have spans

相关标签:
3条回答
  • 2020-12-29 20:33

    This should work for adding a highlight on hover.

    $('.drop_div').droppable({
         hoverClass: "highlight",
         drop: handle_drop_patient,
    });
    

    Then create a css class for highlight that sets the border or changes the background color or whatever you'd like.

    .highlight {
        border: 1px solid yellow;
        background-color:yellow;
    }
    

    As far as the position is concerned you can reapply css once the drop is complete.

    function handle_drop_patient(event, ui) {
         $(this).append( $(ui.draggable).clone().css({'float':'left','display':'block'}) );
         $(ui.draggable).remove();
    }
    
    0 讨论(0)
  • 2020-12-29 20:33

    FYI: hoverClass has been deprecated in favour of classes option. It should now be:

    $('.drop_div').droppable({
        classes: {
          'ui-droppable-hover': 'highlight'
        },
        drop: handle_drop_patient
    });
    
    0 讨论(0)
  • 2020-12-29 20:38

    Check out http://jqueryui.com/demos/droppable/#visual-feedback.

    Ex:

    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
        hoverClass: "ui-state-active",
        drop: function( event, ui ) {
            $( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                    .html( "Dropped!" );
        }
    });
    $( "#draggable2" ).draggable();
    $( "#droppable2" ).droppable({
        accept: "#draggable2",
        activeClass: "ui-state-hover",
        drop: function( event, ui ) {
            $( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                    .html( "Dropped!" );
        }
    });
    
    0 讨论(0)
提交回复
热议问题