jQuery draggable + droppable: how to snap dropped element to dropped-on element

前端 未结 7 1289
无人及你
无人及你 2020-11-30 19:42

I have my screen divided into two DIVs. In the left DIV I have a few 50x50 pixel DIVs, in the right DIV I have an empty g

7条回答
  •  不知归路
    2020-11-30 20:25

    I found that when you do the drag, jQuery UI adds an inline to tell you where you dropped it. Below is a sample of the code that I used to snap it into place

    $('.droppable').droppable({ drop: function(ev, ui) { 
        //Get Details of dragged and dropped
        var draggedclass = ui.draggable.attr('class'),
            droppedclass = 'class' + $(this).attr('name').toLowerCase();
    
        //update the classes so that it looks od.       
        ui.draggable.removeClass(draggedclass).addClass(droppedclass);  
        ui.draggable.removeAttr('style');
    });
    

提交回复
热议问题