In the following code, "Example A" does not animate, while "Example B" does.
The only difference is that A used to not be displayed, and B used to be hidden.
document.getElementById('set').onclick = function() {
document.getElementById('a').classList.add('clicked');
document.getElementById('b').classList.add('clicked');
};
document.getElementById('reset').onclick = function() {
document.getElementById('a').classList.remove('clicked');
document.getElementById('b').classList.remove('clicked');
};
.example {
background: blue;
transition: opacity 2000ms ease;
}
.example.clicked {
opacity: 0;
}
.example:not(.clicked) {
opacity: 1;
}
#a:not(.clicked) {
display: none;
}
#b:not(.clicked) {
visibility: hidden;
}
<button id="set">Show and fade out</button>
<button id="reset">Reset</button>
<div id="a" class="example">Example A</div>
<div id="b" class="example">Example B</div>
Why the two different behaviors? Why doesn't opacity always animate?