I display 40+ boxes on a page:
<div id="primary">
<div class="box" style="background:....">
<a href="" style="color:....">box1</a>
</div>
<div class="box" style="background:....">
<a href="" style="color:....">box2</a>
</div>
....
</div>
As you can see I set background-color and text-color. On hover I want to swap the colors:
$(".box").each( function () {
$(this).data('baseColor',$(this).css('background-color'));
$(this).find('a').data('fontColor',$(this).css('color'));
$(this).hover(function() {
$(this).animate({ backgroundColor:
$(this).data('fontColor') }, 500);
},function() {
$(this).animate({ backgroundColor:
$(this).data('baseColor') }, 1000);
});
});
The animation of the background-color works but I can not change the font color of the a element. Any ideas?
As @Brandon mentioned, you need jQuery UI (or something 😉 to animate non-integer properties.
The bigger issue is the change of context in your
eachcallback: inside thehovercallback methods, the value ofthiswon’t always be what you want. Also, creating new jQuery objects (with$(...)) is relatively expensive. Try:Demo here.