I’m trying to change the background image of a button. Using the css() function changes the background instantly. Is there a way to do a crossfade effect?
Here’s what I’ve got, but the fade effect doesn’t work:
var img_button_pos = 'images/forms/buttonbg_green.gif';
var img_button_neg = 'images/forms/buttonbg_red.gif';
var img_button = 'images/forms/buttonbg.gif';
$().ready(function(){
$('button.type_pos').hover(
function(){
$(this).animate({backgroundImage: 'url("'+img_button_pos+'")'}, 500);
},
function(){
$(this).css('background-image', 'url("'+img_button+'")');
}
);
});
You could fade the original background to about 5% or some low amount and then change the background source to the new image already set to the say 5% opacity then fade it in to 100%.
Not really, one of them can just not have a name attribute at all. You can generate the extra button with JavaScript, and remove one button after the crossfading is done.
If javascript is disabled, you just have one button with no fancy effects (or just one image). If JavaScript is enabled, it creates the second button, adds the second image and adds the event listeners for the crossfade effect.