简体   繁体   中英

Animating to another background-color, then back again

I'm trying to make my white background flash green. Currently I have this code which turns it green:

$( "#modal_newmessage" ).animate({
    backgroundColor: "#8bed7e"
}, 500 );

However, I can't figure how to make it turn white again in the same animation. How do I do this?

You can chain another .animate() call since the animations will be queued in the fx queue.

$("#modal_newmessage").animate({
  backgroundColor: "#8bed7e"
}, 500).animate({
  backgroundColor: "#fff"
}, 500);

Remember most jQuery functions are chainable without need to call $("#modal_newmessage") twice.

See it here.

This should work:

$( "#modal_newmessage" ).animate({
    backgroundColor: "#8bed7e"
}, 500, function() {
    $( "#modal_newmessage" ).animate({ backgroundColor: "#fff" });
} );

Try this:

$(function() {

    $( "#modal_newmessage" ).animate({
      backgroundColor: "#aa0000",
      color: "#fff",
      width: 500
    }, 1000 ).animate({
      backgroundColor: "#fff",
      color: "#000",
      width: 240
    }, 1000 );
});

note:

For animating background colors you need jQuery ui for color animation.

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM