When using JavaScript to change the opacity of an element with transition: opacity 1s
, the expected duration of one second is met, as demonstrated here:
onload = e => {
var p = document.getElementsByTagName("p")[0];
p.style.opacity = 1;
};
p {
opacity: 0;
transition: opacity 1s;
}
<p>
Lorem ipsum...
</p>
However, if the element has display: none
and is first changed to display: initial
(to make it visible) before adjusting the opacity, the transition ceases to work. This can be seen here:
onload = e => {
var p = document.getElementsByTagName("p")[0];
p.style.display = "initial";
p.style.opacity = 1;
};
p {
display: none;
opacity: 0;
transition: opacity 1s;
}
<p>
Lorem ipsum...
</p>
What is causing this behavior?
Note: There is no intention to use a transition on the display attribute or search for workarounds related to it.