How To: Scroll a Web-Page Background using JavaScript
r3dux | August 19, 2009Update: 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 saw this background scroller.
Only it was a bit knackered and jumped when the (hard-coded, no less) background image height was reached. So I fixed it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <!-- Original by Roy Sinclair. Update by r3dux, Nov 2010 --> <title>JavaScript Background Scroller</title> <script type="text/javascript"> <!-- Hide script from browsers not playing nice w/ JavaScript // Function to scroll out background image function scrollBackground(imageWidth, imageHeight) { // Set our background offset. First field is horizontal offset, 2nd is vertical bgObject.style.backgroundPosition = horizOffset + " " + vertOffset; // Move image offset by given speeds horizOffset += horizSpeed; vertOffset += vertSpeed; // Reset our offsets when we've gone over (under if moving left or up) the size of the background image if (Math.abs(horizOffset) > imageWidth) { if (horizOffset > 0) { horizOffset -= imageWidth; } else { horizOffset += imageWidth; } } if (Math.abs(vertOffset) > imageHeight) { if (horizOffset > 0) { vertOffset -= imageHeight; } else { vertOffset += imageHeight; } } } // End of scrollBackground function // End of JavaScript--> </script> </head> <!-- Kick off the document body and use our background image --> <body background="./AU-Flag.gif"> <script type="text/javascript"> <!-- Hide script from browsers not playing nice w/ JavaScript // Movement controlling variables: // horizSpeed - positive values move the background to the right, negative to the left // vertSpeed - positive values move the background downwards, negative values move it upwards // (Feel free to use fractional values, too i.e. 0.3 etc.) // updateDelay - how many milliseconds to wait before updating the screen with the new background position // (1000 ms in 1 second, lower values will move the background more often so it will appear to move faster, at the cost of increased CPU usage) var horizSpeed = 1; var vertSpeed = 1.5; var updateDelay = 50; // Grab our background image var imgFile = new Image(); imgFile.src = "./AU-Flag.gif" // Get the height and width of the image var imgHeight = imgFile.height; var imgWidth = imgFile.width; // Set our initial background offset var horizOffset = 0; var vertOffset = 0; // Get our background var as the document body var bgObject = eval('document.body'); // Kick off the scrolling. The second field is delay between updates in milliseconds. var ScrollTimer = window.setInterval("scrollBackground(" + imgWidth + "," + imgHeight + ")", updateDelay); // End of JavaScript--> </script> </body> </html> |
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 =/












