Package | com.greensock.plugins |
Class | public class CSSPlugin |
Inheritance | CSSPlugin TweenPlugin Object |
vars
parameter, like this:
//notice the css-related values are inside the "css" object but the ease is not: TweenLite.to(element, 1, {css:{top:"20px", left:"100px", backgroundColor:"#FF0000"}, ease:Power2.easeOut});
Note: a common mistake is to forget to wrap css-related properties in a css object which is essential for specifying your intent. Remember, GSAP (GreenSock Animation Platform) isn't just for tweening css properties. And always use camel case representations of the properties, so instead of "font-size", you'd use "fontSize".
To get up and running quickly with GSAP, check out the Jump Start tour which covers the basics in a fun, interactive way. To see the CSS3-specific features (like 3D transforms, boxShadow, textShadow, borderRadius, and clip) demonstrated and explained, see http://www.greensock.com/css3/.
You can even define properties that are not generally tweenable and GSAP will apply the property for you (like
position:"absolute" or borderStyle:"solid"). These non-tweenable properties will be set at the beginning of the
tween (except display:"none"
which will be applied at the end of the tween for obvious reasons).
It is typically
a good idea to define a unit of measurement (like "24px" instead of "24" or "50%" rather than "50") but the default in most
cases is pixels (px), so you can omit the unit if you prefer. In fact, you can define your own defaults using the
CSSPlugin.suffixMap
object. And even if the unit of measurement doesn't match the current one, GSAP will
attempt to convert them for you. So, for example, you could tween a width from "50%" to "200px".
CSSPlugin can even animate complex values like
boxShadow:"0px 0px 20px 20px red"
and
borderRadius:"50% 50%"
and
border:"5px solid rgb(0,255,0)"
.
When necessary, it attempts to figure out if the property needs a vendor prefix and applies it accordingly. There
may be a small subset of complex or bleeding-edge css properties that CSSPlugin can't handle yet, but the vast majority work great.
In addition to almost all of the standard css properties, CSSPlugin recognizes some special ones that can be quite convenient:
//much simpler TweenLite.to(element, 2, {css:{rotation:30, scaleX:0.8}});
//use "deg" or "rad" TweenLite.to(element, 2, {css:{rotation:"1.25rad", skewX:"30deg"}});
transform:rotate(1.25rad) skewX(30deg)
along with all the other browser prefix
values and the necessary IE filter which would be much more verbose. And CSSPlugin will affect the skewX/skewY values in a slightly different (arguably more intuitive)
way than some browsers because visually the object isn't stretched. For example, if you set transform:skewX(85deg)
in the browser
via CSS, the object would become EXTREMELY long (stretched) whereas with CSSPlugin, it would look more like it sheared in 3D space. Again
this was a purposeful design decision because this behavior is more likely what animators desire. You can certainly get the same effect
as the default browser behavior by tweening the scaleX/scaleY accordingly, using trigonometry.
Notes about 2D transforms:
rotate:"+=30deg"
TweenLite.to(element, 2, {css:{rotationX:45, scaleX:0.8, z:-300}});
perspective()
directly inside the css "transform" style, like:
transform:perspective(500px) rotateX(45deg)
which only applies to that specific element whereas if you want to a
group of elements share a common perspective (the same vanishing point), you should set the regular "perspective" property on the
parent/container of those elements. For more information about perspective, see this article.
//apply a perspective to the PARENT element (the container) to make the perspective apply to all child elements (typically best) TweenLite.set(container, {css:{perspective:500}}); //or set a default perspective that will be applied to every individual element that you tween in 3D: CSSPlugin.defaultTransformPerspective = 500; //or apply perspective to a single element using "transformPerspective" TweenLite.set(element, {css:{transformPerspective:500}});
transformOrigin
property (see below).
//sample css: .myClass { transform: scale(1.5, 1.5) rotateY(45deg) translate3d(10px, 0px, -200px) } //corresponding GSAP transform (tweened over 2 seconds): TweenLite.to(element, 2, {css:{scale:1.5, rotationY:45, x:10, y:0, z:-200}}); //sample css that uses a perspective(): .myClass { transform: perspective(500px) rotate(120deg) translateY(50px) } //corresponding GSAP transform (set, not tweened): TweenLite.set(element, {css:{transformPerspective:500, rotation:120, y:50}});
Notes about 3D transforms:
//spins around the element's top left corner TweenLite.to(element, 2, {css:{rotation:360, transformOrigin:"left top"}});
The first value in the quotes corresponds to the x-axis and the second corresponds to the y-axis, so to make the object transform around exactly 50px in from its left edge and 20px from its top edge, you could do:
//spins/scales around a point offset from the top left by 50px, 20px TweenLite.to(element, 2, {css:{rotation:270, scale:0.5, transformOrigin:"50px 20px"}});
//rotates around a point that is 400px back in 3D space, creating an interesting effect: TweenLite.to(element, 2, {css:{rotationY:360, transformOrigin:"50% 50% -400px"}});
Notes about transformOrigin:
transformOrigin:"50% 50% -100px"
), CSSPlugin will record the z-component (-100px in this example) internally and remove it from the transformOrigin that gets applied to the css. Everything will render correctly because the z-axis origin offset is calculated internally and applied to the matrix3d(). Just keep in mind that if you check the css value of the transformOrigin after the tween has started, it won't have the z component but that's by design.
TweenLite.to(element, 2, {css:{shortRotation:-170}});
shortRotationX
and shortRotationY
.
//fade out and set visibility:hidden TweenLite.to(element, 2, {css:{autoAlpha:0}}); //in 2 seconds, fade back in with visibility:visible TweenLite.to(element, 2, {css:{autoAlpha:1}, delay:2});
TweenLite.to(myElement, 1, {css:{className:"class2"}});
And if you want to ADD the class to the existing one, you can simply use the "+=" prefix. To remove a class, use the "-=" prefix like this:
TweenLite.to(myElement, 1, {css:{className:"+=class2"}});
Note: there are some css-related properties that don't tween like IE filters and 3D transforms (support for those may come in the future). Also, there is a slight speed penalty when using className because the engine needs to loop through all of the css properties to see which ones are different.
TweenLite.to(element, 5, {css:{bezier:{curviness:1.25, values:[{x:100, y:200}, {x:250, y:400}, {x:500, y:50}], autoRotate:true}}, ease:Power1.easeOut});
autoRound:false
in the css object.
You can still use the RoundPropsPlugin to manually define properties that you want rounded.Copyright 2008-2012, GreenSock. All rights reserved. This work is subject to the terms in http://www.greensock.com/terms_of_use.html or for Club GreenSock members, the software agreement that was issued with the membership.