Skip to content
Advertisement

Fade in element by setting opacity with Javascript

I have decided to create a fade in animation effect using vanilla javascript.

This is the code for my fade in effect:

JavaScript

What I am trying to do is incrementally increasing the opacity of the #about div from 0 to 1 by running through a for loop which is supposed to wait 100 miliseconds for every iteration of the loop

However the #about div goes from dark to opacity 1 after a set time without seeing the fade in effect.

What is wrong with my logic?

Advertisement

Answer

This for loop is not on a delay, it sets ten timeouts to take place in 100 miliseconds.

JavaScript

So the fade only takes 1 ms.

This on the other hand loops the MyFadeFunction 10 times over a one second period, which is what you are asking for.

JavaScript

http://jsfiddle.net/dL02zqku/1/

Note var opacity in this example and MyFadeFunction() are global, not nested within the startup function, but called via a function call. This is so that the jquery library being used to call the function is not held in a closure state.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement