Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected HTML DIV element draggable using JQueryUi draggable

I've an array of jQuery objects which are draggable.
What I want is when any element present in the array is dragged, All other elements should also be dragged.

Following is sample code that I have tried but I haven't got any success

$(event.target).parents('.ui-class-name').draggable({
   disabled : false,  
   helper: function() {
     var allSelectedEle = $($selected).map( function() {
         return this.toArray() 
     });
     return allSelectedEle;
}
});

Here $selected is the array of jQuery object

Update: Here is the sample markup

like image 276
bitsbuffer Avatar asked Jul 26 '26 12:07

bitsbuffer


1 Answers

You need to save initial coordinates of elements, and update them while you dragging them (demo):

var els = $('.eq-ui-widget')
var coords = { x: 0, y: 0 }

function getSelected() {
    return els.filter('.selected')
}

els
.draggable({
    disbled: true,
    drag: function(e, ui) {
        getSelected().each(function() {
            var orig = $(this).data().orig
            $(this).css({
                top: orig.top + (ui.position.top - coords.y) ,
                left: orig.left + (ui.position.left - coords.x)
            })
        });
    },
    start: function(e, ui) {
        coords.x = ui.position.left;
        coords.y = ui.position.top;

        getSelected().each(function() {
            $(this).data().orig = $(this).position();
        });
    }
})
.on('click', function(event) {
    if(!event.ctrlKey) return;
    $(event.target).toggleClass('selected');
    /*logic for dragging  all selected elements simultaneously*/

    var selected = getSelected()
    els.draggable('option', 'disabled', true )
    selected.draggable('option', 'disabled', false )
});
like image 115
krasu Avatar answered Jul 28 '26 02:07

krasu