I am trying to animate use a page element using the CSS transition on opacity property. Attenuation works correctly, but Fading in does not. What am I doing wrong?
Some facts are really strange :
- Without using the
.no-display class, everything works as expected (but I have to use it). - Playback commands in the browser console work fine (but the function does not work).
Code:
HTML
<p><a href="javascript:fadeIn()">Fade in</a></p> <p><a href="javascript:fadeOut()">Fade out</a></p> <div class="no-display invisible" id="square"></div>
CSS
.no-display { display: none; } .invisible { opacity: 0; } #square { width: 500px; height: 500px; background-color: red; border: 1px solid black; -webkit-transition: opacity 2s ease; -moz-transition: opacity 2s ease; -ms-transition: opacity 2s ease; -o-transition: opacity 2s ease; transition: opacity 2s ease; }
Javascript
function fadeIn() { square.classList.remove("no-display"); square.classList.remove("invisible"); } function fadeOut() { square.classList.add("invisible"); setTimeout(function() { square.classList.add("no-display"); }, 2000 ); }
Or: http://jsfiddle.net/V2Sar/6/ .
Note. I can not use any frameworks like jQuery. I should only work with pure JavaScript.
source share