简体   繁体   中英

jQuery Drag and Drop to highlighted boxes

I have this script. Well I wanted the soldier to be dropped only 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM