Scrolling Background
July 16th, 2010

A friend sent me a link to a site that had an interesting feature – a scrolling page background. After looking at the HTML code, I found that it is being done with Javascript. Even better, this can easily be done in SiteSpinner.

The script is designed to scroll the background image by changing its left position by a set number of pixels at a set time interval. When the background image reaches its left edge, the direction of movement is reversed so the animation is continuous. To use the script, place the following code inside a code object and make sure the code placement is set to “in Body.”

<script type='text/javascript'>
  var speed = 100;
  var ver_pos = 'center';
  var num = wid = -500;
  var dir = 0;
  var bgOb = eval('document.body');
  function scrollBG() {
    if (dir == 1) {
      if ((++num) >= 0) dir = 0;
    }
    else if (dir == 0) {
      if ((--num) <= wid) dir = 1;
    }
    bgOb.style.backgroundPosition = num+'px '+ver_pos;
  }
  setInterval('scrollBG()',speed);
</script>

In the script, you can change the following variables:

Variable Use
speed This is used to set the scrolling speed. The value is in msec (1000 = 1 second); the lower the number, the faster the scroll.
ver_pos This is used to help set the vertical position of the background image. If your background image height is not large enough to cover the entire page, you can use this variable to shift the image to a more desirable position using terms like “center”, “top”, or “bottom”. You can also specify the top position as a percentage of the page (e.g., “25%” ) or in actual units such as “150px”. This is a string value so remember to use quotes around the value.
num

This is used for two purposes: a) It specifies the beginning X-position of the image, and b) It specifies the limit at which point the scrolling reverses direction. A more detailed explanation of this variable will be given below.

The following figure illustrates how to determine the value of the variable num. The background image must be wide enough to have part of the image hidden off the left side of the page and part that is visible in the browser window. The width of the part that is off-page is the value to use for num and in order to shift it to the left (off the page), it must be a negative value. The script scrolls the hidden part into view, and when the left edge of the image is reached, the scrolling direction is reversed.

Scrolling background operation

Background image selection is very important. The image must span the entire width of the browser with extra to allow it to scroll into view (as shown in the above figure). Because you do not know how wide your visitor’s browser will be, you must use an image larger than the width of today’s modern monitors. Unfortunately this could result in a very large image that may take a while to load in a browser. The example site uses a background JPG image that is 2456×668 pixels and is about 140K. A network monitor revealed it took 2.5 seconds to load in my browser.

SiteSpinner Object Background

As you know, you can add an image to a SiteSpinner object using the Shading Editor. You can use a modified form of the script to add a scrolling background to a rectangle shape, text, or code object. There are a few steps you must take to use a scrolling background with these objects.

  1. A rectangle shape must have its Re-Render and Anti-Aliasing options disabled (Object Editor→Options tab). If this is not done, SiteSpinner will convert the shape and any background to a single image, effectively placing an image on your page and not an object with a background. SiteSpinner must convert other shapes (ovals and polygons) to images, so these are not suitable for using with a scrolling background as images cannot have backgrounds.
  2. If using a rectangle shape, you must add the background image to the object as a Tile. Adding the background image using a Stretch or Scale type will cause SiteSpinner to render the shape as an image regardless of the Re-Render setting.
  3. You need to make sure the object is sized large enough to display your background image. When using a text object, this means you may need to make the object larger than the text. If this is necessary, you should lock the object so it is not automatically re-sized by SiteSpinner if you edit the text.

To use a scrolling background with a SiteSpinner object, you will need to use the following modified script. Place the code in a code object with the code placement set to “Header.”

<script type='text/javascript'>
var objSpeed = 75;
var objVer = 'center';
var objNum = objWid = -300;
var objDir = 0;
var obBg; 

function scrollObjBG() {
  if (objDir == 1) {
    if ((++objNum) >= 0) objDir = 0;
  }
  else if (objDir == 0) {
    if ((--objNum) <= objWid) objDir = 1;
  }
  obBg.style.backgroundPosition = objNum+'px '+objVer;
}
function initScrollObjBG(obj) {
  obBg = document.getElementById('O'+obj);
  setInterval('scrollObjBG()',objSpeed);
}
</script>
<body onload="initScrollObjBG('objXX');" >

Change the objXX in the last line to the actual object name of your object. In this modified script, the variables objSpeed, objNum, and objVer have the same use as the variables speed, num, and ver_pos in the original scrolling background script. This allows both scripts to be used on the same page if desired.

In the demo page, you can see both scripts being used at once.



7 Responses to “Scrolling Background”

    admin says:

    Using CSS, you can position a background image virtually anywhere on the page you want. The basic CSS rule to do this is:

    background-position: horizontal vertical;

    where horizontal and vertical are valid CSS positioning values. This script works by periodically shifting the horizontal value by a defined number of pixels while keeping the vertical value constant. So to shift the background image vertically, you only need to keep the horizontal value constant and change the vertical value. The script does this by dynamically altering the page’s background-position using the line:

    obBg.style.backgroundPosition = objNum+”px “+objVer;

    where objNum is the pixel value to shift horizontally and objVer is defined as a constant value of “center”. To shift the image vertically, you would need to modify these to values to

    obBg.style.backgroundPosition = “left “+objNum+”px”;

    so that the horizontal value is a constant “left” and the objNum value modifies the vertical position.

    Of course you would also need to modify the initial value of the variables num and wid to compensate for vertical movement and find a background image that would look good when vertically scrolled.

    Dan says:

    Fantastic script! How do you scroll an image downwards rather than horizontal?

    admin says:

    Ron,

    I would think that a horizontal scroll would be possible, however I can see some drawbacks. First, the background image would have to be limited to a section of the page. Having a full page horizontal scroll would require quite a large image and would chew up quite a few CPU cycles to scroll. Second, I don’t think I’ve ever seen a horizontal panoramic image.

    Fading the image cannot be done using this simple script. It would need to have some fading transitions added, possibly by using jQuery. Also, you need to keep in mind that every background image needs to be downloaded before it is displayed. Since the size of these background images are quite large to begin with, having more than one could significantly impact page loading time.

    If you are trying to use this as a simple image scroller, there are far better scripts to use than this one.

    Ron says:

    Hey Larry,
    Also was wondering would there be a way of have the photos change out? like as it scrolls from left to right that it would fade into another photo?

    Ron says:

    I just figured it out!  had to change in the geometry editor to render the semi-transparent rectangle as a PNG32 and it worked!

    Ron says:

    Also, regarding the site you mentioned above (Boathouse 106), how do they have the semi-transparent rectangle over the moving background where you can see the moving background behind the box? I tried duplicating it in SS but part of the background image is being 'captured' in the box. I tried turning re-render off but then I lose the transparency and it just becomes a solid box.

    Ron says:

    Hey Larry,
    Could this script be modified to have the pic move in a vertical direction (top to bottom and vice versa)?

Leave a Reply

Spam protection by WP Captcha-Free


February 5th, 2012