How To: Scroll a Web-Page Background using JavaScript

Update: As per request, modified to move the background in any direction and at any speed you’d like – all you have to do is change the vertSpeed, horizSpeed, and updateDelay variables to values of your choice! Oh, and I fixed up the jitter-on-wrap-around properly to use a better way of constraining the offsets which caters for all directions without letting the offsets hit any numerical limits.

I’m teaching a course titled Produce and Manipulate Digital Images at the moment, which along with the photography and photoshop type elements you’d expect, has to include an assignment for the students to incorporate digital photography into a multimedia sequence. I can get them to do the sequence in anything I deem appropriate: Flash, JavaScript, OpenGL if I wanna.. But considering the course after this (for this particular class) is on Multimedia Web Design, I thought maybe it might be an idea to do it in JavaScript, so I had a look around for JavaScript image manipulation and found this background image scroller (link now dead unfortunately).

Only it was a bit knackered and jumped when the (hard-coded, no less) background image height was reached. So I fixed it:

You can see it in action here. WoW! LoL! =P

Yeah… Gonna go with Flash CS4, methinks. But still, bit ‘o fun & all that…

P.S. If the background image doesn’t scroll, try re-loading the page… Not sure why but sometimes it just won’t go without having to do that… Very odd =/

7 thoughts on “How To: Scroll a Web-Page Background using JavaScript”

    1. Hi Michelle,

      I’ve modified the above code so that you can enter values for the horizontal and vertical speeds and change the update delay to whatever values you wish.

      One thing to note is that putting lower movement values and updating more often will increase the smoothness of the scrolling, but at the expense of higher CPU usage (because the update function must be called more often).

      For example:
      – Moving with a horizSpeed of 10 and a delay of 100ms will move the image 10 * (1000 / 100) = 100 pixels in one second, by making 10 calls to the scrollBackground function [Very choppy! =( ]
      – Moving with a horizSpeed of 1 and a delay of 10ms will move the image 1 * (1000 / 10) = 100 pixels in one second, by making 100 calls to the scrollBackground function [Very high CPU! =( ]

      Both of these examples are extremes, and it’s best to find a middle ground – a good value for updateDelay is to leave it at 50ms, so that’s 20 frames per second – if you need smoother animation maybe try 25ms (40 frames per second), and then adjust the horiz/vert speeds to move the background at a speed you like. If you’re moving it very slowly, then you could try increasing the updateDelay and see how it looks – either way you’re looking to find the best compromise you can between smoothness of animation and CPU usage.

      Best wishes,
      r3dux

    1. Hi Tonya,

      For the image to scroll upwards only just change the horizSpeed value (line 66) to 0 and vertSpeed value (line 67) to a negative value.

      Regards,
      r3dux

  1. Hey, script is working awesomely for me! Is there anyway though for me to set background to only repeat on the x and not y?

  2. THANK YOU! I have tried 2 or three different scripts to do that (in addition to trying to write my own javascript) and this is the one that worked for me the very first time. Thank you. Thank you.

Leave a Reply

Your email address will not be published.