Single property transition
To perform CSS transition on a single property of an element, specify the element and the
array with transition values.
function animate(element) {
transition.begin(element, [ "background-color" , "#ffffff" , "#ADB5C7" , "500ms" , "linear" ]);
}
|
animate(element)
You can also specify a string with space separated values instead of an array. The order of the
values in string must follow the same rules defined for the values specified in an array.
function animate(element) {
transition.begin(element, "background-color #ffffff #ADB5C7 500ms linear" );
}
|
animate(element)
You have also more verbose way to specify the transition values as an object.
function animate(element) {
transition.begin(element, {
property: "background-color" ,
from: "#ffffff" ,
to: "#ADB5C7" ,
duration: "500ms" ,
timingFunction: "linear"
});
}
|
animate(element)
Multiple properties transition
To perform CSS transition on multiple properties, specify the element and the array of
arrays with transition values.
function animate(element) {
transition.begin(element, [
[ "transform" , "translateX(0)" , "translateX(200px)" , "1s" , "ease-in-out" ],
[ "background-color" , "#ffffff" , "#ADB5C7" , "500ms" , "linear" ]
]);
}
|
animate(element)
As with single property transition, you can specify an array of strings with space separated values.
function animate(element) {
transition.begin(element, [
"transform translateX(0) translateX(200px) 1s ease-in-out" ,
"background-color #ffffff #ADB5C7 500ms linear"
]);
}
|
animate(element)
Multiple properties transition with default transition values
If some or all properties share the same transition values like duration or timing function,
you can pass these values inside the third options
argument. Transition
values specified for a specific transition property will override shared transition values.
function animate(element) {
transition.begin(element, [
[ "transform" , "translateX(0)" , "translateX(200px)" ],
[ "background-color" , "#ffffff" , "#ADB5C7" , "2s" ]
], {
timingFunction: "linear" ,
duration: "500ms"
});
}
|
animate(element)
Multiple properties from multiple begin
calls
Sometimes it may be easier to transition multiple properties of the same element from
different begin
calls. Yet, keeping the requirement that all properties begin their
transitions simultaneously. Transition.js ensures that all transitions executed in the same JavaScript
execution context stack will be started together in a separate execution context stack.
function animate(element) {
transition.begin(element, [ "transform" , "translateX(0) rotate(0)" , "translateX(200px) rotate(180deg)" , "1s" , "ease-in-out" ]);
transition.begin(element, [ "background-color" , "#ffffff" , "#ADB5C7" , "1s" , "ease-in-out" ]);
}
|
animate(element)
Transition with onTransitionEnd
callback
The onTransitionEnd
callback is fired when all properties finish their
transitions. This callback receives two parameters: an element
on which the
transition was performed, and a boolean flag finished
specifying if the
transition was finished by reaching its target value (true
), or halted (false
),
for example when another transition with the same transition properties has began.
function animate(element, trFrom, trTo, clrFrom, clrTo, stop) {
transition.begin(element, [
[ "transform" , "translateX(" + trFrom + ")" , "translateX(" + trTo + ")" ],
[ "background-color" , clrFrom, clrTo]
], {
duration: "1s" ,
timingFunction: "ease-in-out" ,
onTransitionEnd: function (element, finished) {
if (!finished || stop) return ;
animate(element, trTo, trFrom, clrTo, clrFrom, true );
}
});
}
|
animate(element, '0', '200px', '#ffffff', '#ADB5C7', false)
Properties with onTransitionEnd
callback
Every property can have its own onTransitionEnd
callback which is executed as
soon as the property finish its transition. Similarly to transition's onTransitionEnd
callback, this callback sldo receives two parameters, the element
and the finished
flag.
function animate(element) {
transition.begin(element, [
[ "transform" , "rotate(0)" , "rotate(360deg)" , "2s" ],
[ "background-color" , "#ffffff" , "#ADB5C7" , "1s" , function (element, finished) {
if (!finished) return ;
transition.begin(element, [ "background-color" , "#ADB5C7" , "#ffffff" , "1s" ]);
}]
], {
timingFunction: "linear"
});
}
|
animate(element)
Transition with onBeforeChangeStyle
callback
The
onBeforeChangeStyle
callback is called after the "
from
" values
of all transition properties were set on the element and before setting the "
to
" and
transition
values. It receives single parameter, the
element
, on which the transition is performed.
This callback tries to mimic the behaviour of the
before-change style
event. This callback is useful when additional element manipulation is required before
beginning the transition, for example setting element's
display
value to
block
when performing fade-in transition.
function animate(element) {
transition.begin(element, [ "opacity 0 1 1s" , "transform translateX(0) translateX(200px) 1s ease-in-out" ], {
onBeforeChangeStyle: function (element) {
element.style.display = "block" ;
},
onTransitionEnd: function (element, finished) {
if (!finished) return ;
element.style.display = "none" ;
}
});
}
|
animate(element)
Transitioning property from its current value
Sometimes, when beginning new transition with a property that is already being transitioned, it is
desired to transition that property from its current value. For example, assume you have two transitions,
"fadeIn" and "fadeOut" transitioning values of the "opacity" from 0 to 1 and from 1 to 0 respectively.
To ensure these transitions transition opacity from its current value, set the
beginFromCurrentValue
option to true
.
function animate(element) {
if (element._side == "right" ) {
element._side = "left" ;
transition.begin(element, "transform translateX(200px) translateX(0) 1s" , {
beginFromCurrentValue: true
});
} else {
element._side = "right" ;
transition.begin(element, "transform translateX(0) translateX(200px) 1s" , {
beginFromCurrentValue: true
});
}
}
|
animate(element)
Adding new transition to an element with already running transition
You can safely add new transitions to an element with already running transitions.
function animate(element) {
element.style.backgroundColor = "#ffffff" ;
transition.begin(element, "transform translateX(0) translateX(200px) 2s ease-in-out" );
window.clearTimeout(element._timeout);
element._timeout = window.setTimeout( function () {
transition.begin(element, "background-color #ffffff #ADB5C7 1s linear" );
}, 1000);
}
|
animate(element)
Of course, same can be done using CSS transition delay property.
function animate(element) {
transition.begin(element, [
"transform translateX(0) translateX(200px) 2s ease-in-out" ,
"background-color #ffffff #ADB5C7 1s linear 1s"
]);
}
|
animate(element)