jQuery background-position animation

Posted by depi on Stack Overflow See other posts from Stack Overflow or by depi
Published on 2010-06-09T15:17:34Z Indexed on 2010/06/09 15:42 UTC
Read the original article Hit count: 370

Hi guys,

I've created an image which is basically a CSS sprite of 3 images together. It's size is 278x123 so they are basically 3 images of 278x41.

What I am trying to do is to make an animation of that by changing the background position.

I've tried many things, one of my not very working solution is the following:

    var $slogan = $('#header h2 span');
$slogan.css({backgroundPosition: '0px 0px'});
function slogan_animation() {
    if ($slogan.css('background-position') == '0px 0px') {
        $slogan.fadeIn('slow').css('background-position', '0px -41px').fadeOut('slow');
    }
    else if ($slogan.css('background-position') == '0px -41px') {
        $slogan.fadeIn('slow').css('background-position', '0px -82px').fadeOut('slow');
    }
    else if ($slogan.css('background-position') == '0px -82px') {
        $slogan.fadeIn('slow').css('background-position', '0px 0px').fadeOut('slow');
    }
}
setInterval(slogan_animation, 2000);

Any ideas how could I do that?

Basically I just need to: - set my background position to "0px 0px", then move it to "0px -41px", then "0px -82px" and then loop it again from "0px 0px". It would be also great to have fadeIn() effect between those.

Any ideas?

Thank you.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about animation