11

I want to change the background color of 'exampleDiv' from the original white background to when I call the code below to immediate change the background yellow and then fade back to the original white background.

$("#exampleDiv").animate({ backgroundColor: "yellow" }, "fast");

However, this code does not work.

I have only the JQuery core and JQuery UI linked to my web page.

Why doesn't the code above work?

AndyT
  • 217
  • 1
  • 3
  • 8
  • possible duplicate of [jQuery animate backgroundColor](http://stackoverflow.com/questions/190560/jquery-animate-backgroundcolor) – mercator Dec 03 '10 at 10:02
  • Does this answer your question? [jQuery animate backgroundColor](https://stackoverflow.com/questions/190560/jquery-animate-backgroundcolor) – Heretic Monkey Dec 02 '19 at 16:03

9 Answers9

10

I've had varying success with animate, but found that using its built in callback plus jQuery's css seems to work for most cases.

Try this function:

$(document).ready(function () {

    $.fn.animateHighlight = function (highlightColor, duration) {
        var highlightBg = highlightColor || "#FFFF9C";
        var animateMs = duration || "fast"; // edit is here
        var originalBg = this.css("background-color");

        if (!originalBg || originalBg == highlightBg)
            originalBg = "#FFFFFF"; // default to white

        jQuery(this)
            .css("backgroundColor", highlightBg)
            .animate({ backgroundColor: originalBg }, animateMs, null, function () {
                jQuery(this).css("backgroundColor", originalBg); 
            });
    };
});

and call it like so:

$('#exampleDiv').animateHighlight();

Tested in IE9, FF4, and Chrome, using jQuery 1.5 (do NOT need UI plugin for this). I didn't use the jQuery color plugin either - you would only need that if you want to use named colors (e.g. 'yellow' instead of '#FFFF9C').

Peter Gibbons
  • 103
  • 2
  • 15
matadur
  • 819
  • 9
  • 10
  • 5
    In this example yellow backqround doesn't turn into white gradually. After the defined period of time, bacground becomes white suddenly in jQuery 1.5.2 on FF3. – sevenkul Feb 20 '12 at 21:03
  • thx alot! I been searching alot to find a small plugin for this effect. – william Apr 30 '12 at 14:54
9

I believe you also need JQuery Color Animations.

ckhatton
  • 1,359
  • 1
  • 14
  • 43
recursive
  • 83,943
  • 34
  • 151
  • 241
8

I had the same problem and I was able to get everything to work when I included the correct js files.

<script src="/Scripts/jquery-1.9.1.js"></script>
<script src="/Scripts/jquery-ui-1.8.20.js"></script>
<script src="/Scripts/jquery.color-2.1.2.js"></script>

I took it a step further and found a nice extension someone wrote.

jQuery.fn.flash = function (color, duration) {
   var current = this.css('backgroundColor');
   this.animate({ backgroundColor: 'rgb(' + color + ')' }, duration / 2)
   .animate({ backgroundColor: current }, duration / 2);
}

The above code allows me to do the following:

$('#someId').flash('255,148,148', 1100);

That code will get your element to flash to red then back to its original color.

Here's some sample code. http://jsbin.com/iqasaz/2

inVINCEble
  • 171
  • 1
  • 4
5

The jQuery UI has a highlight effect that does exactly what you want.

   $("exampleDiv").effect("highlight", {}, 5000);

You do have some options like changing the highlight colour.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • This requires the Effects package. I'm looking for the most lightweight way for me to fade a background color within having to use a bunch of different library :( – AndyT Nov 07 '09 at 20:35
3

Animating the backgroundColor is not supported in jQuery 1.3.2 (or earlier). Only parameters that take numeric values are supported. See the documentation on the method. The color animations plugin adds the ability to do this as of jQuery 1.2.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
2

I came across the same issue and ultimately it turned out to be multiple call of jquery's js file on the page.

While this works absolutely fine with any other methods and also with animate when tried with other css properties like left, but it doesn't work for background color property in animate method.

Hence, I removed the additional call of jquery's js file and it worked absolutely fine for me.

Jay
  • 181
  • 1
  • 4
1

For me, it worked fine with effects.core.js. However, I don't recall whether that's really required. I think that it only works with hexadecimal values. Here's a sample hover code that makes things fade as you hover. Thought it might be useful:

jQuery.fn.fadeOnHover = function(fadeColor)
{
    this.each(function()
    {
        jQuery(this).data("OrigBg",jQuery(this).css("background-color"));
        jQuery(this).hover(
            function()
            {
                //Fade to the new color
                jQuery(this).stop().animate({backgroundColor:fadeColor}, 1000)
            }, 
            function()
            {
                //Fade back to original color
                original = jQuery(this).data("OrigBg");
                jQuery(this).stop().animate({backgroundColor:original},1000) 
            }
        );
    });
}
$(".nav a").fadeOnHover("#FFFF00");
Eric
  • 95,302
  • 53
  • 242
  • 374
1

Just added this snippet below jquery script and it immediately started working:

<script src="https://cdn.jsdelivr.net/jquery.color-animation/1/mainfile"></script>

Source

Darush
  • 11,403
  • 9
  • 62
  • 60
  • This answer shouldn't be overlooked! I had worked with `$(el).animate()` before, and wasn't quite sure why it stopped working. Adding this as a src worked like a charm! – Malmadork Sep 17 '20 at 04:42
  • Actually, this seems to have a small issue with AJAX refresh... Animations happen the first time, but not on a refresh. – Malmadork Sep 17 '20 at 05:01
1

I had to use the color.js file to get this to work. I'm using jquery 1.4.2.

Get the color.js here

aruffo3
  • 55
  • 4