draggable overlap detection trouble
I have many div's that are spread across the screen. Each one is jQuery draggable on the x-axis. You could imagine it as a ui-slider with many handles. How can I write a function that will check whether when anyone of the div's which I may be dragging has overlapped any other div or div's?
I would like a to have a global status indicating if any are over lapping and also a way to determine the id's of only the div's which overlap.
This is what I have created so far but clearly I have buggered it up... http://jsfiddle.net/bcraig/2vsvdcw4/7/
- <div class="box" id="box0" style=" width:40px;">0</div><div class="box" id="box1" style=" width:50px;">1</div>
- <div class="box" id="box2" style=" width:80px;">2</div>
- <div class="box" id="box3" style=" width:100px;">3</div>
- <br><br>
- <p class="result"></p>
- div { height:40px; float:left; font-size:10px; }
- #box0 { background-color:red; width:40px; }
- #box1 { background-color:blue; width:40px; }
- #box2 { background-color:orange; width:40px; }
- #box3 { background-color:green; width:40px; }
-
- $( "div" ).each(function( index ) {
- var this_right = $(this).position().left + $(this).width();
- $(this).draggable({
- axis: "x",
- drag: function(event,ui) {
- $( "div" ).each(function( index ) {
- var ui_right = (ui.position.left + $(ui.helper).width());
- if(
- ui.position.left < $(this).position().left && ui_right > $(this).position().left ||
- ui.position.left < $(this).position().left && ui_right > this_right ||
- ui.position.left < this_right && ui.position.left > $(this).position().left ||
- ui.position.left > this_right && ui.position.left < this_right
- ){
- $('.result').html('overlapped');
- } else $('.result').html('clear');
- });
-
- $( "div" ).each(function( index ) {
- $(this).html('left:'+$(this).position().left+'<br>right:'+$(this).position().left+$(this).width()));
- });
- }
- });
- });