Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your case, the "jump" you notice in your animation comes from the change of animations you have installed on onmouseup. The "Bounce-Out" Animation has an initial scale of 1 (first Keyframe), and this is what the two circles immediately get set to when the animation is installed.</p> <p>There are two solutions to this, which I can explain in some detail: </p> <p><br/></p> <h2>The Easy Way</h2> <p>You could just wait for the initial animation to end via the 'webkitAnimationEnd' Event and install the onmouseup event with a recursive function waiting for the animation to finish:</p> <pre><code>var initAnimationEnded = false; document.getElementById('sports').addEventListener('webkitAnimationEnd', function() { initAnimationEnded = true; }, false);​ </code></pre> <p>And here's the onmouseup handler:</p> <pre><code>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(); } </code></pre> <p><br/> I installed a <strong><a href="http://jsfiddle.net/GPMta/" rel="nofollow noreferrer">jsfiddle here</a></strong> so you can see it working. The Bounce Out animation is only triggered after the animation finished, nothing unusual about that.</p> <p><br/></p> <h2>The Hard Way</h2> <p>You can pause the animation and parse the current values of the transformation, then install a temporary keyframe animation to bounce out. This gets much more verbose though:</p> <p>First, you have to stop the animations:</p> <pre><code>events.style.webkitAnimationPlayState = "paused"; sports.style.webkitAnimationPlayState = "paused"; </code></pre> <p>Then, you set up a helper to insert new css rules:</p> <pre><code>var addCssRule = function(rule) { var style = document.createElement('style'); style.innerHTML = rule; document.head.appendChild(style); } </code></pre> <p>Then create the css keyframe rules on the fly and insert them:</p> <pre><code> // get the current transform scale of sports and 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 up the first string for the 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 the animation to the current values for (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 the animations to the rules addCssRule([sportsTempAnimation.join(''), eventsTempAnimation.join('')].join(' ')); </code></pre> <p>Then, you restart the animations with these rules:</p> <pre><code> 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"; </code></pre> <p><br/> Et voilà. I made a <strong><a href="http://jsfiddle.net/stJFd/" rel="nofollow noreferrer">jsfiddle</a></strong> here so you can play around with it.</p> <p><br/></p> <h2>More Sugar</h2> <ul> <li><p><strong>In your example, the circles bounce out alternating in bounce</strong>. You can easily get this back to work with the second solution by using setTimeout for all sports circle animations. I did not want to include it here because it would unnecessarily complicate the example code.</p></li> <li><p><strong>I know the provided examples are not really DRY</strong>, you could for example make all the stuff for events and sports work with half the lines of code (with meta properties), but in terms of readability, I think this example serves well.</p></li> <li><p><strong>To have this example working in all browsers with support for css3 animations</strong>, you need to normalize the transition properties. To do this in javascript, <a href="https://stackoverflow.com/questions/5023514/how-do-i-normalize-css3-transition-functions-across-browsers">have a look here</a> The Example works for animations and other properties as well, just replace 'transition' with the property you want</p></li> <li><p><strong>For a further read on modifying css3 animations on the fly</strong>, I found <a href="http://blog.joelambert.co.uk/2011/09/07/accessing-modifying-css3-animations-with-javascript/" rel="nofollow noreferrer">this post</a> very useful, have a look at it as well.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload