I am trying to animate a change in backgroundColor using jQuery on mouseover.
I have checked some example and I seem to have it right, it works with other properties like fontSize, but with backgroundColor I get and "Invalid Property" js error. The element I am working with is a div.
$(".usercontent").mouseover(function() {
$(this).animate({ backgroundColor: "olive" }, "slow");
});
Any ideas?
Simply add the following snippet bellow your jquery script and enjoy:
See the example
Reference for more info
You can use 2 divs:
You could put a clone on top of it and fade the original out while fading the clone in.
When the fades are done, restore the original with the new bg.
jsFiddle example
.toggleClass()
.offset()
.fadeIn()
.fadeOut()
I like using delay() to get this done, here's an example:
This can be called by a function, with "element" being the element class/name/etc. The element will instantly appear with the #FCFCD8 background, hold for a second, then fade into #EFEAEA.
I stumbled across this page with the same issue, but the following problems:
With the above that pretty much ruled out every answer. Considering my fade of colour was very simple, I used the following quick hack instead:
The above creates a temporary div that is never placed in the document flow. I then use jQuery's built-in animation to animate a numeric property of that element - in this case
width
- which can represent a percentage (0 to 100). Then, using the step function, I transfer this numeric animation to the text colour with a simple hex cacluation.The same could have been achieved with
setInterval
, but by using this method you can benefit from jQuery's animation methods - like.stop()
- and you can useeasing
andduration
.Obivously it's only of use for simple colour fades, for more complicated colour conversions you'll need to use one of the above answers - or code your own colour fade math :)
Try this one:
you can preview example here: http://jquerydemo.com/demo/jquery-animate-background-color.aspx
I had the same problem and fixed it by including jQuery UI. Here is the complete script :