I would like to make a css transition to an element that has display: none . Consider the following code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS Transition From Hidden</title> <style type="text/css"> div { -webkit-transition-property: all; -webkit-transition-duration: 2s; } div.hidden { display: none; opacity: 0; } div.from { opacity: 0; } </style> <script type="text/javascript"> function loaded() { var e = document.getElementById("foo"); e.className = "from"; window.webkitRequestAnimationFrame(function(t) { e.className = null; }); } </script> </head> <body onload="loaded()"> <div id="foo" class="hidden"> My test div </div> </body> </html>
I would like to switch from class="div.hidden" to class="" , that is, from display: none; opacity: 0; display: none; opacity: 0; before display: block; opacity: 1; display: block; opacity: 1; However, in chrome (at least), an object that has display: none is not animated. The element immediately goes to its final state.
My job for this is to first set the element to display: block; opacity: 0; display: block; opacity: 0; and then do the transition in the next frame (using requestAnimationFrame() . It's awkward and I can't find an explanation of this behavior in spec . I know that I could use the visibility attribute, but I don't want to use it because I don't want to put a hidden element.
So the questions are: is this the correct behavior or error? If this is the correct behavior, is there a better way to write code? Please note that I am not asking if there are any libraries that can do this, I want to know if there is a better way to do this directly in the DOM.
source share