78

I am having an undesired effect when I drag a div from a container div which is set as overflow: scroll.

I have found an example of someone else where they have had the issue but I have been unable to find a resolution

Example on Paste bin

What happens is that the scroll is just increased, I can see why this would be the desired behaviour if you wanted to drag to a destination within the scrollable div but I want to be able to take it outside of its scrolling grasp.

user229044
  • 232,980
  • 40
  • 330
  • 338
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45

6 Answers6

111

I had a similar problem enabling a drag between two overflow-auto divs. With the help of the previous answers, I found that this combination works for me (Safari 5.0.3, jquery-1.4.4, jquery-ui-1.8.7):

appendTo: 'body',
containment: 'window',
scroll: false,
helper: 'clone'
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
marks_
  • 1,218
  • 2
  • 8
  • 4
56

appendTo

Element, SelectorDefault:'parent'

The element passed to or selected by the appendTo option will be used as the draggable helper's container during dragging. By default, the helper is appended to the same container as the draggable.

Code examples Initialize a draggable with the appendTo option specified.

$('.selector').draggable({ appendTo: 'body' });
Chad Grant
  • 44,326
  • 9
  • 65
  • 80
38

It certainly pays to pay attention to the documentation

http://docs.jquery.com/UI/Draggable#option-scroll

scroll

Type: Boolean
Default: true
If set to true, container auto-scrolls while dragging.

All who enter here, learn from my mistake, RT(F)M!!!

Community
  • 1
  • 1
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45
  • 4
    wasted 2 hours indeed. We should really RTFM – Herr Sep 07 '11 at 13:02
  • I had a similar problem with a accordion. I had a draggable inside an accordion panel and wanted to drag it outside the accordion, but the accordion panel had 'overflow' set to 'auto' and I never managed to get outside the accordion. Setting "scroll: false" on the accordion didn´t help. In the end I overruled the overflow set in jquery-ui. This make the height of my content panel look strange, so I had to had wire a given height. And that worked. – Asle G Feb 20 '16 at 19:40
20

The clone solution it works, but has two problems.

First: the clone are append to the body. Depending of your css, your element can change the styles, since before it starts, it's inside of another element and during the dragging, it will be directly on body element.

Second: Sometimes the element MUST move, and the clone let the object there.

So, the solution for this problems is:

$('.selector').draggable({
    helper: 'clone',
    start: function(){
        $(this).hide();             
    },
    stop: function(){
        $(this).show()
    }
});
2

Setting the scroll option does not stop children from being hidden in the overflow area. I have come up with a work-a-round that uses the helper option. Check it out:

http://pastebin.me/164f0a4073496563fe3179ddcec5fd6d

Also here is a reference to another post I made:

jquery ui draggable elements not 'draggable' outside of scrolling div

Community
  • 1
  • 1
Justin Bull
  • 7,785
  • 6
  • 26
  • 30
-1

You can also specify which types of elements you don't want to include.

<div class="draggable"> 
    <h2>This will drag the div</h2>
    <ul>
       <li>This won't drag the div</li>
    </ul>
</div>

Apply this cancel property to ignore the event in specified child elements

$('.draggable').draggable({cancel : 'ul'});
colin-higgins
  • 1,087
  • 10
  • 14