jQuery Drag and Drop to highlighted boxes
I have this script. Well I wanted the soldier to be dropped only开发者_JS百科 at the highlighted boxes. I tried accept: $(selector) and revert: "invalid" but it seems that it disallows it to be dropped even on the un-highlighted boxes.
Initially make all droppables disabled, then during the drag start routine make $('.cell .validmove')
droppable, and add a revert: "invalid"
option to the draggable, so that it moves back when not put in a valid cell, if that is what you want. Incidentally, you would probably be better having the routine you have in the drag()
event under start()
and reverting on stop()
, as the drag()
event fires continuously as you move.
unit.draggable({
start: function() {
startGrid.addClass("validmove").droppable("option", "disabled", false);
},
stop: function() {
startGrid.removeClass("validmove").droppable("option", "disabled", true);
},
cursor: "move",
revert: "invalid"
});
If you want the unit to snap to the grid, either instruct it to using the grid/snap options in draggable()
, or add some code in the drop()
event to animate the unit in to place.
NB. The CSS isn't working properly in Chrome, the grid does not show up, works fine in Firefox.
精彩评论