<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
#box {
width: 300px;
height: 300px;
background-color: olivedrab;
transition: all 0.5s;
}
</style>
</head>
<body>
<div id="box"></div>
</body>
<script>
const div = document.querySelector("div");
const move = (obj, x, y) => {
obj.style.transform = `translate(${x}px,${y}px)`;
return new Promise((resolve) => {
obj.ontransitionend = () => {
resolve();
};
});
};
div.onclick = function () {
move(this, 150, 0)
.then(() => move(this, 150, 150))
.then(() => move(this, 0, 150))
.then(() => move(this, 0, 0));
};
</script>
</html>