2

I would like to make a div appear with fade effect :

<div id="toto" style="background:red;width:200px;height:200px"></div>

The simplest way I know is with CSS properties "transition" and "opacity". In my mind, this code should works (but it doesn't) :

document.getElementById("toto").style.opacity = 0;
document.getElementById("toto").style.transition = "all 5s ease";
document.getElementById("toto").style.opacity = 1;

(See at : http://jsfiddle.net/87q44ysg/1/#share)

Anyone know why ?? I have found this hack but I can't explain why it works in this case only... :

document.getElementById("toto").style.opacity = 0;
window.setTimeout(function(){
  document.getElementById("toto").style.transition = "all 5s ease";
  document.getElementById("toto").style.opacity = 1;
}, 1);

(See at : http://jsfiddle.net/36009t1x/#share)

Thank you !

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
Klitzounet
  • 23
  • 3

4 Answers4

2

First case

document.getElementById("toto").style.opacity = 0;
document.getElementById("toto").style.transition = "all 5s ease";
document.getElementById("toto").style.opacity = 1; 

This happens because each line in above javascript is invoked immediately and it is similar to setting these css rules:

#toto {
    opacity: 0;
    transition: all 5s ease;
    opacity: 1;
}

as you can see the second occurence of opacity will override the value 0, so in result it will be 1.

#toto {
    transition: all 5s ease;
    opacity: 1;
}

There is no animation in this case just static opacity that isn't going to change.

Second case

document.getElementById("toto").style.opacity = 0;
window.setTimeout(function(){
  document.getElementById("toto").style.transition = "all 5s ease";
  document.getElementById("toto").style.opacity = 1;
}, 1);

In this case you initially set the opacity to 0. And after 1ms you change the css rules, so there is a change in opacity from 0 to 1. That's why the animation was fired.

Regards.

luke
  • 3,531
  • 1
  • 26
  • 46
0

You should avoid setting styles in plain Javascript and move this to CSS. As for your question, it's not standardised and each browser can do whatever developers think is right. Probably, transition property is not really applied until reflow or something like this. See this question for more detailed testing: Clean way to programmatically use CSS transitions from JS?

Community
  • 1
  • 1
Artem
  • 400
  • 2
  • 7
0

You need to play with timeouts in this one.

Try this: DEMO

Play with the values if you want, to achieve your desired effect.

You could achieve the same result with setInterval, but i prefer setTimeout in this one.

  /*Fades in an element. Params: A DOM Element*/
  var fade = function (node) {
    var level = 0;
    var step = function ( ) {
      node.style.opacity = level;
      if (level < 1) {
        level += 0.1;
        setTimeout(step, 50); //increase the value by 0.1 every 50ms
      }
    };
    setTimeout(step, 0); //calls immidiatelly (4ms after)
  };
  fade(document.body);

Cheers!

gdyrrahitis
  • 5,598
  • 3
  • 23
  • 37
  • I don't like this solution, CSS3 transition is here to do the job! – Klitzounet Jan 30 '15 at 11:10
  • Ok, I agree that with CSS this unit of work is an easy task. But the question is about the js way to solve this issue. Also for older browsers the CSS3 transitions/animations are useless. One good way to go is to use CSS3 for animations for modern browsers and jQuery as a fallback mechanism (or pure JS if you don't want to add jQuery, but it would be pain, jQuery is already cross-browser tested) – gdyrrahitis Jan 30 '15 at 14:08
-1

This works fine, does that help?

setTimeout(function() {document.getElementById("toto").style.opacity = "1";},500);
#toto {
    transition: opacity 5s;
    opacity: 0;
}
<div id="toto" style="background:red;height:200px"></div>
Mandera
  • 2,647
  • 3
  • 21
  • 26