How to Create a Dynamic HTML Navigation Page - Subroutines
(Page 3 of 6 )
Surprisingly, you need to write a total of thirteen subroutines to make it happen: eight onclick events (two for each directional button), plus five additional sub's to control the image movement. There are four global variables that are used throughout the script to control the image animation. They need to be global because each of them is accessed by two separate subroutines, so they need to be declared at the start of the script:
dim leftButtonDown
dim upbuttonDown
dim rightButtonDown
dim downButtonDown
dim animator
The first four are switches that are set to either 1 or 0 and are 'switched' by various mouse events. The reason you need four of them is that the script seems to get a little confused if you use just one, and the movement of the central image is a little irrational. Try using just one switch and see what happens.
Next you need to script the onclick events, two for each of the directional control buttons:
sub buttonLeft_onMouseDown
animator=window.setInterval("flyLeft", 10)
leftButtonDown=1
End sub
sub buttonLeft_onMouseUp
window.clearInterval(animator)
leftButtonDown=0
end sub
The mouseDown event initiates the setInterval method, which take a subroutine name and an integer as its argument, and assigns this to a variable, in this case 'animator'. The setInterval method is used to repeat a subroutine for a specified number of milliseconds, so what we're telling the script to do is execute the flyLeft function repeatedly for 10 milliseconds. It is this that produces the smooth animated movement of the central image, as you'll see when the file is finished. The subroutine also sets the leftButtonDown switch to on. We could simply call the subroutine that moves the central image instead of using the animator, but this way, users of your site would need to repeatedly click the directional button to move the image.
The mouseUp event uses the clearInterval method to stop the animation and takes simply the variable name assigned to setInterval as an argument. It also switches leftButtonDown back off. Without the mouseUp subroutine, the central image you continue to move after the user had stopped clicking.
In using these two subroutines you'll find that the images scrolls across the screen while the directional button is down, and stops as soon as the button is released. After writing the remaining OnClick subroutines, you should end up with a block of code like this:
sub buttonLeft_onMouseDown
animator=window.setInterval("flyLeft", 10)
leftButtonDown=1
End sub
sub buttonLeft_onMouseUp
window.clearInterval(animator)
leftButtonDown=0
end sub
sub buttonUp_onMouseDown
animator=window.setInterval("flyUp", 10)
upButtonDown=1
End sub
sub buttonUp_onMouseUp
window.clearInterval(animator)
upButtonDown=0
end sub
sub buttonRight_onMouseDown
animator=window.setInterval("flyRight", 10)
rightButtonDown=1
End sub
sub buttonRight_onMouseUp
window.clearInterval(animator)
rightButtonDown=0
end sub
sub buttonDown_onMouseDown
animator=window.setInterval("flyDown", 10)
downButtonDown=1
End sub
sub buttonDown_onMouseUp
window.clearInterval(animator)
downButtonDown=0
end sub
Next: The Remaining Subroutines >>
More HTML Articles
More By Dan Wellman