add( )
Adds an animation to a timeline.
Description
The add()
function adds one or more animations on one or more page
elements to a timeline. Any element that mixes in the HiddenMixin
can be added as an animation target
in a timeline.
By default, when no offset
is specified ,the animations in a timeline run in
the order they are added. When multiple animations are added using a single call
to the add function, those animations default to run together at the same time
within the timeline.
You can override the default order that the animations run using the offset
parameter.
You can add multiple add()
functions to make more complex animations.
Syntax
function add(target: Element | Array<Element>, animation: AnimationAttributes | Array<AnimationAttributes>, [offset: number | string]): TimeLine
add Parameters
NAME
TYPE
DESCRIPTION
Attributes of the animation or animations to add to the timeline.
When the animation starts in the timeline. When no
offset
is specified, the animation added starts after the previous animation ends.
The offset
can be relative to the last animation added or absolute within the whole
timeline using this following formats:
- A non-negative number specifying the absolute offset from the beginning of the timeline in milliseconds, regardless of the animation's position in the timeline.
- A
"+="
expression to specify a relative offset in milliseconds, where the animation starts the specified number of milliseconds after the previous animation in the timeline ends. For example,"+=1000"
starts the current animation 1 second after the previous animation ends. - A
"-="
expression to specify a relative offset in milliseconds, where the animation starts the specified number of milliseconds before the previous animation in the timeline ends. For example,"-=1000"
starts the current animation 1 second before the previous animation ends.
If the specified relative offset
is before the current start of the timeline,
the timeline is lengthened and the beginning of the timeline is now the start
of the new animation. When played, the timeline will begin playing from its
original start.
For example, if a timeline is 1000 milliseconds long and you add an animation with
an offset
of -500
, the timeline length will become 1500 milliseconds and the
beginning of the timeline now be the start time of the newly added animation.
Was this helpful?
You can test out the code in our example template.
1import { timeline } from 'wix-animations-frontend';23$w.onReady(() => {4 const revealTimeline = timeline()5 .add($w('#pink'), {duration: 1500, x: -160, scale: 1.3, easing: 'easeOutBack'})6 .add($w('#green'), {duration: 1500, x: 160, scale: 1.3, easing: 'easeOutBack'}, 0)78 $w('#container').onMouseIn(() => {9 revealTimeline.play();10 });1112 $w('#container').onMouseOut(() => {13 revealTimeline.reverse();14 });15});
1import wixAnimationsFrontend from 'wix-animations-frontend';23let timeline = wixAnimationsFrontend.timeline();45// ...67const myImage = $w("#myImage");89timeline.add(myImage, {10 "rotate": 360,11 "scale": .5,12 "duration": 100013});1415timeline.add(myImage, {16 "opacity": 0,17 "duration": 100018});
1import wixAnimationsFrontend from 'wix-animations-frontend';23let timeline1 = wixAnimationsFrontend.timeline();4let timeline2 = wixAnimationsFrontend.timeline();56// ...78const myImage1 = $w("#myImage1");9const myImage2 = $w("#myImage2");1011// In timeline1, the rotation animation and opacity12// change animation will happen at the same time.13timeline1.add(myImage1,14 [15 {16 "rotate": 360,17 "scale": .5,18 "duration": 1000,19 "easing": "easeInCirc"20 }, {21 "delay": 500,22 "opacity": 0,23 "duration": 50024 }25 ]26);2728// In timeline2, the rotation animation and opacity29// change animation will happen one after the other.30timeline2.add(myImage2, {31 "rotate": 360,32 "scale": .5,33 "duration": 100034});3536timeline2.add(myImage2, {37 "opacity": 0,38 "duration": 100039});
1import wixAnimationsFrontend from 'wix-animations-frontend';23let timeline = wixAnimationsFrontend.timeline();45// ...67const myImage1 = $w("#myImage1");8const someImages = $w("#myImage2", "#myImage3");9const vectorImages = $w("VectorImage");1011// Adds an animation for myImage112timeline.add(myImage1, {13 "rotate": 360,14 "scale": .5,15 "duration": 100016})1718// Adds an animation for myImage219// and myImage3 together20timeline.add(someImages, {21 "rotate": -360,22 "scale": .5,23 "duration": 100024})2526// Adds an animation for all the27// vector images on the page28timeline.add(vectorImages, {29 "opacity": 0,30 "duration": 100031})
1import wixAnimationsFrontend from 'wix-animations-frontend';23let timeline = wixAnimationsFrontend.timeline();45$w.onReady( function () {6 const myImage1 = $w("#myImage1");7 const myImage2 = $w("#myImage2");8 const myImage3 = $w("#myImage3");910 timeline11 .add(12 [myImage1, myImage2],13 {14 "rotate": 360,15 "scale": .5,16 "duration": 100017 }18 )19 .add(20 myImage3,21 {22 "opacity": 0,23 "duration": 50024 },25 "-=500"26 )27 .play();28} );