Animating Object Size Example
//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 });
Tips for usage
- Animating object dimension can be tricky, especially if the object has paddings or borders. It works best on objects with clear formatting. If your object has padding you may need to set an initial value like
from: { width: $('.selector').outerHeight() }
and this is to make sure the animation is not "jumpy". - Both width and height accept values such as 150, "150px" and "150%" but will always use the measurement unit of the target values, that's right, the
{ to: { width: '150px' } }
values. - If you only do a simple width animation for instance, in most cases you don't need to specify an initial value, jQueryTween will measure it for you.
- TIP | you can animate same object with 2 tweens at the same time, one for width and one for height, but with different options of delay, speed or easing. Just like in my demos.
Animating Color Example
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 });
Tips for usage
- So here we go, another thing jQuery cannot do by itself, animating
color
transitions. - Both
color
andbackground-color
accept all possible cross-browser color values:#069
,#006699
,rgb(0,102,153)
and evenrgba(0,102,153,1)
, just that the alpha value is ignored. - If you only do a simple color animation for instance, in most cases you don't need to specify an initial value, jQueryTween will get it for you.
- TIP | both
color
andbackground-color
can run well in separate tween instances.
Animating Transform Translate Example
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 });
Tips for usage
- If you only do a simple
translateZ
animation for instance, you will not see any effect taking place until you give the object or the object wrapper aperspective: 400px;
. The more perspective value the deeper the depth of view, producing less effect. - Sometimes when you perform a tween with translateZ and another property, they could compete and produce little to no effect. That is because the perspective.
- All translate properties accept values such as 150, "150px" and "150%", will always use the measurement unit of the target values, but the initial values are 0 by default and you may need to provide
{ from: { translate: {x: 50, y: 20} } }
value if you have set the object styling this way. Why? The script cannot read the browser computedmatrix3d()
values, it's a matter of performance and honestly not worth the effort. - Notice | unlike tweening width/height, transform does not allow tweening different properties with multiple tween instances at the same time. Just like with CSS you cannot set multiple transforms at once. Only used all together in the same tween instance or CSS line. You can only combine any transform instance with any other jQueryTween supported property.
Animating Transform Rotation Example
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 });
Tips for usage
- Same as for translation, rotation also requires a perspective. If the
perspective
is not set anywhere, the rotation would not produce a 3D effect. - Also same as for
translate
, rotation requires initial values if they were set by user via CSS, else default value 0 will be used. - Unlike
translate
,rotate
always performs with degrees. So it accepts values like "180" or "180deg" only.
Animating Transform Scale Example
$('.selector').jQueryTween({ to: { scale: 2 }, yoyo: true }); $('.selector').jQueryTween({ from: {scale: 2}, to: { scale: 1 }, yoyo: true });
Tips for usage
scale
is also atransform
property, but it does not require a perspective.- Unlike
translate
orrotate
,scale
is always taken as absolute value. No unit required.
Animating Background Position Example
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 });
Tips for usage
- This effect is not producing if the background image is not large enough or it's a case of
background-size: cover
. - jQueryTween only performs background position animation in percent values, but this can be easily changed if you want.
- As you could see in the examples, it accepts strings as values but transforms them into percent. Example: "top" becomes "0%", or "center" becomes "50%".
- Using large background images can cause severe performance drawbacks so use with caution.
Animating Object Position Example
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%' } } });
Tips for usage
- Notice | unlike
width
andheight
, for positioning the script does not read the current values, it will assume it's 0, so you must always define initial values in thefrom
option. This is to avoid jumpy animations. - In order to perform properly, make sure the parent element is set to
position: relative
. - This effect can be used as fallback and replace
translate
for old browsers. - The positioning animation accepts for all top, right, bottom, left values such as "150", "150px" or "150%".
- The positioning animation can work for both
position: absolute
andposition: relative
elements, excepttop
andbottom
only work forabsolute
. - To have a smooth animation and prevent jumps, you must use values for both
from
andto
in the exact same units for initial and target values.
Animating Window Scroll Example
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 } ); });
Tips for usage
- You don't want to use
yoyo
for this kind of stuff :D. - The selector does not really matter since we only scroll the document.body.
- You may wanna check the
../assets/js/scripts.js
I used for these demos, the scrolling can get quite fun.
Tween Manipulation Stop / Pause / Resume
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
Tips for usage
- Make sure you check the
./assets/js/scripts.js
for more detailed usage. - The method controls all tween instances, so for instance when you pause, all tweens assigned to your object will pause.
- Sometimes pausing delayed tweens or yoyo enabled tweens, the callback functions will trigger prematurely on resume.
Easing Functions
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.
下面的每个demo会重复5次动画,请点击表格相应的行来激活动画查看easing效果。
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 |