This plugin is a bridge to tween.js and aims to drastically simplify your work. Each script does what it does best, for high performance hardware accelerated animations.
Using tween.js is now super duper easy with jQueryTween, as it expands it's powers in many ways. It also comes with 2 special callbacks for your other Javascript functions.
jQueryTween is only 8Kb, and works as a controller for tween.js while this one is about 6Kb and calculates the "between" values from an initial state to a target state based on easing functions.
Yes that's right! You can tween colors, transform properties, scroll window, and even tween background position. Most properties accept multiple types of values, including percentage or strings.
jQueryTween is completely free for commercial use. Deploy your awesome project right away.
In a single page, jQueryTween is showing it's strengths and also explaining it's behind the scenes coding.
jQueryTween comes in 2 distribution formats. The AIO includes tween.js inside so you don't have to worry.
jQueryTween supports all major browsers and because it's size, you can spare some scripting and do your own custom fallback animations for old browsers at your discretion.
Here's a perfect jQuery start-up environment with Bootstrap, and each script does what it does best. Not tested but it should work with zepto as well.
Why not just use jQuery's .animate()
functions, or some CSS3 tricks? It's about performance, flexibility and dynamics. Tweening engines makes it a more smooth experience and have built-in easing functions. We use the famous Robert Penner's Easing Functions. Tweening engines provides hardware accelerated animations with up to 60 FPS, jQuery doesn't. Despite the size jQuery cannot do many other things such as color transitions, yoyo, pause or reverse playback.
CSS3 cannot scroll the window and you don't need to write entire pages of CSS3 for various animation timings and delays, with jQueryTween you write one line and it's done.
Another great thing about jQueryTween is that you can add your custom callback functions when animation is finished or even while animating. Ha!
First, you need to download the package at Github and copy the distribution that suits you to your assets folders. If you are using the AIO pack (all in one), you can add the following to your site and you are ready to go.
<script type="text/javascript" src="../assets/js/jQueryTween-aio-min.js">
In other cases, you may need to have all files separate. So you need:
<script type="text/javascript" src="../assets/js/RequestAnimationFrame.js"> <script type="text/javascript" src="../assets/js/tween.min.js"> <script type="text/javascript" src="../assets/js/jQueryTween.min.js">
The basic jQueryTween syntax:
$('.selector').jQueryTween(options,callback,special);
The extrapolated syntax:
$('.selector').jQueryTween({ //options from : {}, to : {}, duration: 500, delay : 0, yoyo : false, easing : TWEEN.Easing.Linear.None }, // callback when tween is finished function() { //some cool function }, // special callback while tweening function() { //another cool function } );
In the examples below we are going to explore all the options and features in detail so stay tuned.
//first example $('.selector').jQueryTween({ to: { width: 150 }, yoyo: true }); //second example $('.selector').jQueryTween({ to: { height: 150 }, yoyo: true }); //third example $('.selector').jQueryTween({ to: { height: 150 }, yoyo: true, duration: 1500, easing: TWEEN.Easing.Cubic.Out }); $('.selector').jQueryTween({ to: { width: 150 }, yoyo: true, duration: 1000, delay: 500, easing: TWEEN.Easing.Bounce.In });
from: { width: $('.selector').outerHeight() }
and this is to make sure the animation is not "jumpy".{ to: { width: '150px' } }
values.In this example, the object is going to animate text color and background color.
jQuery cannot do that.
//first example $('.selector').jQueryTween({ to: { color: '#069' }, yoyo: true }); //second example $('.selector').jQueryTween({ to: { background-color: 'rgb(0,102,153)' }, yoyo: true });
color
transitions.color
and background-color
accept all possible cross-browser color values: #069
, #006699
, rgb(0,102,153)
and even rgba(0,102,153,1)
, just that the alpha value is ignored.color
and background-color
can run well in separate tween instances.In this example, the object is going to translate or move left, right and backward. Translate is part of CSS3 transform, what you know as transform: translateX(150px)
. Please know that for this example we have used a 400px perspective for the animated object container.
$('.selector').jQueryTween({ to: { translate: { x: 150 } }, yoyo: true }); $('.selector').jQueryTween({ to: { translate: { y: -150 } }, yoyo: true }); $('.selector').jQueryTween({ to: { translate: { z: -150 } }, yoyo: true }); $('.selector').jQueryTween({ to: { translate: { x: 150, y: -150, z: -300 } }, yoyo: true });
translateZ
animation for instance, you will not see any effect taking place until you give the object or the object wrapper a perspective: 400px;
. The more perspective value the deeper the depth of view, producing less effect.{ from: { translate: {x: 50, y: 20} } }
value if you have set the object styling this way. Why? The script cannot read the browser computed matrix3d()
values, it's a matter of performance and honestly not worth the effort.We take rotate
transformation into another example as it's a bit different. In this example, the object is going to rotate on all axis separate and all together. Please know that for this example we have used a 400px perspective for the animated object container.
$('.selector').jQueryTween({ to: { rotate: { x: 180 } }, yoyo: true }); $('.selector').jQueryTween({ to: { rotate: { y: -180 } }, yoyo: true }); $('.selector').jQueryTween({ to: { rotate: { x: 90, y: -180, z: -360 } }, yoyo: true });
perspective
is not set anywhere, the rotation would not produce a 3D effect.translate
, rotation requires initial values if they were set by user via CSS, else default value 0 will be used.translate
, rotate
always performs with degrees. So it accepts values like "180" or "180deg" only.$('.selector').jQueryTween({ to: { scale: 2 }, yoyo: true }); $('.selector').jQueryTween({ from: {scale: 2}, to: { scale: 1 }, yoyo: true });
scale
is also a transform
property, but it does not require a perspective.translate
or rotate
, scale
is always taken as absolute value. No unit required.In this example, the object is going to have background position transitions, one axis or both.
This is technique very much used for parallax effects. Some scripts such as scrollMagic even change background position in the same time with window scroll.
$('.selector').jQueryTween({ to: { backgroundPosition: { x: 'left' } }, yoyo: true }); $('.selector').jQueryTween({ to: { backgroundPosition: { y: '100%' } }, yoyo: true }); $('.selector').jQueryTween({ to: { backgroundPosition: { x: 90, y: 'center' } }, yoyo: true });
background-size: cover
.In this example, jQueryTween is going to perform multiple tween instances for the same object with an absolute positioned element. yoyo is not used.
Let's go ahead and hit play and see what's going on.
$('.selector').jQueryTween({ to: { position: { left: '50%' } } }); $('.selector').jQueryTween({ to: { position: { left: '-100%' } } });
width
and height
, for positioning the script does not read the current values, it will assume it's 0, so you must always define initial values in the from
option. This is to avoid jumpy animations.position: relative
.translate
for old browsers.position: absolute
and position: relative
elements, except top
and bottom
only work for absolute
.from
and to
in the exact same units for initial and target values.This very page uses this feature. Make sure you check the navigation in the right side of the page (it's at bottom on mobiles).
You can check the code below for using smooth scroll on mouse wheel.
Or click here if you are not convinced.
// a simple scroll to top $('.selector').jQueryTween({ to: { scroll: 0 } }); //a simple way to scroll to some element $('.selector').jQueryTween({ to: { scroll: $(this).offset().top } }); // you may want to have a smooth animation on mouse scroll, here's the code // remember, this feature can chew your browser if has the console enabled $(window).on('mousewheel DOMMouseScroll',function(e) { e.preventDefault(); var el = $(this); var scrollAmount = 250, currentScroll = $(el).scrollTop(); var delta = e.originalEvent.wheelDelta/120 || -e.originalEvent.detail/3; var finalScroll = currentScroll - parseInt(delta*scrollAmount); $('body').jQueryTween( { to : { scroll: finalScroll }, easing: TWEEN.Easing.Exponential.Out, duration : 500 } ); });
yoyo
for this kind of stuff :D.../assets/js/scripts.js
I used for these demos, the scrolling can get quite fun.In this example we are going to demonstrate the stop/pause/resume functions. The object is performing multiple tween instances for same object, as described in the
You can pause / stop a tween or all tweens assigned to an object at any given time, and if paused, you can resume at any point in time.
$('.selector').stop(); // stops all the object's tweens $('.selector').pause(); // pauses all the object's tweens $('.selector').play(); // resumes all the object's tweens
../assets/js/scripts.js
for more detailed usage.The tween.js distribution comes with all the easing functions that we need to perform most of the animation needs. There are 31 functions so far, here is a complete list.
The demo below will repeat 5 times, just mouse click inside the table and animations will go wild.
Function | Demo |
---|---|
TWEEN.Easing.Linear.None | |
TWEEN.Easing.Quadratic.In | |
TWEEN.Easing.Quadratic.Out | |
TWEEN.Easing.Quadratic.InOut | |
TWEEN.Easing.Cubic.In | |
TWEEN.Easing.Cubic.Out | |
TWEEN.Easing.Cubic.InOut | |
TWEEN.Easing.Quartic.In | |
TWEEN.Easing.Quartic.Out | |
TWEEN.Easing.Quartic.InOut |
Function | Demo |
---|---|
TWEEN.Easing.Quintic.In | |
TWEEN.Easing.Quintic.Out | |
TWEEN.Easing.Quintic.InOut | |
TWEEN.Easing.Sinusoidal.In | |
TWEEN.Easing.Sinusoidal.Out | |
TWEEN.Easing.Sinusoidal.InOut | |
TWEEN.Easing.Exponential.In | |
TWEEN.Easing.Exponential.Out | |
TWEEN.Easing.Exponential.InOut |
Function | Demo |
---|---|
TWEEN.Easing.Circular.In | |
TWEEN.Easing.Circular.Out | |
TWEEN.Easing.Circular.InOut | |
TWEEN.Easing.Elastic.In | |
TWEEN.Easing.Elastic.Out | |
TWEEN.Easing.Elastic.InOut | |
TWEEN.Easing.Back.In | |
TWEEN.Easing.Back.Out | |
TWEEN.Easing.Back.InOut | |
TWEEN.Easing.Bounce.In | |
TWEEN.Easing.Bounce.Out | |
TWEEN.Easing.Bounce.InOut |
jQueryTween comes with a useful feature for callbacks. You can set a custom function after the animation is finished and even a function to execute while the tween is running. This explains what happens to the buttons on the dimension tweening example.
In the example, there are some custom functions triggered at custom events, let's explore the code a little bit.
OK. Let's take it from the start shall we? Here's the basic syntax for jQueryTween.
$('.selector').jQueryTween(options,callback,special);
Now, back to our example, looks like this.
//so when we click the button $('.btn-width').on('click',function() { //first we cache the button object var bt = $(this); //next, for all buttons we change classes and disable them, we don't want to interrupt the animation $('.btn-width, .btn-height, .btn-width-height').removeClass('btn-jtween').addClass('btn-info disabled'); //then we initialize our tween animation $('#size-example').jQueryTween({ //these are the tween options to: { width: 150 }, duration: 1000, yoyo: true }, //here we trigger the callback function when animation is finished function() { //the button gets a new text $(bt).text('Play width again' ); //all buttons are enabled and get their initial class $('.btn-width, .btn-height, .btn-width-height').removeClass('btn-info disabled').addClass('btn-jtween'); }, // while animation was running, for each frame we were calling the special callback // this updates the button text with the current width function() { $(bt).text('Width: ' + $('#size-example').width() + 'px' ); }); });
If you only want to add a function while tween is running, you can do something like this:
$('.selector').jQueryTween( {to: { opacity: 0 } }, null, function() { //code for special callback goes here .... }
Notice we have declared the callback on finish as null
so that we can only affect what happens while tweening.
Nowadays you can detect browsers on any device, so you should use jQueryTween with IE11+ and all other modern browsers and remember that CSS3 transitions still have higher performance on most mobile devices.
On old browsers you can do fallback transitions as described in the positioning examples.
Using the given code and examples, add your features to your heart's desire. You may consider animating border-radius
, or rgba()
colors, just remember we are (be)tweening numbers.
If the given methods don't suit you or the CSS properties supported, you are free to delete them, it's a good way to improve performance. I don't need color transitions for instance.
The given demo is full of examples, tips and instructions, you better keep an eye on maybe you learn something new. Also don't forget to check the ../assets/js/scripts.js
for all the examples.
You will notice that the script is very heavily based on plain JavaScript because it has superior performance over any framework, no exception.
This means that if IE10 or below is used, you can use almost all properties, color
, background-color
, opacity
, width/height
, repositioning and background-position
, so all except transform
. The script can only do what browsers would do at their best. You cannot expect 15Kb!! of script to make IE8-IE10 animate like Google Chrome.
Not enough data. Could be a tween.js related issue.
background-position
only works with percent values.This is actually not bad at all, you can hack the code to work with pixels as well, just I don't recommend it.
To make such a feature would require a complete rewrite of the jQueryTween to also incorporate the tween.js script. You can use $.queue()
instead.
document.body
so far, no other option implemented.This also can be changed easily but not really worth the effort.
If you found NEW issues please use the issue tracker and provide detailed steps to replicate and/or fiddle code sample.
Same as with error reporting, please fork jQueryTween and request a PR, any time you see fit, here.
Please feel free to donate, here's a quick link.