I have this example in jsfiddle
I have a var as being the collision element:
var $div = $('.container');
and then the collision:
function test (){
$('.drag')
.drag("start",function( ev, dd ){
dd.limit = $div.offset();
dd.limit.bottom = dd.limit.top + $div.outerHeight()
- $( this ).outerHeight();
dd.limit.right = dd.limit.left + $div.outerWidth()
- $( this ).outerWidth();
})
.drag(function( ev, dd ){
$( this ).css({
top: Math.min( dd.limit.bottom, Math.max( dd.limit.top, dd.offsetY ) ),
left: Math.min( dd.limit.right, Math.max( dd.limit.left, dd.offsetX ) )
});
});
}
for:
<div class="container"></div>
<div class="drag xxx" style="left:40px;"></div>
<div class="drag xxx" style="left:120px;"></div>
<div class="drag xxx" style="left:200px;"></div>
This code implements collision detection of the child divs against the container div. How would I implement collision detection to make those 3 divs collide with each other?
I'm thinking to set up another div : var $divs = $('.xxx');
but im not sure how to use it within this example.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…