YUI 3.x Home -

YUI Library Examples: Animation: Chaining Animations Using the end Event

Animation: Chaining Animations Using the end Event

This demonstrates how to use the end event to chain animations runs together.

Click the X in the header to fade the element out, then shrink its height to zero.

Animation Demo

x

This an example of what you can do with the YUI Animation Utility.

Follow the instructions above to see the animation in action.

This is placeholder text used to demonstrate how the above animation affects subsequent content.

Setting up the HTML

First we add some HTML to animate.

  1. <div id="demo" class="yui-module">
  2. <div class="yui-hd">
  3. <h4>Animation Demo</h4>
  4. <a title="remove module" class="yui-remove"><em>x</em></a>
  5. </div>
  6. <div class="yui-bd">
  7. <p>This an example of what you can do with the YUI Animation Utility.</p>
  8. <p><em>Follow the instructions above to see the animation in action.</em></p>
  9. </div>
  10. </div>
<div id="demo" class="yui-module">
    <div class="yui-hd">
        <h4>Animation Demo</h4>
        <a title="remove module" class="yui-remove"><em>x</em></a>
    </div>
    <div class="yui-bd">
        <p>This an example of what you can do with the YUI Animation Utility.</p>
        <p><em>Follow the instructions above to see the animation in action.</em></p>
    </div>
</div>

Creating the Anim Instance

Now we create an instance of Y.Anim, passing it a configuration object that includes the node we wish to animate and the to attribute containing the properties to be transitioned and final values.

  1. var anim = new Y.Anim({
  2. node: '#demo',
  3. to: {
  4. opacity: 0
  5. }
  6. });
var anim = new Y.Anim({
    node: '#demo',
    to: {
        opacity: 0
    }
});

Handling the End Event

We will need a function to run when the end event fires. Note that the context of our handler defaults to anim, so this refers to our Anim instance inside the handler.

  1. var onEnd = function() {
  2. this.unsubscribe('end', onEnd);
  3. this.setAttrs({
  4. to: { height: 0 },
  5. easing: Y.Easing.bounceOut
  6. });
  7. this.run();
  8. };
var onEnd = function() {
    this.unsubscribe('end', onEnd);
    this.setAttrs({
        to: { height: 0 },
        easing: Y.Easing.bounceOut
    });
    this.run();
};

Listening for the End Event

Now we will use the on method to subscribe to the end event, passing it our handler.

  1. anim.on('end', onEnd);
anim.on('end', onEnd);

Running the Animation

Finally we add an event handler to run the animation.

  1. Y.get('#demo .yui-remove').on('click', anim.run, anim);
Y.get('#demo .yui-remove').on('click', anim.run, anim);

Full Script Source

  1. YUI().use('anim', function(Y) {
  2. var anim = new Y.Anim({
  3. node: '#demo',
  4. to: { opacity: 0 }
  5. });
  6.  
  7. var onEnd = function() {
  8. this.unsubscribe('end', onEnd);
  9. this.setAttrs({
  10. to: { height: 0 },
  11. easing: Y.Easing.bounceOut
  12. });
  13. this.run();
  14. };
  15.  
  16. anim.on('end', onEnd);
  17.  
  18. Y.get('#demo .yui-remove').on('click', anim.run, anim);
  19.  
  20. });
YUI().use('anim', function(Y) {
    var anim = new Y.Anim({
        node: '#demo',
        to: { opacity: 0 }
    });
 
    var onEnd = function() {
        this.unsubscribe('end', onEnd);
        this.setAttrs({
            to: { height: 0 },
            easing: Y.Easing.bounceOut
        });
        this.run();
    };
 
    anim.on('end', onEnd);
 
    Y.get('#demo .yui-remove').on('click', anim.run, anim);
 
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings