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
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 )
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With