transition
transition-property
transition-duration
transition-timing-function
transition-delay
animation-name
animation-duration
animation-timing-function
animation-iteration-count
animation-delay
animation-direction
animation
shorthandanimation-fill-mode
animation-play-state
a { color: green; } a:hover { color: orange; }
Hover Over Me
a { color: green; transition: 1s; } a:hover { color: orange; }
Hover Over Me
Pprovide a way to control the animation when changing CSS property values. Instead of instaneous changes from one value to the next, the value can transition over a period of time, following an acceleration curve, the start of which can be delayed.
transition-property:
properties that transitiontransition-duration:
time it takes to transitiontransition-timing-function:
bezier curve or steps of transitiontransition-delay:
time delay before transition startstransition:
shorthand for the four transition propertiesThese enable customization of the delay, timing, and duration of the transition between the two values of the property that is being changed.
transition
property
Shorthand property for transition-property, transition-duration, transition-timing-function
,
and transition-delay
.
none | [ <property> ] || <duration> || <timing-function> || <delay> ]#
One or more single-property transitions, separated by commas.
li ul:hover { transition: transform 200ms ease-in 50ms, color 50ms linear; }
Transition between two states of an element, like default and :hover or :invalid, or between a JS toggling of a class name.
transition-property
property
CSS properties to which a transition effect should be applied
none | all | <custom-ident>#
Either none
, or all
and / or a comma-separated list
transition-duration: transform, color;
If using all with other properties, define all
first.
If a property is the list is invalid, but the syntax is correct, the property will be kept in the list so other properties align correctly
transition-duration
property
Sets the length of time a transition animation should take to complete.
<time>#
transition-duration: 200ms, 50ms;
Each duration is applied to the corresponding property as specified by the transition-property
property. If there are fewer durations than properties the duration list is repeated. Too many is OK too.
transition-timing-function
property
Provide an acceleration curve to control the speed of the transition over its duration.
linear | ease | ease-in | ease-out | ease-in-out | start | end cubic-bezier( n1, n2, n3, n4 ) | step-start | step-end steps(<int>, <pos>) | jump-start | jump-end | jump-none | jump-both
These will be described in huge detail in the animation section
transition-timing-function: ease-in, linear;
Each timing function is applied to the corresponding property as specified by the
transition-property
property. If there are fewer timing functions than properties the timing
function list is repeated.
transition-delay
property
Specifies the time to wait before starting a property's transition effect when its value changes.
<time>#
Specifies the amount of time to wait between a property's value changing and the start of the transition effect.
transition-duration: 50ms, 0ms;
Each delay is applied to the corresponding property as specified by the transition-property
property. If there are fewer delays than properties, the delay list is repeated.
code { color: black; font-size: 85%; background-color: rgba(255,255,255,0.9); transition: all 2s ease-in 50ms; } code:hover { color: red; font-size: 120%; background-color: rgba(255,255,255,0.8); }
code { transition: color, font-size, background-color 2s ease-in 50ms; }
code { color: black; font-size: 85%; background-color: rgba(255,255,255,0.9); transition: all 2s ease-in 50ms; } code:hover { color: red; transform: scale(1.4); transform-origin: 0 0; background-color: rgba(255,255,255,0.8); }
code { transition: transform 2s ease-in 50ms; }
Anything that has intermediate values
code { opacity: 1; } code:halfway { opacity: 0.5; } code:hover { opacity: 0; }
code { display: block; } code:halfway { display: ??? } code:hover { display: none; }
2 values that have REAL intermediary values
code { font-size: 100%; } code:halfway { font-size: 110%; } code:hover { font-size: 120%; }
code { height: auto; } code:halfway { height: ??? } code:hover { height: 1000px; }
If a property is the list is invalid, but the syntax is correct, the property will be kept in the list so other properties align correctly.
Use transition-delay
to chain transitions
The order is important: The duration must come before the delay!
Avoid transitioning to auto
visibility
<input type=number min=0 max=10 step=2>
transitionend
eventtransitionend
for EVERY propertytransitionend
for each long-hand property within a shorthandtable { border: 1px black solid; transition: border 1s linear 50ms; } table:hover { border: 5px red solid; }
nav ul li { list-style-type: none; } nav > ul > li { display: inline-block; position:relative; } nav ul ul { transform: scale(1,0); transform-origin: top center; transition:0.2s linear 50ms; position:absolute; top: 100%; } nav li:hover ul { transform: scale(1,1); }
.card { position: absolute; perspective: 600px; } .card:hover { transition: 1s ease; } .card .front { transform: rotateX(0deg) rotateY(0deg); transform-style: preserve-3d; backface-visibility: hidden; transition: all .4s ease-in-out; } .card:hover .front { transform: rotateY(180deg); } .card .back { transform: rotateY(-180deg); transform-style: preserve-3d; backface-visibility: hidden; transition: all .4s ease-in-out; } .card:hover .back { transform: rotateY(0); } .card:first-of-type { transform: rotate(-5deg); } .card:nth-of-type(2):hover { transform: rotate(0) translatex(120px); } .card:last-of-type { transform: rotate(5deg); } .card:first-of-type:hover { transform: rotate(10deg) translatey(180px); }
- @keyframes
- animation-name
- animation-duration
- animation-timing-function
- animation-iteration-count
- animation-direction
- animation-play-state
- animation-delay
- animation-fill-mode
- animation
(shorthand)
@keyframes
at-rule
An at-rule that controls the intermediate steps in a CSS animation sequence by defining styles for keyframes along the animation sequence.
@keyframes <name> { [ <keyframe-selector># { <property-value-list> }]# }
JavaScript can access via the CSSKeyframesRule
interface.
Provides more control over transitions: alllow for intermediate steps
@keyframes ...
@keyframes clever_name_here ...
@keyframes slides { }
Specifies percentages along the animation when the keyframe occurs.
[ from | to | <percentage> ]
@keyframes writing { from { left: 0; } to { left: 100%; } }
@keyframes writing { 0% { left: 0; } 100% { left: 100%; } }
Don't forget the %
Quote on the animation name are optional in most cases
@keyframes no0or100 { 0% { background-color: red; border-width: 1px;} 45% { background-color: green; border-width: 10px; } 55% { background-color: blue; border-width: 20px;} 100% { background-color: red; border-width: 1px;} }
@keyframes writingWhileTired { 0% { left: 0; } 10% { left: 45%; } 90% { left: 55%; } 100% { left: 100%; } }
@keyframes duplicate { 45% { background-color: green; opacity: .5; } 55% { background-color: blue; opacity: .7; } }
Every keyframe selector list has an associated <property-value-list>, better known as the declaration list
@keyframes 'myUniqueAnimation' { from { /* 0% property: values */ opacity: 1.0; color: red; } 25%, 75% { /* values for both the 25 and 75% keyframe */ opacity: 0.5; color: blue; } 75% { /* values unique to 75% */ color: green; /* overrides the blue */ } to { /* 100% property: values */ opacity: 0.9; color: orange; } }
@keyframes W { 0% { left: 0; top: 0; } 25%, 75% { top: 400px; } 50% { top: 50px; } 100% { left: 80%; top: 0; } }
@keyframes gettingThePencil { 0%{ transform: translate3d(0, 0, 0) rotate(0deg) scale(0.5, 0.5); } 100% { transform: translate3d(600px, 400px, 0) rotate(80deg) scale(1.2, 1.2); } }
!important
it isn't@keyframes important { 0% { background-color: red;} 45% { background-color: green; } 100% { background-color: red;} }
.pencil { width:100px; text-align: right; border-bottom: 10px solid; } .pencil::after { content: '✎'; position: absolute; bottom: -20px; right: -10px; }
@keyframes drawALine { 0%{ width:0; color:green; } 100% { width: 100%; color:blue; } }
animation-name
property
Sets one or more animations to apply to an element.
[ none | <keyframes-name> ]#
Each name is an @keyframes
at-rule that sets the property values for the animation sequence.
animation-name: red, blue, green, green, green;}
animation-duration
property
Sets the length of time that an animation takes to complete one cycle.
<time>#
A comma separated list of times in seconds (s) or milliseconds (ms).
animation-duration: 200ms, 300ms, 2s, 200ms, 200ms;
If there are more animation names than durations, the list will repeat. If there are more durations than animation, the extra durations will be ignored.
Negative values are ignored.
You must include the unit, even if the value is zero.
animation-name
and animation-duration
.pencil {
animation-name: drawALine;
animation-duration: 3s;
}
Whichever animation happens last is the background-color I will permanently be
Based on important, I should be the same color as the header. Based on ID I should be blue. Based on animation, green.
Some browsers override erroneously override !important. This is against the specification
Based on the spec I should be the !important color from the non-animated state. If no important is set, I should be the animation's color.
animation-timing-function
animation-timing-function
property
Sets how the animation progresses through the duration of a full animation iteration.
linear | ease | ease-in | ease-out | ease-in-out | start | end cubic-bezier( n1, n2, n3, n4 ) | step-start | step-end steps(<int>, <pos>) | jump-start | jump-end | jump-none | jump-both
The non-step keyword values are actually cubic Bézier curves with predefined four point values. Use the
cubic-bezier()
function for non-predefined values.
The step timing functions divide the input time into a specified number of intervals that are equal in length, with a number of steps and a step position.
animation-timing-function: ease-in, linear;
Each timing function is applied to the corresponding animation as specified by the
animation-name
property. If there are fewer timing functions than animations the timing
function list is repeated.
animation-timing-function
bezier curves
animation-timing-function
non-bezier values
.pencil {
animation-name: drawALine;
animation-duration: 5s;
animation-timing-function: ease-in-out;
}
ease linear ease-in ease-out ease-in-out cubic-bezier(x1, y1, x2, y2) step-start /* same as steps(1, start) */ step-end /* same as steps(1, end) */ steps( X, start|end) /* X = # of steps + when change of value occurs */
animation-iteration-count
property
Sets the number of times an animation cycle should be played before ending the animation.
[ infinite | <number> ]#
A comma-seperated list of interations, where the value is infinite
or a number greater than or
equal to zero. Doesn't need to be an interger.
animation-iteration-count: infinite, 3, 3, 1, 1, 1;
Each entry is matched up with an animation name. If there are too few, the list repeats, if there are too many, the last ones are ignored.
If 0, the animation still happens. Goes directly to the end.
.pencil { ... animation-name: drawALine; animation-duration: 3s; animation-timing-function: ease-in-out; animation-iteration-count: 3; }
‘infinite’ or number. Default is ‘1’.
animation-delay
animation-delay
property
Sets when an animation starts. The animation can start immediately from its beginning or midway thru, or it can wait until a timer expires before starting from the beginning.
<time>#
A comma-seperated list of times, where each time is matched with an animation name. If there aren't enough delays listed, the list repeats. Extra are ignored
animation-delay: 0, 0, 0, 200ms, 2500ms, 5000ms;
.pencil {
animation-name: drawALine;
animation-duration: 8s;
animation-timing-function: ease-in-out;
animation-iteration-count: 2;
animation-delay: 2s;
}
animation-direction
property
Sets whether an animation should play forwards, backwards, or alternating back and forth.
normal | reverse | alternate | alternate-reverse
.pencil {
...
animation-name: drawALine;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-iteration-count: 5;
animation-direction: normal
}
values:
normal | alternate | reverse | alternate-reverse;
animation-direction: alternate;
animation
shorthandanimation
shorthand property
Shorthand for
animation-name, animation-duration, animation-timing-function, animation-delay, animation-iteration-count, animation-direction, animation-fill-mode
,
and animation-play-state
. Well cover teh last two in a few minutes.
<time-duration> <timing-function> <time-delay> <iteration-count> <direction> <fill-mode> <play-state> <keyframes-name> | none
A comma separated list of any or all those values.
animation: 200ms ease-in 100ms alternate infinite red, 300ms linear 2s reverse 3 blue;
Order of properties matters to some extent:
animation-name
value last.
.pencil { animation-name: drawALine; animation-duration: 5s; animation-delay: 100ms; animation-timing-function: ease-in-out; animation-iteration-count: 5;animation-direction: normal;}
. . . can also be written as . . .
.pencil { animation: drawALine 5s ease-in-out 100ms 5; }
.carda.flipped .front, .carda.unflipped .back { animation: flipme 1s linear forwards; } .carda.unflipped .front, .carda.flipped .back { animation: unflipme 1s linear both; } .card.flipped:nth-of-type(2){ transform: rotate(0) translatex(120px); } .carda.flipped:first-of-type{ transform: rotate(-5deg) translate(240px, 25px); } @keyframes flipme { 0% {transform: rotatey(0deg)} 100% {transform: rotateY(-180deg);} } @keyframes unflipme { 0% {transform: rotatey(180deg)} 100% {transform: rotateY(0deg);} }
animation-fill-mode
property
Sets how a CSS animation applies the keyframe styles to the animated element before the animation starts and after it ends.
none | forwards | backwards | both #
animation-direction
and animation-iteration-count
animation-direction: normal
or
alternate
and the properties defined in the 100% keyframe in the case of
animation-direction: reverse
or alternate-reverse
will be applied from the
moment the animation is applied to an element until the delay expires an the animation starts iterating.
forwards
and backwards
are appliedvalues: none | forwards | backwards | both
.pencil { animation: gettingThePencil 3s ease-in-out 1s forwards; }
.pencil { animation-fill-mode: forwards; animation-name: gettingThePencil; animation-duration: 3s; animation-timing-function: ease-in-out; animation-iteration-count: 1; animation-delay: 2s; }
.box { animation: showme 5s linear 5s 1;} @keyframes showme { 0% {left: 200px; background-color:blue;} 50% {background-color:green;} 100% {left: 600px; background-color: yellow;} }
animation-play-state
property
Sets whether an animation is running or paused.
running | paused #
Resuming a paused animation will start the animation from where it left off at the time it was paused, rather than starting over from the beginning of the animation sequence.
animation-play-state: play, play, pause;
While paused, the delay does not count down
animation-play-state: paused | running
.pencil { transform-origin: center 300px; animation: writingInCircles 3s linear infinite; } .pencil:hover { animation-play-state:paused; }
@keyframes writingInCircles { 100% { transform: rotate(360deg); } }
animation-play-state: paused | running
.pencil:hover { animation-play-state: paused; }
Check out the animation inspector
el.addEventListener( 'animationend', function( event ) { console.log('Animation Ended'); }, false );
el.addEventListener( 'animationend', function( event ) { el.removeClassName('newClass'); setTimeout("el.addClassName('newClass')", 100ms) }, false );
animationend