1

I'm trying to animate the background color of a div onclick but it's simply not changing it:

HTML

<div id="list2" onclick="changeCol();">
    color
</div>

CSS

#list2{ width:200px; height:100px; border:1px solid #ccc; background-color:#ccc}

JQuery/JS

function changeCol(){
    alert('foo'); // testing function called
    $("#list2").animate({
      backgroundColor: "#8bed7e"
    }, 500).animate({
      backgroundColor: "#ccc"
    }, 500);
}

Any ideas why? Here's a fiddle

Turnip
  • 35,836
  • 15
  • 89
  • 111
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • jquery animations only work on numeric values, you cannot use it for background color – Kinnza Dec 04 '14 at 11:12
  • 4
    Possible duplicate of [jQuery animate backgroundColor](http://stackoverflow.com/questions/190560/jquery-animate-backgroundcolor) – Abhinav S Feb 10 '17 at 10:56

7 Answers7

5

use CSS3 (will work for CSS3 supporting browsers only, unsupported browsers WILL change the color but without animation):

html:

<div id="list2">color</div>

css:

#list2{background:red;transition:0.5s ease;-moz-transition:0.5s ease;-webkit-transition:0.5s ease;}
#list2.clicked{background:blue;}

js:

$(function(){ 
    $('#list2').click(function(){ 
        $(this).toggleClass('clicked') 
    }) 
});

working example here: http://jsfiddle.net/18v8ofwn/1/

note that instead of CSS3 you can use the jQuery UI library (see mour answer), without a single change for your code (cross-browser solution).

hope that helps.

geevee
  • 5,411
  • 5
  • 30
  • 48
0

check

http://jsfiddle.net/qggyec4t/16/

$(document).ready(function () {

    $("#list2").click(function () {
        alert('hii');
        $("#list2").animate({
            backgroundColor: 'red',
        });
    });
});
mydeve
  • 553
  • 1
  • 14
  • 39
0

You have to use jquery-ui.js and jquery-ui.css with your code. It will work perfectly fine.

Updated JSFiddle

function changeCol(){
    $("#list2").animate({
       backgroundColor: "#8bed7e"
    }, 500).animate({
        backgroundColor: "#ccc"
    }, 500);
}
rishiAgar
  • 168
  • 3
  • 10
0

Here's a working code for what you're trying to do using jquery-color-plugin as it says in the Official jQuery documentation

All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color plugin is used). Property values are treated as a number of pixels unless otherwise specified. The units em and % can be specified where applicable.

function changeCol() {
  $("#list2").animate({
    backgroundColor: "red"
  }, 500).animate({
    backgroundColor: "green"
  }, 500);
}
#list2 {
  width: 200px;
  height: 50px;
  border: 1px solid #ccc;
  background-color: #ccc;
  text-align: center;
  vertical-align: center;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://code.jquery.com/color/jquery.color.plus-names-2.1.2.js"></script>
<div id="list2" onclick="changeCol()">
  color
</div>

JS Fiddle Code Snippet Link

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Abhinav S
  • 111
  • 3
  • 12
-1

I had this issue before. JQuery cannot animate background color, you need a plugin to do that.

Here's one that does it:

http://www.bitstorm.org/jquery/color-animation/

https://github.com/jquery/jquery-color/ is another

roryok
  • 9,325
  • 17
  • 71
  • 138
-1

jQuery, by default, can't animate colors. I'm using the following plugin when I have to do such things:

https://github.com/jquery/jquery-color/

You can also animate the background using CSS3 which performs really well (see: Transition of background-color)

Community
  • 1
  • 1
posixpascal
  • 3,031
  • 3
  • 25
  • 44
-1

DO you have a color plugin? You can't animate background color without it. You can find one here: https://github.com/jquery/jquery-color

Sheremet
  • 32
  • 3