Search code examples
jquerybackground-imagecss-spritesbackground-position

using background-position to spread background accross multiple items


So I am working what should be a nifty effect. I have a bunch of li's of height 40px. I want a background image to display so that the top 40px is the background for the first li, the next 40px is the background for the second one... etc etc...

currently it will only show the top section of the image, I cannot get bgCount to increment... Any suggestions?

        var bgCount = 0;
        $('li').each(function(i) {                
           $(this).css('background-image', 'url(forground.jpg)');
           $(this).css('background-position', '0px' + bgCount + 'px');
           bgCount += 40;
        });

JSfiddle: http://jsfiddle.net/yZDDK/4/


Solution

  • You're missing a space

           $(this).css('background-position', '0px ' + bgCount + 'px');
                                                  ^