javascript - CSS3 Smooth transition when dynamically changing animations -
i have 2 keyframe animations "bounce-in" , "bounce-out" bounce-in animation takes 1.2 seconds complete, if user triggers bounce-out function before it's finished jump 100% scale , doesn't gracefully scale out it's current animation position.
is possible keyframe animations? i've seen done transition property not using scale().
@-webkit-keyframes bounce-in { 0% { -webkit-transform: scale(0) } 40% { -webkit-transform: scale(1.0) } 60% { -webkit-transform: scale(0.7) } 80% { -webkit-transform: scale(1.0) } 90% { -webkit-transform: scale(0.9) } 100% { -webkit-transform: scale(1.0) } } @-webkit-keyframes bounce-out { 0% { -webkit-transform: scale(1.0) } 40% { -webkit-transform: scale(0.1) } 60% { -webkit-transform: scale(0.4) } 80% { -webkit-transform: scale(0.1) } 90% { -webkit-transform: scale(0.2) } 100% { -webkit-transform: scale(0) } }
i have demo on jsfiddle: http://jsfiddle.net/vn3bm/98/ *if click "games" circle before animation finished notice other 2 jump 100% , animate out (that's i'm trying make smooth).
i tried removing 0% keyframe bounce-out , didn't help...
in case, "jump" notice in animation comes change of animations have installed on onmouseup. "bounce-out" animation has initial scale of 1 (first keyframe), , 2 circles set when animation installed.
there 2 solutions this, can explain in detail:
the easy way
you wait initial animation end via 'webkitanimationend' event , install onmouseup event recursive function waiting animation finish:
var initanimationended = false; document.getelementbyid('sports').addeventlistener('webkitanimationend', function() { initanimationended = true; }, false);
and here's onmouseup handler:
document.getelementbyid('games').onmouseup = function() { function bounceout() { if (initanimationended) { events.style.webkitanimationname = "bounce-out"; sports.style.webkitanimationdelay = "0.2s"; sports.style.webkitanimationname = "bounce-out"; } else { settimeout(bounceout, 20); } } bounceout(); }
installed jsfiddle here can see working. bounce out animation triggered after animation finished, nothing unusual that.
the hard way
you can pause animation , parse current values of transformation, install temporary keyframe animation bounce out. gets more verbose though:
first, have stop animations:
events.style.webkitanimationplaystate = "paused"; sports.style.webkitanimationplaystate = "paused";
then, set helper insert new css rules:
var addcssrule = function(rule) { var style = document.createelement('style'); style.innerhtml = rule; document.head.appendchild(style); }
then create css keyframe rules on fly , insert them:
// current transform scale of sports , events function getcurrentscalevalue(elem) { return document.defaultview. getcomputedstyle(elem, null). getpropertyvalue('-webkit-transform'). match(/matrix\(([\d.]+)/)[1]; } var currentsportsscale = getcurrentscalevalue(sports); var currenteventsscale = getcurrentscalevalue(events); // set first string keyframes rule var sportstempanimation = ['@-webkit-keyframes sports-temp-bounce-out {']; var eventstempanimation = ['@-webkit-keyframes events-temp-bounce-out {']; // basic bounce out animation var bounceoutanimationbase = { '0%': 1, '40%': 0.1, '60%': 0.4, '80%': 0.1, '90%': 0.2, '100%': 0 }; // scale animation current values (prop in bounceoutanimationbase) { sportstempanimation.push([ prop, ' { -webkit-transform: scale(', currentsportsscale * bounceoutanimationbase[prop], ') } '].join('')); eventstempanimation.push([ prop, ' { -webkit-transform: scale(', currenteventsscale * bounceoutanimationbase[prop], ') } ' ].join('')); } // add closing brackets sportstempanimation.push('}'); eventstempanimation.push('}'); // add animations rules addcssrule([sportstempanimation.join(''), eventstempanimation.join('')].join(' '));
then, restart animations these rules:
events.style.webkitanimationname = "events-temp-bounce-out"; events.style.webkitanimationdelay = "0s"; sports.style.webkitanimationdelay = "0s"; sports.style.webkitanimationname = "sports-temp-bounce-out"; events.style.webkitanimationplaystate = "running"; sports.style.webkitanimationplaystate = "running";
et voilĂ . made jsfiddle here can play around it.
more sugar
in example, circles bounce out alternating in bounce. can work second solution using settimeout sports circle animations. did not want include here because unnecessarily complicate example code.
i know provided examples not dry, example make stuff events , sports work half lines of code (with meta properties), in terms of readability, think example serves well.
to have example working in browsers support css3 animations, need normalize transition properties. in javascript, have here example works animations , other properties well, replace 'transition' property want
for further read on modifying css3 animations on fly, found this post useful, have @ well.
Comments
Post a Comment