0

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().

Daniel Ashfall Zhou
  • 167
  • 1
  • 2
  • 11

2 Answers2

2
  1. Create a rule in your stylesheet with a selector that matches the div and which sets a transition property.
  2. 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).
  3. Create the div
  4. Add the class to the div
  5. Add the div to the document
  6. 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