views:

100

answers:

2

I know I can fade in and out divs and elements, but can I animate properties as well? I thought about this just now when changing background-image property of a certain div. And thought it'd be cool if the background image was faded in as well ;)

Here's the code I'm using to replace background image. But can it be animated?

var originalBG = $('#wrapper').css('background-image');
$('.bggallery_images img').hover(function () {
    var newBG = "url('" + $(this).attr('src');

    $('#wrapper').css('background-image', newBG);
}, function () {
    $('#wrapper').css('background-image', originalBG);
});
+2  A: 

jQuery has the animate() method that does exactly that.

Check out the examples in the manual, they should show you everything you need.

Pekka
Just checked it out. But it doesn't seem like it recognize background-image. Background seems to work though. Would you mind having a peek at my OP, updated with some code.
Kenny Bones
@Kenny mmm, overread that part, I understood `background-color`. `background-image` is not going to work, that is not fadable in CSS and jQuery can't add that functionality. You'd have to change the `opacity` of the element containing the background image, or introduce a new element for that.
Pekka
Yeah, thats what I figured. I'd have to apply a new set of wrapper div and apply the new background image onto that. Then fade the old one away.
Kenny Bones
+1  A: 

JQuery UI is a plug in that extends the animation function to animate color if that's what you're looking to do with the background. Basically if JQuery doesn't do it, there's most likely a library that extends it to have that functionality.

Alex Larzelere