7

is it possible to say "draggable div's -> no overlap" ?

I think the mainproblem is the position:absolute; ... right?

kind reagards Peter

1
  • 2
    Hi Peter, we don't know because you have forgotten to supply an example of what you are doing! We can guess that you are using position: absolute somewhere, and some divs, but we know nothing else!
    – Fenton
    Aug 14, 2010 at 7:38

2 Answers 2

16

You can try jquery-collision plus jquery-ui-draggable-collision. Full disclosure: I just wrote and released these on sourceforge.

The first allows this:

var hit_list = $("#collider").collision(".obstacle");

which is the list of all ".obstacle" that overlap "#collider".

The second allows:

$("#collider").draggable( { obstacle: ".obstacle" } );

Which gives you (among other things), a "collision" event to bind to:

$("#collider").bind( "collision", function(event,ui){...} );

And you can even set:

$("#collider").draggable( { obstacle: ".obstacle", preventCollision: true } );

to prevent "#collider" from ever overlapping any ".obstacle" while dragging.

8
  • 3
    Doesn't work 100% with jQuery version 1.8.20. Alternative is to use the supplied 1.8.12 version
    – 321X
    May 17, 2012 at 10:04
  • 1
    @321X - thanks, i'll look into why it doesn't work with 1.8.20 -- could you file a bug fix ticket on sourceforge with an example, please?
    – eruciform
    Aug 1, 2012 at 3:53
  • 1
    @eruciform, this plugin is AMAZING! The only problem is, that I cannot get it to work with jQuery 1.10.1 and jQuery UI 1.10.3. Is there anything that you can do ? Feb 5, 2014 at 13:52
  • This version (raw) of jquery-ui-draggable-collision works with v1.11.1.
    – rgajrawala
    Nov 8, 2014 at 17:35
  • 2
    @usandfriends: Nevermind. Found this fork: github.com/dsbaars/jq-ui-draggable-collision Feb 5, 2015 at 7:07
5

There isn't a built-in function for collision detection in jQuery UI draggable, unless you're actually after something like sorting. The closest thing that's built-in to what you want is sortable's plceholder, which looks like this.

The short version is collision detection with every dragged element isn't trivial in terms of performance (depending on the number of elements), so it's left out of the library since it's a very rare request. You could however calculate the collisions yourself inside the draggable's drag event if you really do need the collision detection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.