Jquery draggable / droppable change snap
I am trying to create a series of div / droppable divs. Once div1 is reset, I will create a new div and bind it to a new position. However, the anchor position remains the same when div1 was dropped and div2 is dragged: ( http://jsfiddle.net/kLpgukkj/2/ )
var trackID = "path0";
var trackType = "a";
$('.map-track-box').droppable({
tolerance: 'touch',
drop: function(event,ui){
// RETURNS HOME
$('#trackDragger').animate({ top: '30px', left: '20px' },500);
}
});
$('#'+trackID).droppable({
tolerance: 'touch',
drop: function(event,ui){
// IN POSITION
var pos = $('#'+trackID).position();
$('#trackDragger').animate({ top: pos.top, left: pos.left },500,function() {
$('#draggerBox').append("<div class='"+trackType+"' style='z-index:10;position:absolute;top:"+pos.top+"px;left:"+pos.left+"px;'></div>");
$('#trackDragger').css({ top: '30px', left: '20px' },500);
$('#trackDragger').attr('class', 'd');
$('#'+trackID).removeClass('ui-droppable'); // I attempted to remove the added class, and add it to the new element, but without luck
if(trackID=="path0") {
trackID = "path1";
}else if(trackID=="path1") {
trackID = "path2";
trackType = "d";
}else if(trackID=="path2") {
trackID = "path3";
trackType = "e";
}
$('#'+trackID).addClass('ui-droppable');
});
}
});
$('#trackDragger').draggable({
revert: 'invalid',
start: function(event,ui) {
$('#trackDragger').attr('class', trackType);
},
snap: "#"+trackID, // This keeps snapping to initial div (path0) even though trackID is being changed to path1,path2 etc
snapMode: "inner",
stop: function(){
var dragPos = $('#trackDragger').position();
if(dragPos.left < 101 && dragPos.top < 91) {
$('#trackDragger').attr('class', 'd');
}
//$(this).draggable('option','revert','invalid');
}
});
This works well, but the element being dragged retains its anchor to path0 (the starting div being dragged). Is there a way to force the new anchor location?
thank
source to share
As for the fiddle you mentioned, you droppable
only bind the event to one element. it
$('#'+trackID) // Your variable don't change from "path0" to any other.
As I understand it, you want the other black boxes to have the droppable bind event as well.
Just change the code to the following in the section droppable
.
$('.path').droppable({});
This will bind an event to every element .path
on the page.
see updated script here .
source to share