I have a small problem that I'm really scratching my head as to how to implement. I would like to know how to make a dynamically generated div fade in only. The div(s) are dynamically generated by Javascript code. How do I make it so that the div fades in onto the page? I cannot use jQuery or .innerHTML().
Asked
Active
Viewed 1,239 times
0
-
1Why you cant use **jQuery**?: $('.yourElement').fadeIn(); – Adrian Preuss Mar 14 '14 at 21:05
-
Adrian, the poster SPECIFICALLY said NO JQUERY! – Diodeus - James MacFarlane Mar 14 '14 at 21:05
-
It's an exercise, Adrian. – Daniel Ashfall Zhou Mar 14 '14 at 21:06
-
Diodeus, thanks, but that's for hovering. The divs are dynamically generated by javascript, but I want the divs to be faded in. – Daniel Ashfall Zhou Mar 14 '14 at 21:07
-
If it's an exercise, what have you tried? You can look at jquery code for inspiration https://github.com/jquery/jquery/blob/master/src/effects.js – Ruan Mendes Mar 14 '14 at 21:09
-
I found this guys! And it works perfectly for me! :D http://stackoverflow.com/questions/11679567/using-css-for-fade-in-effect-on-page-load – Daniel Ashfall Zhou Mar 14 '14 at 21:20
2 Answers
2
- Create a rule in your stylesheet with a selector that matches the div and which sets a
transition
property. - Create another rule that matches the div if it is a member of an additional class and style it to be invisible (
opacity
is a good property to use for this). - Create the div
- Add the class to the div
- Add the div to the document
- With a minimal
setTimeout
(to allow the browser to repaint the document with the div added), remove the class

Quentin
- 914,110
- 126
- 1,211
- 1,335
1
CSS:
#fade-in-div{
transition: 1s;
-webkit-transition: 1s;
opacity: 0;
}
JS
//generate new div
document.write("<div id='fade-in-div'>New Div!</div>");
//make opacity = 1. This should take 1 second to make (1s fade).
document.getElementById("fade-in-div").style.opacity = 1; //this should take 1 second

AskYous
- 4,332
- 9
- 46
- 82
-
I'm not hovering, the div is generated by javascript. I want the div to fade in when generated. – Daniel Ashfall Zhou Mar 14 '14 at 21:14