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:

JavaScript

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,Nov2010-->

<title>JavaScriptBackgroundScroller</title>

<script type="text/javascript">

<!--Hide script from browsers notplaying nicew/JavaScript

// Function to scroll out background image

functionscrollBackground(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 documentbody anduseour background image-->

<body background="./AU-Flag.gif">

<script type="text/javascript">

<!--Hide script from browsers notplaying nicew/JavaScript

// Movement controlling variables:

// horizSpeed - positive values move the background to the right, negative to the left

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.