Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example using the X-Y coordinate solution.</p> <p><a href="http://jsfiddle.net/MAazv/">Working example on jsfiddle</a></p> <p>The example can be improved, but is a good starting point.</p> <p>Simply keeps track of the mouse location and checks if it appears to be inside any bounding boxes of the droppable objects. Hence, if the mouseup event fires on any one of them, dragged object is dropped.</p> <p>You can also use the coordinates of the object you are dragging for detecting if its on a droppable box, but it requires a little more code for finding the bounding box coords and using the mouse is enough for me.</p> <p>The code uses jQuery but no jQueryUI. I tested in Chrome, Firefox and Opera, but not IE :)</p> <p>I'm also adding the code to here if jsfiddle is not accessible.</p> <p>HTML</p> <pre><code>&lt;p&gt;Drag orange boxes to grey ones&lt;/p&gt; &lt;div class="droppable"&gt;&lt;/div&gt; &lt;div class="droppable"&gt;&lt;/div&gt; &lt;div class="droppable"&gt;&lt;/div&gt; &lt;div class="droppable"&gt;&lt;/div&gt; &lt;div class="draggable"&gt;&lt;/div&gt; &lt;div class="draggable"&gt;&lt;/div&gt; &lt;div class="draggable"&gt;&lt;/div&gt; </code></pre> <p>CSS</p> <pre><code>.droppable { width:50px; height:50px; float: left; background-color: #DDD; margin: 5px; } .draggable { width:40px; height:40px; float: right; background-color: #FC0; margin: 5px; cursor: pointer; } .dropped { background-color: #FC0; } .somethingover { background-color: #FCD; } </code></pre> <p>JS</p> <pre><code>var dragged, mousex, mousey, coordinates = []; var continueDragging = function(e) { // Change the location of the draggable object dragged.css({ "left": e.pageX - (dragged.width() / 2), "top": e.pageY - (dragged.height() / 2) }); // Check if we hit any boxes for (var i in coordinates) { if (mousex &gt;= coordinates[i].left &amp;&amp; mousex &lt;= coordinates[i].right) { if (mousey &gt;= coordinates[i].top &amp;&amp; mousey &lt;= coordinates[i].bottom) { // Yes, the mouse is on a droppable area // Lets change the background color coordinates[i].dom.addClass("somethingover"); } } else { // Nope, we did not hit any objects yet coordinates[i].dom.removeClass("somethingover"); } } // Keep the last positions of the mouse coord.s mousex = e.pageX; mousey = e.pageY; } var endDragging = function(e) { // Remove document event listeners $(document).unbind("mousemove", continueDragging); $(document).unbind("mouseup", endDragging); // Check if we hit any boxes for (var i in coordinates) { if (mousex &gt;= coordinates[i].left &amp;&amp; mousex &lt;= coordinates[i].right) { if (mousey &gt;= coordinates[i].top &amp;&amp; mousey &lt;= coordinates[i].bottom) { // Yes, the mouse is on a droppable area droptarget = coordinates[i].dom; droptarget.removeClass("somethingover").addClass("dropped"); dragged.hide("fast", function() { $(this).remove(); }); } } } // Reset variables mousex = 0; mousey = 0; dragged = null; coordinates = []; } var startDragging = function(e) { // Find coordinates of the droppable bounding boxes $(".droppable").each(function() { var lefttop = $(this).offset(); // and save them in a container for later access coordinates.push({ dom: $(this), left: lefttop.left, top: lefttop.top, right: lefttop.left + $(this).width(), bottom: lefttop.top + $(this).height() }); }); // When the mouse down event is received if (e.type == "mousedown") { dragged = $(this); // Change the position of the draggable dragged.css({ "left": e.pageX - (dragged.width() / 2), "top": e.pageY - (dragged.height() / 2), "position": "absolute" }); // Bind the events for dragging and stopping $(document).bind("mousemove", continueDragging); $(document).bind("mouseup", endDragging); } } // Start the dragging $(".draggable").bind("mousedown", startDragging); </code></pre>
    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