Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag Jquery draggable out of it's parent jquery dialog

I have a jquery dialog that is filled with draggable objects. The droppable target is outside of the dialog.

When I initiate a drag, the droppable responds correctly (visual indications that it is a droppable target) and after a drop the correct events trigger so I can correctly handle the drop.

The problem is that the dragged object stays visible only within the dialog, and does not "jump out".

I already have draggables that drag from one scrollable div to another without problem, but from a dialog to the page containing the dialog it does not work. The dialog contents scroll in whatever direction the drag is going.

My draggable arguments are as follows:

var draggableArguments={
     revert: 'invalid',
     helper:'clone',
     containment: 'DOM',
     zIndex: 999,
     addClasses: false
    }

   theObject.draggable(draggableArguments);

Any suggestions so that my draggable objects can cross the dialog boundaries?

Thanks.

like image 330
Daniel Gollás Avatar asked Sep 27 '10 18:09

Daniel Gollás


People also ask

How do I drag in jQuery?

Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port. If the value of this option is set to false, it will prevent the DOM elements to be dragged .

What is the syntax of the draggable function?

Syntax: $(selector, context). draggable ("action", [params]);

How do I delete a draggable element?

We can disable drag and drop on HTML elements by setting the draggable attribute to false . We set draggable to false so we can't drag it. We add event listeners for the dragstart and drop events with addEventListener .

How do I limit draggable area in HTML?

Limit draggable area using 'containment' option It is simple. All you have to do is, add an option called containment to the draggable() method. The containment option has a value parent.


2 Answers

Fixed, it was quite simple actually.

I just needed to use the appendTo option on the draggable so that the helper is appended to element where I want it to drag around (e.g. #page, a div that encompasses my page). This removes it from the dialog (which has an "overflow: auto" property, which adds scrollbars to extend the canvas so that the drag element is always within) and appends it to the #page element.

The only problem was my dialog had a pretty high zIndex, so I just incremented the zIndex option to be higher.

var draggableArguments={
     revert: 'invalid',
     helper:'clone',
     appendTo: '#page',
     containment: 'DOM',
     zIndex: 1500,
     addClasses: false
}

theObject.draggable(draggableArguments);
like image 100
Daniel Gollás Avatar answered Oct 24 '22 23:10

Daniel Gollás


You have to do this:

$('.my_draggable').draggable({
  helper:'clone',
  appendTo: 'body',
  scroll: false
});
like image 36
Ivelin Avatar answered Oct 25 '22 01:10

Ivelin