Suppose I have some CSS so #joe { transition: opacity 500ms; opacity: 0; }
Then I have JavaScript so when you click the page it adds the div and then animates opacity to = 1.
document.body.onclick = function () {
var joe = document.createElement('div')
joe.innerHTML = 'yo!'
joe.id = 'joe'
document.body.appendChild(joe)
joe.style.opacity = 1
}
Well, it doesn't animate. (I guess because the CSS transition considers it an 'initial page load')
But if you add a timeout
document.body.onclick = function () {
var joe = document.createElement('div')
joe.innerHTML = 'yo!'
joe.id = 'joe'
document.body.appendChild(joe)
setTimeout(function () {
joe.style.opacity = 1
}, 100)
}
Then the animation works.
But in some situations, even a 100ms delay has a visible affect on the apparent responsiveness of the page. And, unfortunately, setting the timeout to 1ms doesn't work.
So, what should the timeout be? I am looking to have animation that works most all the time across major browsers, but I don't want to raise the timeout without good reason.
I am not interested in jQuery animation because in my experience it is less efficient than CSS transitions, and doesn't sync multiple animations well. I'm open to suggestion if there's another JavaScript implementation that wins over CSS, but don't let that stop you from answering the question.
If there is an initial animation for CSS, that would be nice to know...
But what I really want to know is how the timeout should be set, because sometimes I animate left or width and I don't want to have to code in start and finish into CSS, when I need to use JavaScript to dynamically calculate these values.
animate();, but I think the OP needs acallback();after the div is inserted to the dom, to fade it from 0 to 1 opacity or something.