Using the following code, I can move two rectangles together with a transition, but after moving the two rectangles together, one of the rectangles moves from bottom to top again.
<script>
var rectangles = document.getElementsByClassName("rectangle");
for (var i = 0; i < rectangles.length; i++) {
var rectangle = rectangles[i];
var upButton = rectangle.getElementsByClassName("up")[0];
var downButton = rectangle.getElementsByClassName("down")[0];
upButton.addEventListener("click", function() {
moveFile(this.parentNode, "up");
});
downButton.addEventListener("click", function() {
moveFile(this.parentNode, "down");
});
}
function moveFile(currentRectangle, direction) {
var siblingRectangle = direction === "up" ? currentRectangle.previousElementSibling : currentRectangle.nextElementSibling;
if (siblingRectangle) {
currentRectangle.classList.add(direction === "up" ? "move-up" : "move-down");
siblingRectangle.classList.add(direction === "up" ? "move-down" : "move-up");
setTimeout(function() {
if (direction === "up") {
currentRectangle.parentNode.insertBefore(currentRectangle, siblingRectangle);
} else {
currentRectangle.parentNode.insertBefore(siblingRectangle, currentRectangle);
}
currentRectangle.classList.remove(direction === "up" ? "move-up" : "move-down");
siblingRectangle.classList.remove(direction === "up" ? "move-down" : "move-up");
}, 1000);
}
}
</script>
How to remove this extra movement?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/hgO1cxB
Comments
Post a Comment