This is (obviously) part of a bigger project, but I am trying to trigger a CS transition on setTimeout
.
(I know about using CSS animations, but this is not just about repeated transitions).
A CSS transition will occur when a property changes. For my own purposes, I use setAttribute
as that isolates the behaviour from other class-related stuff, but the behaviour below works the same.
Assuming the HTML and other code is in place, here is simplified version:
var test=document.querySelector('div#test');
window.setInterval(doit,4000);
function doit() {
test.removeAttribute('thing');
test.innerHTML=new Date();
test.setAttribute('thing',null);
}
The CSS is:
div#test {
opacity: 0;
}
div#test[thing] {
transition: opacity 1s;
opacity: 1;
}
The trick I am trying is to change clear the attribute and then set it again — that should trigger the CSS change. It doesn’t work as above. However, if I delay setting the attribut by 0 seconds, it does work:
function doit() {
test.removeAttribute('thing');
window.setTimeout(function() {
test.setAttribute('thing',null);
},0);
test.innerHTML=new Date();
}
The question is: why does it not work unless I add this minuscule delay, and is there a better way to do this?
I would like an answer without jQuery.
Note: I have accepted an answer below. The following code will work:
function doit() {
test.removeAttribute('thing');
test.offsetHeight; // force update
test.innerHTML=new Date();
test.setAttribute('thing',null);
}
Thanks