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

白昼怎懂夜的黑 提交于 2019-12-18 12:06:06

问题


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 that are marked as draggable. I want it so when you are dragging an element and it is hovered over a droppable area that area either highlights or has a border so they know they can drop it there.

As secondary question, all my draggable elements have a display:block, a width and a float on them, so they look nice and neat in my droppable areas. When items are dropped they seem to get a position set to them as they no longer float nice and neat like the rest of my items. For reference, here is my javascript.

$('.drag_span').draggable({
    revert: true
});
$('.drop_div').droppable({
    drop: handle_drop_patient
});

function handle_drop_patient(event, ui) {
    $(this).append($(ui.draggable).clone());
    $(ui.draggable).remove();
}

回答1:


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!" );
    }
});



回答2:


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();
}



回答3:


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
});


来源:https://stackoverflow.com/questions/9879414/how-do-i-highlight-a-droppable-area-on-hover-using-jquery-ui-draggable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!