YUI 3.x Home -

YUI Library Examples: Animation: Reversing an Animation

Animation: Reversing an Animation

This demonstrates how to use the reverse attribute to change the behavior of the animation.

Click the icon in the header to toggle the element's height.

Animation Demo

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-toggle"><em>-</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-toggle"><em>-</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>

Using the NodeFX Plugin

For this example, we will use Node's fx plugin to animate the element. The plugin adds the anim instance to the Node instance, pre-configuring it to use the Node instance as the Anim's node. The plug method accepts a class to construct and an optional config to pass to the constructor.

Setting the from attribute to the expanded height of the element allows us to toggle the effect using the reverse attribute, which we will see below (from uses current value when omitted).

  1. var module = Y.get('#demo');
  2.  
  3. // add fx plugin to module body
  4. var content = module.query('.yui-bd').plug(Y.Plugin.NodeFX, {
  5. from: { height: 0 },
  6. to: {
  7. height: function(node) { // dynamic in case of change
  8. return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
  9. }
  10. },
  11.  
  12. easing: Y.Easing.easeOut,
  13. from: { height: 0 },
  14. duration: 0.5
  15. });
var module = Y.get('#demo');
 
// add fx plugin to module body
var content = module.query('.yui-bd').plug(Y.Plugin.NodeFX, {
    from: { height: 0 },
    to: {
        height: function(node) { // dynamic in case of change
            return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
        }
    },
 
    easing: Y.Easing.easeOut,
    from: { height: 0 },
    duration: 0.5
});

Creating the Control Element

Because our behavior only works when JS is available, let's go ahead and add our control element using JS as well.

  1. // use dynamic control for dynamic behavior
  2. var control = Y.Node.create(
  3. '<a title="show/hide content" class="yui-toggle">' +
  4. '<em>toggle</em>' +
  5. '</a>'
  6. );
  7.  
  8. // append dynamic control to header section
  9. module.query('.yui-hd').appendChild(control);
// use dynamic control for dynamic behavior
var control = Y.Node.create(
    '<a title="show/hide content" class="yui-toggle">' +
        '<em>toggle</em>' +
    '</a>'
);
 
// append dynamic control to header section
module.query('.yui-hd').appendChild(control);

Toggling Animation Behavior

Before calling run in our click handler, we will use the reverse attribute toggle the direction of the animation depending on whether its opening or closing.

  1. var onClick = function(e) {
  2. module.toggleClass('yui-closed');
  3. content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
  4. };
var onClick = function(e) {
    module.toggleClass('yui-closed');
    content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
};

Running the Animation

Finally we add an event handler to run the animation.

  1. module.query('.yui-toggle').on('click', onClick);
module.query('.yui-toggle').on('click', onClick);

Full Script Source

  1. YUI().use('anim', function(Y) {
  2. var module = Y.get('#demo');
  3.  
  4. // add fx plugin to module body
  5. var content = module.query('.yui-bd').plug(Y.Plugin.NodeFX, {
  6. from: { height: 0 },
  7. to: {
  8. height: function(node) { // dynamic in case of change
  9. return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
  10. }
  11. },
  12.  
  13. easing: Y.Easing.easeOut,
  14. duration: 0.5
  15. });
  16.  
  17. var onClick = function(e) {
  18. module.toggleClass('yui-closed');
  19. content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
  20. content.fx.run();
  21. };
  22.  
  23. // use dynamic control for dynamic behavior
  24. var control = Y.Node.create(
  25. '<a title="show/hide content" class="yui-toggle">' +
  26. '<em>toggle</em>' +
  27. '</a>'
  28. );
  29.  
  30. // append dynamic control to header section
  31. module.query('.yui-hd').appendChild(control);
  32. control.on('click', onClick);
  33. });
YUI().use('anim', function(Y) {
    var module = Y.get('#demo');
 
    // add fx plugin to module body
    var content = module.query('.yui-bd').plug(Y.Plugin.NodeFX, {
        from: { height: 0 },
        to: {
            height: function(node) { // dynamic in case of change
                return node.get('scrollHeight'); // get expanded height (offsetHeight may be zero)
            }
        },
 
        easing: Y.Easing.easeOut,
        duration: 0.5
    });
 
    var onClick = function(e) {
        module.toggleClass('yui-closed');
        content.fx.set('reverse', !content.fx.get('reverse')); // toggle reverse
        content.fx.run();
    };
 
    // use dynamic control for dynamic behavior
    var control = Y.Node.create(
        '<a title="show/hide content" class="yui-toggle">' +
            '<em>toggle</em>' +
        '</a>'
    );
 
    // append dynamic control to header section
    module.query('.yui-hd').appendChild(control);
    control.on('click', onClick);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

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