YUI 3.x Home -

YUI Library Examples: Animation: Animating Colors

Animation: Animating Colors

This demonstrates how to animate color attributes.

Mouse over the link to begin the animation.

hover me

Setting up the HTML

First we add some HTML to animate.

  1. <a href="#" id="demo">hover me</a>
<a href="#" id="demo">hover me</a>

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 final properties and their values.

Adding a from attribute allows us to reset the colors onmouseout using the reverse attribute, which we will see below.

  1. var node = Y.get('#demo');
  2.  
  3. var anim = new Y.Anim({
  4. node: node,
  5. from: {
  6. backgroundColor:node.getStyle('backgroundColor'),
  7. color: node.getStyle('color'),
  8. borderColor: node.getStyle('borderTopColor')
  9. },
  10.  
  11. to: {
  12. color: '#fff',
  13. backgroundColor:'#ffa928',
  14. borderColor: '#71241a'
  15. },
  16.  
  17. duration:0.5
  18. });
var node = Y.get('#demo');
 
var anim = new Y.Anim({
    node: node,
    from: {
        backgroundColor:node.getStyle('backgroundColor'),
        color: node.getStyle('color'),
        borderColor: node.getStyle('borderTopColor')
    },
 
    to: {
        color: '#fff',
        backgroundColor:'#ffa928',
        borderColor: '#71241a'
    },
 
    duration:0.5
});

Running the Animation

Next we need a handler to run when the link is moused over, and reverse when moused out.

  1. var hover = function(e) {
  2. var reverse = false;
  3. if (anim.get('running')) {
  4. anim.pause();
  5. }
  6. if (e.type === 'mouseout') {
  7. reverse = true;
  8. }
  9. anim.set('reverse', reverse);
  10. anim.run();
  11. };
var hover = function(e) {
    var reverse = false;
    if (anim.get('running')) {
        anim.pause();
    }
    if (e.type === 'mouseout') {
        reverse = true;
    }
    anim.set('reverse', reverse);
    anim.run();
};

Listening for the Events

Finally we add an event handler to run the animation.

  1. node.on('mouseover', hover);
  2. node.on('mouseout', hover);
node.on('mouseover', hover);
node.on('mouseout', hover);

Full Script Source

  1. YUI().use('anim-color', function(Y) {
  2. var node = Y.get('#demo');
  3.  
  4. var anim = new Y.Anim({
  5. node: node,
  6. from: {
  7. backgroundColor:node.getStyle('backgroundColor'),
  8. color: node.getStyle('color'),
  9. borderColor: node.getStyle('borderTopColor')
  10. },
  11.  
  12. to: {
  13. color: '#fff',
  14. backgroundColor:'#ffa928',
  15. borderColor: '#71241a'
  16. },
  17.  
  18. duration:0.5
  19. });
  20.  
  21. var hover = function(e) {
  22. var reverse = false;
  23. if (anim.get('running')) {
  24. anim.pause();
  25. }
  26. if (e.type === 'mouseout') {
  27. reverse = true;
  28. }
  29. anim.set('reverse', reverse);
  30. anim.run();
  31. };
  32. node.on('mouseover', hover);
  33. node.on('mouseout', hover);
  34. });
YUI().use('anim-color', function(Y) {
    var node = Y.get('#demo');
 
    var anim = new Y.Anim({
        node: node,
        from: {
            backgroundColor:node.getStyle('backgroundColor'),
            color: node.getStyle('color'),
            borderColor: node.getStyle('borderTopColor')
        },
 
        to: {
            color: '#fff',
            backgroundColor:'#ffa928',
            borderColor: '#71241a'
        },
 
        duration:0.5
    });
 
    var hover = function(e) {
        var reverse = false;
        if (anim.get('running')) {
            anim.pause();
        }
        if (e.type === 'mouseout') {
            reverse = true;
        }
        anim.set('reverse', reverse);
        anim.run();
    };
    node.on('mouseover', hover);
    node.on('mouseout', hover);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

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