Create a gist now

Instantly share code, notes, and snippets.

Webkit CSS: fade in and out

I was looking for a CSS-only solution to fade a page overlay in and out. Obviously this should do the trick:

#loadingIndicator {position: absolute, z-index: 1;  opacity: 0; -webkit-transition: opacity 2s; }
body.loading #appLoadingIndicator { opacity: 0.8; }

However, this leaves an invisible layer on top of everything, which catches all clicks or other mouse events. This problem seems to have come up more often ( The solution is to use a transition on 2 properties, and move the layer out of the way after it has faded out:

#loadingIndicator {position: absolute, z-index: 1;  top: -1000px; opacity: 0.2; -webkit-transition-property: opacity, top; -webkit-transition-duration: 2s,0; -webkit-transition-delay: 0, 2s; }
body.loading #appLoadingIndicator { opacity: 0.8; top: 0; -webkit-transition-delay: 0,0;}

Because the "top" property is transitioned with a delay of 2s and a duration of 0, the layer jumps out of the way the moment the opacity reaches 0. However we need to make sure that on the fade in, this delay is not there, hence the extra -webkit-transition-delay in the body.loading #appLoadingIndicator.

Works like a charm (for me, on chrome...).

In stead of "top", one could also choose to use "height"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment