KUTE.js can be used in most cases with native Javascript, but also with jQuery. So, before we head over to the more advanced examples, let's have a quick look at these two basic examples here. Note: the examples are posted on codepen.
When developing with native Javascript, a very basic syntax goes like this:
// this is the tween object, basically KUTE.method(element, from, to, options);
var tween = KUTE.fromTo('selector', {left: 0}, {left: 100}, {yoyo: true});
Now the tween object is created, it's a good time for you to know that via Native Javascript we always animate the first HTML element only, even if you're using a class selector. To create/control a tween for multiple elements such as querySelectorAll() or getElementsByTagName(), you need to do a for () loop. Now let's apply the tween control methods:
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
The demo for the above example is here.
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:
// this is the tween object, basically $('selector').KUTE(method, from, to, options);
var tween = $('selector').KUTE('fromTo', {top: 20}, {top: 100}, {yoyo: true});
We mentioned that the KUTE jQuery plugin is different, and here's why: the above code creates an Array of objects for each HTML element of chosen selector, while the Native Javascript creates a single object. For these objects we can now apply the tween control methods as follows:
$(tween).KUTE('start'); // starts the animation
$(tween).KUTE('stop'); // stops current tween and all chained tweens animating
$(tween).KUTE('pause'); // pauses current tween animation
$(tween).KUTE('play'); // or $(myTween).KUTE('resume'); resumes current tween animation
$(tween).KUTE('chain',myTween2); // when tween animation finished, you can trigger the start of another tween
The demo for the above example is here.
KUTE.js supports almost all about transform as described in the spec: the 2D translate, rotate, skewX, skewY and scale, as well as the 3D translateX, translateY, translateZ, translate3d, rotateX, rotateX, rotateY, rotateZ properties. Additionally it allows you to set a perspective for the element or it's parent as well as a perpective-origin for the element or it's parent.
In the next example the first box is moving to left 250px with translate property, the second box is moving to the right by 200px using translateX and the third box is moving to the bottom using translate3d. The last box also uses translate3d but requires a perspective value for the animation on the Z axis to be effective.
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});
And here is how it looks like:
As you can see in your browsers console, for all animations translate3d is used, as explained in the features page. Also the first example that's using the 2D translate for both vertical and horizontal axis even if we only set X axis. You can download this example here.
Remember: stacking translate and translate3d together may not work and IE9 does not support perspective.
Next we're gonna animate 4 elements with one axis each element. Unlike translations, KUTE.js does not support rotate3d.
var tween1 = KUTE.fromTo('selector1',{rotate:0},{rotate:-720});
var tween2 = KUTE.fromTo('selector2',{rotateX:0},{rotateX:200});
var tween3 = KUTE.fromTo('selector3',{rotateY:0},{rotateY:160},{perspective:100});
var tween4 = KUTE.fromTo('selector4',{rotateZ:0},{rotateZ:360});
And here is how it looks like:
The rotateX and rotateY 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 perspective 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 here.
KUTE.js supports skewX and skewY so let's animate the two. Since they are 2D transformations, IE9 supports skews.
var tween1 = KUTE.fromTo('selector1',{skewX:0},{skewX:20});
var tween2 = KUTE.fromTo('selector2',{skewY:0},{skewY:45});
And here is how it looks like:
You can download this example here.
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:
var tween1 = KUTE.fromTo('selector1',{rotateX:0},{rotateX:20}).start();
var tween2 = KUTE.fromTo('selector1',{skewY:0},{skewY:45}).start();
If you check the test here, you will notice that only the skewY is going to work and no rotation. Now let's do this properly.
var tween1 = KUTE.fromTo(
'selector1', // element
{translateX:0, rotateX:0, rotateY:0, rotateZ:0}, // from
{translateX:250, rotateX:360, rotateY:15, rotateZ:5}, // to
{perspective:400, perspectiveOrigin: 'center top'} // trasform options
);
var tween2 = KUTE.fromTo(
'selector2', // element
{translateX:0, rotateX:0, rotateY:0, rotateZ:0}, // from values
{translateX:-250, rotateX:360, rotateY:15, rotateZ:5}, // to values
{parentPerspective:400, parentPerspectiveOrigin: 'center top'} // trasform options
);
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.
This example also shows the difference between an element's perspective and a parent's perspective. You can download the above example here.
I'm gonna insist on the tween chaining feature a bit 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 .to() method because it has the ability to stack 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:
What's this all about?
.fromTo() 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.fromTo() object, but using proper values for all tweens at all times, so we fixed that glitch.to() method, and does the chaining easier for most properties, especially for transformWhen coding transformation chains I would highly recommend:
translate:[x,y] and translate3d:[x,y,z]; for instance using translate:150 or translateX:150 would mean that all other axis are 0;.fromTo() 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 features page;In the example below we are doing some animation on the border-radius property. The first box animates all corners, while the other boxes animate each corner at a time. A quick reminder, for radius properties KUTE.js supports px, % and text properties' units such as em or rem.
KUTE.to('selector1',{borderRadius:'100%'}).start();
KUTE.to('selector2',{borderTopLeftRadius:'100%'}).start();
KUTE.to('selector3',{borderTopRightRadius:'100%'}).start();
KUTE.to('selector4',{borderBottomLeftRadius:'100%'}).start();
KUTE.to('selector5',{borderBottomRightRadius:'100%'}).start();
And here is how it looks like:
A quick important reminder here is that KUTE.js does not support shorthands for radius properties. Also early implementations by Mozilla's Firefox browser like -moz-border-radius-topleft are not supported because they were depracated with later versions. Download this example here.
While KUTE.js supports almost all the box model properties, the next example will animate most common properties, we will focus mostly on size, spacing and position. Other properties such as minWidth or maxHeight require a more complex context and we won't insist on them.
var tween1 = KUTE.to('selector1',{width:200});
var tween2 = KUTE.to('selector1',{height:300});
var tween3 = KUTE.to('selector1',{left:250});
var tween4 = KUTE.to('selector1',{top:100});
var tween5 = KUTE.to('selector1',{padding:'5%'});
We're gonna chain these tweens and start the animation. You can download this example here.
TIP: the width and height properties used together can be great for scale animation fallback on images for legacy browsers.
OK here we're gonna do a cool example for text properties. Basically the below code would work:
var tween1 = KUTE.to('selector1',{fontSize:'200%'});
var tween2 = KUTE.to('selector1',{line-height:24});
var tween3 = KUTE.to('selector1',{letter-spacing:50});
But our example will feature some more than just that. We're gonna animate each character of a given string, with a small delay. The heading will animate fontSize and letterSpacing properties for each character while the button will animate fontSize and lineHeight properties. Watch this:
TIP: this should also work in IE8 as a fallback for scale animation for text. It's not perfect, can be improved for sure, but if it's a must, this would do. Download this example here.
The next example is about animating color properties. As for example, check these lines for reference.
KUTE.to('selector1',{color:'#069'}).start();
KUTE.to('selector1',{backgroundColor:'#069'}).start();
KUTE.to('selector1',{borderColor:'rgb(25,25,25)'}).start();
KUTE.to('selector1',{borderTopColor:'#069'}).start();
KUTE.to('selector1',{borderRightColor:'rgba(25,25,25,0.25)'}).start();
KUTE.to('selector1',{borderBottomColor:'#069'}).start();
KUTE.to('selector1',{borderLeftColor:'#069'}).start();
Let's get some animation going. Download the example here.
A quick reminder: you can also use RGB or RGBA, but the last one is not supported on IE8 and it will fallback to RGB.
This property allows you to animate the rectangular shape of an element that is set to position:absolute. In CSS this property works like this clip: rect(top,right,bottom,left) forming a rectangular shape that masks an element making parts of it invisible.
KUTE.to('selector',{clip:[0,150,100,0]}).start();
A quick example here could look like this:
Note that this would produce no effect for elements that have overflow:visible style rule. Download this example here.
Another property we can animate with KUTE.js is backgroundPosition. Quick example:
KUTE.to('selector1',{backgroundPosition:[0,50]}).start();
A working example would look like this:
Download this example here.
Another property we can animate with KUTE.js is scrollTop. I works for both the window and any scrollable object. Quick example:
KUTE.to('selector',{scroll:450}).start(); // for a scrollable element
KUTE.to('window',{scroll:450}).start(); // for the window
A working example would work like this. Scroll works with IE8+ and is a unitless property even if these scroll distances are measured in pixels.
Unlike the examples hosted on Codepen, 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, first check your HTML template to have the browser detection for IE8-IE9, then check to have the polyfills and also make sure you target your browsers, here's a complete reference. In some cases you may not have access to the HTML tag, here's a work around. Now we are ready:
// grab an HTML element to build a tween object for it
var element = document.getElementById("myElement");
// check for IE legacy browsers
var isIE8 = /ie8/.test(document.documentElement.className);
var isIE9 = /ie9/.test(document.documentElement.className);
// most browsers have specific checks, so make sure
// you include all you need for your target audience
// create values and options objects
var startValues = {};
var endValues = {};
var options = {};
// here we define properties that are commonly supported
startValues.opacity = 1;
endValues.opacity = 0.5;
startValues.backgroundColor = '#CDDC39';
endValues.backgroundColor = '#ec1e71';
// here we define the properties according to the target browsers
if (isIE8) { // or any other browser that doesn"t support transforms
startValues.left = 0;
endValues.left = 250;
} else if (isIE9) { // or any other browser that only support 2d transforms
startValues.translate = 0; // 2d translate on X axis
endValues.translate = 250;
startValues.rotate = -180; // 2d rotation on Z axis
endValues.rotate = 0;
startValues.scale = 1; // 2d scale
endValues.scale = 1.5;
} else { // most modern browsers
startValues.translate3d = [0,0,0]; //3d translation on X axis
endValues.translate3d = [250,0,0];
startValues.rotateZ = -180; // 3d rotation on Z axis
endValues.rotateZ = 0;
startValues.rotateX = -20; // 3d rotation on X axis
endValues.rotateX = 0;
startValues.scale = 1; // 2d scale
endValues.scale = 1.5;
options.perspective = 400; // 3d transform option
}
// common tween options
options.easing = "easingCubicOut";
options.duration = 2500;
// the cached object
var myTween = KUTE.fromTo(element, startValues, endValues, options);
// trigger buttons
var startButton = document.getElementById('startButton'),
stopButton = document.getElementById('stopButton'),
playPauseButton = document.getElementById('playPauseButton');
// add handlers for the trigger buttons
startButton.addEventListener('click', function(e){
e.preventDefault();
if (!myTween.playing) { myTween.start(); } // only start the animation if hasn't started yet
}, false);
stopButton.addEventListener('click', function(e){
e.preventDefault();
if (myTween.playing) { myTween.stop(); } // only stop the animation if there is any
}, false);
playPauseButton.addEventListener('click', function(e){
e.preventDefault();
if (!myTween.paused && myTween.playing) {
myTween.pause(); playPauseButton.innerHTML = 'Resume';
} else {
myTween.resume();
playPauseButton.innerHTML = 'Pause';
}
}, false);
Let's explain this code a bit. KUTE.js gives you the internal variables myTween.playing and myTween.paused (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.
START button will use the .start() method and the animation starts;STOP button will use the .stop() method and stops the animation; after this the, animation can only be started againPAUSE button will use the .pause() method and pauses the animation; this also changes the button's text and functionality;RESUME button will use the .resume() method and resumes the animation; this reverses the button's initial state;!myTween.playing and myTween.paused conditions because you could end up with errors.