There’s something about text moving across the screen that fascinates people. Since it was first possible to create moving text in a webpage using JavaScript, countless so-called scroller scripts have been created. Have you noticed, however, that most of them are limited to scrolling the text either inside a textbox, or status bar? Click here to see a scroller that scrolls text inside a textbox.
Not that there’s anything wrong with textboxes or the status bar, but personally, I like my text scrolled all by itself, not wrapped inside any other element. In other words, a “true” text scroller. Is it possible? Apparently, yes. After a couple of weeks of searching, I finally found some working scrolling text examples on the web, and in the process, learned for myself how to create them. In this article, I wish to share that knowledge with you.
I’m going to teach you how to create a basic, side-scrolling scroller using the DHTML features of IE4+ and NS4+. Click here for a demo.
Whether you’re using IE4 or NS4, the scroller works equally well. Two completely different concepts are involved in the realization of it. Let’s see for ourselves how to create a text scroller!
Scrolling the text in IE4
In IE4, it’s actually very simple to scroll any text, thanks to a default tag supported since IE3: the <marquee>
tag. Just wrap any text you want to scroll inside it, and off it goes:
<marquee>This is scrolling text</marquee>
In IE4, however, it seems you can now also put in HTML tags, and they will interpreted as such:
<marquee><big>This is a BIG scrolling text</big></marquee>
So that’s that for IE4. If you’re only creating a text scroller for IE4, you already know all there is to know. However, like many other people, I use NS4 to browse the web, so whatever you have planned for IE4 users, I would appreciate seeing it as well. Let’s move on to see how to make the scroller work in NS4 equally, which is a little more complex.
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:
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.
The entire scrolling text code
Putting the bits and pieces together, along with some added code, here is the entire script that renders the scroller you saw in the beginning of this article. I’ll list it first, then explain any parts of it that might need clarification:
<script language="JavaScript1.2">
/* Script by Billy Pete (https://members.xoom.com/billypete/) */
/* Idea based on scroller found at https://dynamicdrive.com */
/* Specify the marquee's scroll speed (larger is faster) */
var speed=6;
/* Specify the marquee contents */
var marqueecontents = '<font face="Arial"><strong>This is is scrolling text script. This is a scrolling text script. This is a scrolling text script.</strong></font>';
if (document.all)
document.write( '<marquee scrollAmount=' + speed + ' style="width:300">' + marqueecontents + '</marquee>' );
function intializemarquee(){
if (document.layers){
document.cmarquee01.document.cmarquee02.document.write( '<nobr>' + marqueecontents + '</nobr>' );
document.cmarquee01.document.cmarquee02.document.close();
thelength = document.cmarquee01.document.cmarquee02.document.width;
scrollit();
}
}
function scrollit(){
if (document.cmarquee01.document.cmarquee02.left >= thelength*(-1)){
document.cmarquee01.document.cmarquee02.left -= speed;
setTimeout( "scrollit()", 100 );
}
else{
document.cmarquee01.document.cmarquee02.left = 300;
scrollit();
}
}
window.onload=intializemarquee;
</script>
<ilayer width=300 height=20 name="cmarquee01">
<layer name="cmarquee02"></layer>
</ilayer>
I use document.write()
to dynamically write out the <marquee>
tag for IE (instead of simply directly embedding it on the page). This is to avoid future potential problems with NS when and if NS eventually does support the <marquee>
; the code was written exclusively for IE in this respect! Function initializemarquee()
is what’s used to fill the scroller with the desired text for NS. It first accesses the <ilayer>
, then <layer>
tag, and finally, it’s document.write()
method to accomplish this.
So there you have it! A cool, cross browser scroller you can use on your webpage. Finally, I leave you with a few additional examples on the web regarding DHTML scrollers:
- Dynamic Drive DHTML scrollers
- DevEdge Ticker object — Requires NS 4
Take a peek into their source code… that’s how you learn!