r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

How To: Scroll a Web-Page Background using JavaScript

r3dux | August 19, 2009

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 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 =/

Comments
2 Comments »
Categories
Coding
Tags
Background, Image, JavaScript, Scroll, Scrolling
Comments rss Comments rss
Trackback Trackback

Josh Joplin – Must Be You

r3dux |

I’ve had this song going through my head for days… Love it.

Josh Joplin Group - The Future That Was album cover

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

I think therefore, I think I am
I can’t make my mind up and I can’t make you understand
How I get distracted, how I search for things to say
How I let myself get myself into my way
I love to hear you talk, it’s funny but it’s true

CHORUS: What gets into me must be you
What gets into me must be you

I’m always late but it’s really not my fault
Time just hates me, that’s why it made me an adult
If wisdom comes with age then I haven’t got a clue

CHORUS

BRIDGE: Turn off your clock sleep in today
There isn’t a job that can not wait
I can not wait to be home with you

I might be walking and I might just laugh out loud
at something you said in the middle of a crowd
Let the people stare I can’t tell them what to do

CHORUS

Comments
1 Comment »
Categories
Music
Tags
Josh Joplin Group, Must Be You
Comments rss Comments rss
Trackback Trackback

Visualising Ten Dimensions

r3dux |

Food for thought, no?

Comments
No Comments »
Categories
Misc
Tags
Dimension, point, Portal, timeline
Comments rss Comments rss
Trackback Trackback

Trials HD

r3dux | August 12, 2009

YouTube Preview Image

Hmm, looks alright, in a Little Big Planet meets Kickstart kinda way…

Comments
No Comments »
Categories
Gaming
Tags
Kickstart, Little Big Planet, Motorbikes, Trials
Comments rss Comments rss
Trackback Trackback

Super Mario AI

r3dux | August 11, 2009

There’s an upcoming Super Mario AI Competition where the goal is to write code to control Mario through an infinite real-time-generated level. Robin Baumgarten happens to be good at AI programming… Very good, in fact.

YouTube Preview Image

That. Is. Awesome.

Comments
No Comments »
Categories
Coding, Gaming
Tags
AI, Heuristics, Mario, Super Mario
Comments rss Comments rss
Trackback Trackback

« Previous Entries Next Entries »

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash GLSL Gnome Hack How-To install Jaunty Java Kinect Linkage Linux Mash-Up Microsoft Motion OpenGL Particle Problem PS3 Remix Retro script Slides Sound Systems Texture Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“Better to be occasionally cheated than perpetually suspicious.”

 - B.C. Forbes

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox