$(function () {
var pastDraggable = "";
$("#draggable1").draggable({
start: function () {
Positioning.initialize($(this));
},
});
$("#draggable2").draggable({
start: function () {
Positioning.initialize($(this));
},
});
$("#draggable3").draggable({
start: function () {
Positioning.initialize($(this));
},
});
$("#droppable").droppable({
//Event to accept a draggable when dropped on the droppable
drop: function (event, ui) {
$(this).addClass("ui-state-highlight").find("p").html("Dropped!");
//Get the current draggable object
var currentDraggable = $(ui.draggable).attr('id');
//If there is an object prior to the current one
if (pastDraggable != "") {
//Place past object into its original coordinate
$("#" + pastDraggable).animate($("#" + pastDraggable).data().originalLocation, "slow");
}
//Store the current draggable object
pastDraggable = currentDraggable;
},
//Event to accept a draggable when dragged outside the droppable
out: function (event, ui) {
var currentDraggable = $(ui.draggable).attr('id');
$(ui.draggable).animate($(ui.draggable).data().originalLocation, "slow");
}
});
});
var Positioning = (function () {
return {
//Initializes the starting coordinates of the object
initialize: function (object) {
object.data("originalLocation", $(this).originalPosition = {
top: 0,
left: 0
});
},
//Resets the object to its starting coordinates
reset: function (object) {
object.data("originalLocation").originalPosition = {
top: 0,
left: 0
};
},
};
})();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.