I couldn't make this backgroundcolor fade out work
$("#x").css("background-color", "#00FF00")
$("#x").animate({ backgroundColor: '#000000' }, 2000)
I couldn't make this backgroundcolor fade out work
$("#x").css("background-color", "#00FF00")
$("#x").animate({ backgroundColor: '#000000' }, 2000)
The way you used it is part of jquery-ui
Color Animation. You can include it to your project:
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
I'd suggest you do this with CSS transitions:
#x
{
-webkit-transition: background-color 2s; /* For Safari 3.1 to 6.0 */
transition: background-color 2s;
}
Then merely set the CSS or toggle a class without worrying about the animation:
$("#x").css("background-color", "#000000");
$("#x").css("background-color", "#00FF00");
I would probably prefer to toggle a class if it were me and the context was right for it:
CSS:
#x
{
-webkit-transition: background-color 2s; /* For Safari 3.1 to 6.0 */
transition: background-color 2s;
background-color: #00FF00;
}
#x.black
{
background-color: #000000;
}
JS:
$('#x').toggleClass('black');
Here you've got a nice separation of functionality and style.
Transitions can have certain performance benefits, but be sure to check compatibility if you choose this route: