Article

Creating a Cross-Browser Scroller using DHTML

Page: 1 2 3 Next

Scrolling the text in NS4

To scroll text in NS4, everything -- including the interface -- has to be created from scratch. That's because no default tag or feature exists in NS4 to simulate this action.

What does exist in NS4, though, is the <layer> tag (the DHTML tag of the browser). This tag allows you to move whatever is inside of it freely around the page, and by applying some control, we can mold that into a scroller!

Here's the basic idea. We define a <layer> tag and put the text to scroll inside of it. We then wrap all of that with the <ilayer> tag, which simply grounds it to appear inline with the rest of the page (as opposed to the coordinates defined by the layer's left and top position).

<ilayer name="scroll1" width=300 height=20>
<layer name="scroll2">This is scrolling text. This is scrolling text. This is scrolling text...</layer>
</ilayer>

Then, by using a simple script to increment the left position of this layer, it moves, just like in a scroller. Before I show you the script itself, allow me to illustrate what I've just talked about graphically:

DHTML Scroller Schematic

The <ilayer> tag defines the "scroller window", the physical viewable area of the scroller (green rectangle). The <layer> tag, on the other hand, defines/contains the scrolling text itself, and is represented above as the white rectangle. We want to create a script that will move this white rectangle continuously to the left until it reaches the end of the text, then start over again.

Here's the function that does that:

function scrollit(){
/* get the total length of the scroller (white rectangle) */
scrollerlength = document.scroll1.document.scroll2.document.width;
/* if the scroller's left position is greater than -scrollerlength (hasn't reached the end) */
if (document.scroll1.document.scroll2.left >= scrollerlength*(-1)){
/* decrease it's left position by 6 pixels */
document.scroll1.document.scroll2.left -= 6;
setTimeout("scrollit()",100);
}
else{
/* else if the scroller has reached the end, reset the scroller position */
document.scroll1.document.scroll2.left=300;
/* and start things all over */
scrollit();
}
}

Read my comments inside to see how it works. Basically, the idea is to decrease the "left" value of the layer continuously, until it reaches the end of the layer. Then, repeat and start all over again from it's original position.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links