<metaname="description"content="Animation examples for KUTE.js core engine with most essential CSS and CSS3 properties, as well as all easing functions.">
<p>KUTE.js core engine can animate by itself a great deal of CSS properties as well as scroll. You can do it all with native Javascript or with jQuery, but before we head over to the more advanced examples, let's have a quick look at these two
basic examples here. <strong>Note</strong>: the examples are posted on <ahref="http://codepen.io/thednp/pens/public/"target="_blank">codepen</a>.</p>
<h3>Basic Native Javascript Example</h3>
<p>When developing with native Javascript, a very basic syntax goes like this:</p>
<pre>
<codeclass="language-javascript">// this is the tween object, basically KUTE.method(element, from, to, options);
var tween = KUTE.fromTo('selector', {left: 0}, {left: 100}, {yoyo: true});</code>
</pre>
<p>Now the tween object is created, it's a good time for you to know that via Native Javascript we <strong>always</strong> animate the first HTML element only, even if you're using a class selector. To create/control a tween for multiple elements
such as <code>querySelectorAll()</code> or <code>getElementsByTagName()</code>, you need to do a <code>for ()</code> loop, or make use of the two new methods: <code>.allTo()</code> or <code>.allFromTo()</code>. Now let's apply the tween
control methods:</p>
<pre><codeclass="language-javascript">tween.start(); // starts the animation
tween.stop(); // stops current tween and all chained tweens animating
tween.pause(); // pauses current tween animation
tween.play(); // or tween.resume(); resumes current tween animation
tween.chain(tween2); // when tween animation finished, you can trigger the start of another tween
tween.playing // checks if the tween is currenlty active so we prevent start() when not needed
tween.paused // checks if the tween is currenlty active so we can prevent pausing or resume if needed
</code></pre>
<p>The demo for the above example is <ahref="http://codepen.io/thednp/pen/Bozbgg"target="_blank">here</a>.</p>
<h3>Basic jQuery Example</h3>
<p>KUTE.js includes a jQuery Plugin to help you easily implement KUTE.js in your jQuery applications. When using jQuery, the syntax is familiar but works a bit different than plain Javascript due to jQuery's specific code standards. Let's have
a look:</p>
<pre><codeclass="language-javascript">// this is the tween object, basically $('selector').method(fromValues, toValue, options)
var tween = $('selector').fromTo({top: 20}, {top: 100}, {delay: 500});
</code></pre>
<p>We mentioned that the jQuery Plugin is different, and here's why: the above function call uses the <code>allFromTo</code> method to create an <code>Array</code> of objects for each <code>HTML</code> element of chosen selector, but if the selector
only returns one element the call returns a single tween object built with <code>fromTo</code> method. For the array of objects we can now apply the exact same tween control methods, except these:</p>
<pre><codeclass="language-javascript">tween.length // check if the tween is an array of objects
tween.length && tween.tweens[0].playing && tween.tweens[tween.length-1].playing // now we know that one of the tweens is playing
tween.length && tween.tweens[0].paused && tween.tweens[tween.length-1].paused // now we know that one of the tweens is paused
</code></pre>
<p>The demo for the above example is <ahref="http://codepen.io/thednp/pen/rryWZQ"target="_blank">here</a>.</p>
<h3>Transform Properties Examples</h3>
<p>KUTE.js supports almost all about <code>transform</code> as described in the <ahref="http://www.w3.org/TR/css3-transforms/"target="_blank">spec</a>: the 2D <code>translate</code>, <code>rotate</code>, <code>skewX</code>, <code>skewY</code> and <code>scale</code>, as well as the 3D <code>translateX</code>, <code>translateY</code>, <code>translateZ</code>, <code>translate3d</code>, <code>rotateX</code>, <code>rotateY</code>, <code>rotateZ</code> properties. Additionally it
allows you to set a <code>perspective</code> for the element or it's parent as well as a <code>perpective-origin</code> for the element or it's parent.</p>
<h4>Translations</h4>
<p>In the next example the first box is moving to left 250px with <code>translate</code> property, the second box is moving to the right by 200px using <code>translateX</code> and the third box is moving to the bottom using <code>translate3d</code>.
The last box also uses <code>translate3d</code> but requires a <code>perspective</code> value for the animation on the Z axis to be effective.</p>
<pre><codeclass="language-javascript">var tween1 = KUTE.fromTo('selector1',{translate:0},{translate:-250}); // or translate:[x,y] for both axis
var tween2 = KUTE.fromTo('selector2',{translateX:0},{translateX:200});
var tween3 = KUTE.fromTo('selector3',{translate3d:[0,0,0]},{translate3d:[0,100,0]});
var tween4 = KUTE.fromTo('selector4',{translate3d:[0,0,0]},{translate3d:[0,0,-100]},{parentPerspective: 100});
<p>As you can see in your browsers console, for all animations <code>translate3d</code> is used, as explained in the <ahref="features.html">features page</a>. Also the first example that's using the 2D <code>translate</code> for both vertical
and horizontal axis even if we only set X axis. You can download this example <ahref="http://codepen.io/thednp/share/zip/rOLbyY">here</a>.</p>
<p><strong>Remember</strong>: stacking <code>translate</code> and <code>translate3d</code> together may not work and IE9 does not support <code>perspective</code>.</p>
<h4>Rotations</h4>
<p>Next we're gonna animate 4 elements with one axis each element. Unlike translations, KUTE.js does not support <code>rotate3d</code>.</p>
<p>The <code>rotateX</code> and <code>rotateY</code> are 3D based rotations, so they require a perspective in order to make the browser render proper 3D layers, but in the example they animate different because only the second, Y axis, uses a
<code>perspective</code> setting. The rotation on Z axis does not require a perspective. Unlike translations, you can stack all axis rotation for your animation, but we will see that in a later example. You can download this example <a
<p>You can download this example <ahref='http://codepen.io/thednp/share/zip/wKWbKd/'>here</a>.</p>
<h4>Mixed Transformations</h4>
<p>The current specification does not support animating different transform properties with multiple tween objects at the same time, you must stack them all together into a single object. See the example below:</p>
var tween2 = KUTE.fromTo('selector1',{skewY:0},{skewY:45}).start();
</code></pre>
<p>If you check the <ahref="http://codepen.io/thednp/pen/avZrYo"target="_blank">test here</a>, you will notice that only the <code>skewY</code> is going to work and no rotation. Now let's do this properly.</p>
<p>Now you can see we are using the specific transform options, the first tween object uses these settings for an element and the second for its parent.</p>
<p>This example also shows the difference between an element's perspective and a parent's perspective. You can download the above example <ahref='http://codepen.io/thednp/share/zip/jbrovv/'>here</a>.</p>
<h4>Chained Transformations</h4>
<p>KUTE.js has the ability to stack transform properties as they come in chained tweens because when we run animations one after another we are kinda expecting a certain degree of continuity. As explained before, the best solution is the <code>.to()</code> method because it has the ability to <strong>stack</strong> properties found in the element's inline styling, mostly from previous tween animation, and use them as start values for the next tween. It also transfers unchanged values to
values end for that same reason. OK now, let's see a side by side comparison with 3 elements:</p>
<li>the first box uses a regular <codeclass="bg-gray">.fromTo()</code> object, we are doing things normally, we would expect them to work properly but due to the nature of the transforms, this is what it does</li>
<li>the second box is also using <codeclass="bg-olive">.fromTo()</code> object, but using proper values for all tweens at all times, so we fixed that glitch</li>
<li>and the last box uses the <codeclass="bg-indigo">.to()</code> method, and does the chaining easier for most properties, especially for <code>transform</code></li>
</ul>
<p>When coding transformation chains I would highly recommend:</p>
<ul>
<li>keep the same order of the transform properties, best would be: translation, rotation, skew and scale; an example of the difference shown <ahref="http://jsfiddle.net/nvy26bgb/"target="_blank">here</a> for rotations and
<ahref="http://jsfiddle.net/nvy26bgb/1/"
target="_blank">here</a> for rotations and skew;</li>
<li>2D and 3D translations would work best in if you provide a value at all times; eg. <code>translate:[x,y]</code> and <code>translate3d:[x,y,z]</code>; for instance using <code>translate:150</code> or <code>translateX:150</code> would mean
that all other axis are 0;</li>
<li>on larger amount of elements animating chains, the <code>.fromTo()</code> method is fastest, so you will have more work, but can potentially minimize or eliminate any syncronization issues that may occur, as explained in the <ahref="features.html">features page</a>;</li>
<li>download this example <ahref='http://codepen.io/thednp/share/zip/PPWZRL/'>here</a>.</li>
</ul>
<h3>Box Model Properties</h3>
<p>KUTE.js core engine supports most used box model properties, and almost all the box model properties via the <ahref="css.html">CSS Plugin</a>, so the next example will only animate <code>width</code>, <code>height</code>, <code>top</code> and <code>left</code>.</p>
<p>We're gonna chain these tweens and start the animation. You can download this example <ahref='http://codepen.io/thednp/share/zip/xwqYbX/'>here</a>.</p>
<p>TIP: the <code>width</code> and <code>height</code> properties used together can be great for <code>scale</code> animation fallback on images for legacy browsers.</p>
<h3>Color Properties</h3>
<p>The next example is about animating color properties. As for example, check these lines for reference. Additional color properties such as <code>borderColor</code> or <code>borderLeftColor</code> are supported via the <ahref="css.html">CSS Plugin</a>.</p>
<p>A quick reminder: you can also use <code>RGB</code> or <code>RGBA</code>, but the last one is not supported on IE8 and it will fallback to <code>RGB</code>.</p>
<h3>Vertical Scrolling</h3>
<p>Another property we can animate with KUTE.js is <code>scrollTop</code>. I works for both the window and any scrollable object. Quick example:</p>
<pre><codeclass="language-javascript">KUTE.to('selector',{scroll:450}).start(); // for a scrollable element
KUTE.to('window',{scroll:450}).start(); // for the window
</code></pre>
<p>The scroll animation could however be influenced by mouse hover effects, usually creating some really nasty bottlenecks, but you can always add some CSS to your page to prevent that:</p>
<p>A working example would work like <ahref="http://codepen.io/thednp/pen/bVqKmp/"target="_blank">this</a>. Scroll works with IE8+ and is a unitless property even if these scroll distances are measured in pixels.</p>
<p>Unlike the examples <ahref="http://codepen.io/thednp/pens/public/"target="_blank">hosted on Codepen</a>, most examples here should be supported on legacy browsers. The next example is going to explain more details about how to target browsers according to their supported properties, so stick around. So, if your target audience uses legacy browsers in a significant percent, check to have the proper polyfills and also make sure you target your browsers, here's a <ahref="http://browserhacks.com/"target="_blank">complete reference</a>. Now we are ready:</p>
<p>Let's explain this code a bit. KUTE.js gives you the internal variables <code>myTween.playing</code> and <code>myTween.paused</code> (both true/false) to help you easily manage the tween control methods all together as in this example
here. As said before, KUTE.js version 0.9.5 doesn't stat animating by default, for all the examples on this page you have to start it yourself, unlike their versions hosted on Codepen.</p>
<p>Also please know that opacity animation only works on Internet Explorer 8 if the target element uses <code>float: left/right</code>, <code>display: block</code> or <code>display: inline-block</code>.</p>
<ul>
<li>the <codeclass="bg-blue">START</code> button will use the <code>.start()</code> method and the animation starts;</li>
<li>the <codeclass="bg-red">STOP</code> button will use the <code>.stop()</code> method and stops the animation; after this the, animation can only be started again</li>
<li>the <codeclass="bg-orange">PAUSE</code> button will use the <code>.pause()</code> method and pauses the animation; this also changes the button's text and functionality;</li>
<li>the <codeclass="bg-olive">RESUME</code> button will use the <code>.resume()</code> method and resumes the animation; this reverses the button's initial state;</li>
<li>make sure you work with the conditions properly when you want to pause an animation you MUST check both <code>!myTween.playing</code> and <code>myTween.paused</code> conditions because you could end up with errors.</li>
</ul>
<h3>Tween Object Collections</h3>
<p>With KUTE.js 1.5.0 new tween object constructor methods were introduced, and they allow you to create a tween object for each element in a collection, a very handy way to ease and speed up the animation programing workflow. Let's have
a little fun.</p>
<pre><codeclass="language-javascript">// a simple .to() for a collection of elements would look like this
var myMultiTween1 = KUTE.allTo('selector1',{translate:[0,150]});
// or a more complex .fromTo() example with the two new options
<p>As you can see, we also used the new tween options <code>offset</code> and <code>transformOrigin</code> and they make it so much more easy.</p>
<h3>Easing Functions</h3>
<p>KUTE.js core engine comes with Robert Penner's easing functions, but it can also work with any other easing functions, including custom functions. In the example below the first box animation uses the <code>linear</code> easing function
and the second will use another function you choose.</p>
<p>For more examples and info about the other easing functions, head over to the <ahref="easing.html">easing examples</a> page.</p>
</div>
<ulid="share"class="nav">
<li>Share </li>
<liclass="hidden-xs"><atarget="_blank"href="https://www.facebook.com/sharer/sharer.php?u=http://thednp.github.io/kute.js/index.html"title="Share KUTE.js on Facebook"><spanclass="ion-social-facebook-outline icon"></span></a></li>
<liclass="hidden-xs"><atarget="_blank"href="https://twitter.com/home?status=Spread the word about %23KUTEJS animation engine by @dnp_theme and download here http://thednp.github.io/kute.js/index.html"title="Share KUTE.js on Twitter"><spanclass="icon ion-social-twitter-outline"></span></a></li>
<liclass="hidden-xs"><atarget="_blank"href="https://plus.google.com/share?url=http://thednp.github.io/kute.js/index.html"title="Share KUTE.js on Google+"><spanclass="icon ion-social-googleplus-outline"></span></a></li>
</ul>
<!-- FOOTER -->
<footer>
<divclass="content-wrap">
<pclass="pull-right"><aid="toTop"href="#">Back to top</a></p>