I'm trying to ditch setTimeout-based JavaScript in favor of a CSS transition for a one-second highlight. Instead of changing the background color instantly or even fading between two background colors, I'd like to first switch to yellow immediately, then fade to the proper color.
The way I imagine it should work is I set the class to something with a yellow background, then change it to a class with a transition property and a new color. This just fades to the final color instead of starting from yellow. I don't think I can attach the transition to the element irrespective of class or the two class changes will overlap and run their animations at the same time.
Clearly I need to be structuring things in a different way to accomplish this. I must not be grasping the basics of when transitions actually occur when attached to classes that are added and removed.
<style>
.highlight { background-color: yellow; }
.selected { background-color: black; -moz-transition: background-color 2s linear; }
</style>
<div id="samp" onclick="flash();">SAMPLE</div>
<script>
function flash() {
document.getElementById("samp").className = "highlight";
document.getElementById("samp").className = "selected";
}
</script>