Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
bobdobbs

How do I execute a function after an animation has completed?

Recommended Posts

How do I execute a function after an animation has completed?

 

I've got a simple animation:

I grab an element, and then I animate it.

 

Lets say my animation function is called changeState().  I want to do this sequence:

 

1. changeState()

2. selectNextElement()

3. changeState()

 

selectNextElement() should only run once changeState() has completed it's animation sequence.

Here's my stub for changeState:


// get current block and animate it
function changeState() {
    var currentBlock = $(".current") ;
   
    // colortween the current block
    var tl = new TimelineMax() ;
    tl.to(
        currentBlock, 1, { backgroundColor:"rgb(205,151,20)" }
    )
        .to(
            currentBlock, 1, { backgroundColor:"rgb(152, 129, 74)" }
        ) ;
}
Link to post
Share on other sites

You can add an onComplete callback to the timeline OR the last tween. 

In your case I would just put it in the constructor of the timeline like so:

var tl = new TimelineMax({onComplete:someFunction}) ;
tl.to(elem, 0.2, {left:200});

function someFunction() {
  console.log("animation complete");
}

http://www.greensock.com/jump-start-js/#event-callbacks

http://codepen.io/GreenSock/pen/srtqe

 

You can also pass in parameters and scope (if necessary)

See the TimelineLite / TimelineMax special properties section of the docs for all the details: http://api.greensock.com/js/

  • Like 3
Link to post
Share on other sites

You can use the onComplete property to do this:

// get current block and animate it
function changeState() {
    var currentBlock = $(".current") ;
   
    // colortween the current block
    var tl = new TimelineMax({ onComplete: selectNextElement }}) ;
    tl.to(
        currentBlock, 1, { backgroundColor:"rgb(205,151,20)" }
    )
    .to(
        currentBlock, 1, { backgroundColor:"rgb(152, 129, 74)" }
    ) ;
}

function selectNextElement() {
    // code here
}

You can read more about the special properties of Timelines and Tweens in the API

  • Like 2
Link to post
Share on other sites

I tried detecting completion without TimelineMax by putting the onComplete property into the first animation, but it doesn't work for me:

 

window.onload = function(){
    var logo = document.getElementById("logo");
    TweenLite.to(logo,1, {left:"440px", ease:Power1.easeOut, onCompleteParams:a1});
    }

function a1(){
    TweenLite.to(logo, 1, {left:"0px"});
    } // a1

Link to post
Share on other sites

Hi and welcome to the GreenSock forums.

 

Without a live sample and by looking to the code you posted, all I can see is that you are using the onCompleteParams to pass the method you want to be called when the animation is completed. You need to use onComplete and pass the function's name or variable holding an anonymous function:

 

window.onload = function(){
  var logo = document.getElementById("logo");
  TweenLite.to(logo,1, {
    left:"440px", ease:Power1.easeOut,
    onComplete:a1
  });
}

function a1(){
  TweenLite.to(logo, 1, {left:"0px"});
}

 

Try that code and let us know if it works. Also I'd recommend you to check the official documentation to learn the difference between onComplete and onCompleteParams. Follow the next link and scroll down to the part that says: Special properties, eases and callbacks (no plugins required)

 

https://greensock.com/docs/TweenLite

 

Happy Tweening!!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×