What I want to do is play a transition from the beginning if the user presses the button again. The problem right now is if the user clicks twice or more the button it will not start the two animations properly (one after the other).
So right now transitions are working but I have trouble making this work if the user clicks the button more than once.
let button = document.querySelector("button")
let box = document.querySelector(".box")
button.onclick = () => {
box.style.transform = "";
anim1(function(){
anim2(function() {
})
})
}
function anim1(cb) {
box.style.transition = ""
box.clientHeight;
box.style.transition = "all 1s";
box.style.transform = "translateX(50px)";
setTimeout(() => {
cb()
},1000)
}
function anim2(cb) {
box.style.transition = ""
box.clientHeight;
box.style.transition = "all 1s";
box.style.transform = "translateX(350px)";
setTimeout(() => {
cb()
},1000)
}
Live example https://jsfiddle.net/kzjpb55f/