Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$("#right").click(function(){ $(".block").animate({"left": "+=50px"}, "fast", checkCollisions); }); $("#left").click(function(){ $(".block").animate({"left": "-=50px"}, "fast", checkCollisions); }); $("#up").click(function(){ $(".block").animate({"top": "-=50px"}, "fast", checkCollisions); }); $("#down").click(function(){ $(".block").animate({"top": "+=50px"}, "fast", checkCollisions); }); function getPositions(box) { var $box = $(box); var pos = $box.position(); var width = $box.width(); var height = $box.height(); return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ]; } function comparePositions(p1, p2) { var x1 = p1[0] &lt; p2[0] ? p1 : p2; var x2 = p1[0] &lt; p2[0] ? p2 : p1; return x1[1] &gt; x2[0] || x1[0] === x2[0] ? true : false; } function checkCollisions(){ var box = $(".bomb")[0]; var pos = getPositions(box); var pos2 = getPositions(this); var horizontalMatch = comparePositions(pos[0], pos2[0]); var verticalMatch = comparePositions(pos[1], pos2[1]); var match = horizontalMatch &amp;&amp; verticalMatch; if (match) { $("body").append("&lt;p&gt;COLLISION !!!&lt;/p&gt;"); } }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>.block { position:absolute; background-color:#abc; left:50px; width:90px; height:90px; margin:5px; } .bomb { position:absolute; background-color:red; left:250px; width:40px; height:40px; margin:5px; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;button id="left"&gt;left&lt;/button&gt; &lt;button id="right"&gt;right&lt;/button&gt; &lt;button id="up"&gt;up&lt;/button&gt; &lt;button id="down"&gt;down&lt;/button&gt; &lt;div class="block"&gt;&lt;/div&gt; &lt;div class="bomb"&gt;&lt;/div&gt;</code></pre> </div> </div> </p> <p>You can use <a href="http://sourceforge.net/projects/jquerycollision/" rel="nofollow noreferrer">JQuery Collision</a> and <a href="http://sourceforge.net/projects/jquidragcollide/" rel="nofollow noreferrer">JQuery UI Draggable Collision</a>.</p> <p>JQuery Collision extension returns the collisions between two selectors. Handles padding, margin, borders, and can determine either overlap or portion outside.</p> <p>With <strong>JQuery Collision</strong> you can find all overlaps:</p> <pre><code>var list = $("#selector").collision(".obstacle"); </code></pre> <p><em>Returns a list of all elements that overlap "#selector".</em></p> <p>With <strong>JQuery UI Draggable</strong>, you can bind an event when you drag an element and a collision happens and you can prevent the collision:</p> <pre><code>$("#selector").draggable( { obstacle: ".obstacle", preventCollision: true } ); </code></pre> <p>Some plugins for collision detection:<br/> <a href="http://archive.plugins.jquery.com/project/collidable" rel="nofollow noreferrer"><strong>Collidable Draggables</strong></a>,<br/> <a href="http://www.48design.de/news/2009/11/20/kollisionsabfrage-per-jquery-plugin-update-v11-8/" rel="nofollow noreferrer"><strong>Collision Check Plugin</strong> v1.1</a>,<br/> <a href="http://blog.threedubmedia.com/2008/08/eventspecialdrop.html" rel="nofollow noreferrer"><strong>$.event.special.drop</strong></a></p> <p>Also :</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var overlaps = (function () { function getPositions( elem ) { var pos, width, height; pos = $( elem ).position(); width = $( elem ).width(); height = $( elem ).height(); return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ]; } function comparePositions( p1, p2 ) { var r1, r2; r1 = p1[0] &lt; p2[0] ? p1 : p2; r2 = p1[0] &lt; p2[0] ? p2 : p1; return r1[1] &gt; r2[0] || r1[0] === r2[0]; } return function ( a, b ) { var pos1 = getPositions( a ), pos2 = getPositions( b ); return comparePositions( pos1[0], pos2[0] ) &amp;&amp; comparePositions( pos1[1], pos2[1] ); }; })(); $(function () { var area = $( '#area' )[0], box = $( '#box0' )[0], html; html = $( area ).children().not( box ).map( function ( i ) { return '&lt;p&gt;Red box + Box ' + ( i + 1 ) + ' = ' + overlaps( box, this ) + '&lt;/p&gt;'; }).get().join( '' ); $( 'body' ).append( html ); });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>body { padding: 30px; color: #444; font-family: Arial, sans-serif; } h1 { font-size: 24px; margin-bottom: 20px; } #area { border: 2px solid gray; width: 500px; height: 400px; position: relative; } #area &gt; div { background-color: rgba(122, 122, 122, 0.3); position: absolute; text-align: center; font-size: 50px; width: 60px; height: 60px; } #box0 { background-color: rgba(255, 0, 0, 0.5) !important; top: 150px; left: 150px; } #box1 { top: 260px; left: 50px; } #box2 { top: 110px; left: 160px; } #box3 { top: 200px; left: 200px; } #box4 { top: 50px; left: 400px; } p { margin: 5px 0; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;h1&gt;Detect overlapping&lt;/h1&gt; &lt;div id="area"&gt; &lt;div id="box0"&gt;&lt;/div&gt; &lt;div id="box1"&gt;1&lt;/div&gt; &lt;div id="box2"&gt;2&lt;/div&gt; &lt;div id="box3"&gt;3&lt;/div&gt; &lt;div id="box4"&gt;4&lt;/div&gt; &lt;/div&gt;</code></pre> </div> </div> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload