The following snippet creates a blue box, then turns it purple. Ideally there would be a smooth transition from blue to purple, but it's practically instant instead. The only way I can achieve this is effect is with a timeout (see the second snippet).
let DOM = document.createElement('div');
DOM.classList.add('box');
document.body.appendChild(DOM);
DOM.classList.add('purple');
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 0.25s;
}
.box.purple {
background-color: purple;
}
let DOM = document.createElement('div');
DOM.classList.add('box');
document.body.appendChild(DOM);
window.setTimeout(() => {
DOM.classList.add('purple');
}, 100);
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 0.25s;
}
.box.purple {
background-color: purple;
}
A timeout seems like a bit of a hacky solution that could fail if the machine was slow or cause excess delay on faster machines. Primarily though, I'm looking for a better solution for this problem. Is there a way to await
appendChild
so that the class is added as soon as the element has been added?