1

In this demo http://www.htmldrive.net/items/demo/527/Animated-background-image-with-jQuery

This code is for one background only. I want to add multiple background with different direction and speed.

 var scrollSpeed = 70;
 var step = 1;
 var current = 0;
 var imageWidth = 2247;
 var headerWidth = 800;  

 var restartPosition = -(imageWidth - headerWidth);

 function scrollBg(){
  current -= step;
  if (current == restartPosition){
   current = 0;
  }

  $('#header').css("background-position",current+"px 0");
 }

 var init = setInterval("scrollBg()", scrollSpeed);

Currently it has settings for

  $('#header').css("background-position",current+"px 0");

In a website I want to use this effect on #footer or #content background also. but with different speed and direction.

And is there any better and more optimized jquery method to achieve same effect?

And can we get same effect using CSS 3, without javascript?

Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
  • 3
    Don't use a string as the first parameter of `setInterval` just pass the function, e.g `setInterval(scrollBg, scrollSpeed)` passing a string will call `eval` in the background, which is evil. – Ivo Wetzel Dec 26 '10 at 18:10

2 Answers2

4

Just saw the OP's answer, but decided to post anyway:

I've created a jQuery plugin to do this:

(function($) {
    $.fn.scrollingBackground = function(options) {
        // settings and defaults.
        var settings = options || {};
        var speed = settings.speed || 1;
        var step = settings.step || 1;
        var direction = settings.direction || 'rtl';
        var animStep;

        // build up a string to pass to animate:
        if (direction === 'rtl') {
            animStep = "-=" + step + "px";
        }
        else if (direction === 'ltr') {
            animStep = '+=' + step + "px";
        }

        var element = this;

        // perform the animation forever:
        var animate = function() {
            element.animate({
                backgroundPosition: animStep + " 0px"
            }, speed, animate);           
        };
        animate();
    };
})(jQuery);

Usage:

$("#header").scrollingBackground({
    speed: 50,
    step: 50,
    direction: 'ltr'
});

This is pretty basic, and assumes that you're background-repeat is 'repeat-x' on the element you call it on. This way, there's no need to reset the background position every so often.

Working example: http://jsfiddle.net/andrewwhitaker/xmtpr/

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

I could work out the following solution. Am not sure if it is efficient. Will wait for anyone to comment or provide a better option. Till then...:

var scrollSpeed = 70;
 var step = 1;
 var current = 0;
 var images = 
 [
    {
    imageWidth:2247,
    imagePath:"images/image1"
    },
    {
    imageWidth:1200,
    imagePath:"images/image2"
    }
 ]
 var headerWidth = 800;  
 var imageRotateCount = 0;
 var imagesLength = images.length;

 $('#header').css("background-image", images[0].imagePath);

 function scrollBg(){
    var curIndex = imageRotateCount%imagesLength;
    var curImage = images[curIndex];
  current -= step;

    var restartPosition = -(curImage.imageWidth - headerWidth);
  if (current == restartPosition){
   current = 0;
     imageRotateCount++;
     curIndex = imageRotateCount%imagesLength;
     curImage = images[curIndex];
     $('#header').css("background-image", curImage.imagePath);
  }

  $('#header').css("background-position",current+"px 0");
 }

 var init = setInterval("scrollBg()", scrollSpeed);
Chandu
  • 81,493
  • 19
  • 133
  • 134