Preface
Let's pretend that a div is animated from opacity:0; to opacity:1; and i want to keep opacity:1; after the animation ends.
That's what animation-fill-mode:forwards; does.
@keyframes myAnimation { from { opacity:0; } to { opacity:1; } } div { opacity:0; animation-name:myAnimation; animation-delay:1s; animation-duration:2s; animation-fill-mode:forwards; } <div>just a test</div> - Note 1: i didn't include the vendor prefixes here to simplify
- Note 2: that's just an example, please don't reply with "just use jQuery fadeIn function" etc.
Some things to know
In this answer i read that animation-fill-mode is supported by Chrome 16+, Safari 4+, Firefox 5+.
But animation alone is supported by Chrome 1+ and Opera too. So a general test with Modernizr may return positive even if fill-mode is not supported.
To target animation-fill-mode i added a new test on Modernizr:
Modernizr.addTest('animation-fill-mode',function(){ return Modernizr.testAllProps('animationFillMode'); }); Now i've a .no-animation-fill-mode class for CSS and Modernizr.animationFillMode for JavaScript.
I also read from CSS3 animations specs that:
an animation specified in the document style sheet will begin at the document load
Finally, the question(s)
Is it ok to run a simple jQuery function at the exact number of seconds the animation ends
$(document).ready(function(){ if(!Modernizr.animation){ $('div').delay(1000).fadeIn(2000); }else if(!Modernizr.animationFillMode){ $('div').delay(3000).show(); } }); And in CSS:
.no-animation-fill-mode div { animation-iteration-count:1; } /* or just animation:myAnimation 2s 1s 1; for everyone */ Or is there any known polyfill specific for animation-fill-mode ?
Also, what happens if i use the shorthand syntax
animation:myAnimation 2s 1s forwards; on browsers supporting animation but not animation-fill-mode ?
Thanks a lot!