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+. Here's a demo:
Whether you're using IE4 or NS4, the scroller works equally. Two completely different concepts is 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, I, like many other people, 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).
name="scroll1" width=300 height=20>
<layer name="scroll2">This is scrolling text. This is scrolling text. This is scrolling text...</layer>
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:
//get the total length of the scroller (white rectangle)
//if the scroller's left position is greater than -scrollerlength (hasn't reached the end)
//decrease it's left position by 6 pixels
//else if the scroller has reached the end, reset the scroller position
//and start things all over
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 by Billy Pete (http://members.xoom.com/billypete/) -->
<!--Idea based on scroller found at http://dynamicdrive.com -->
//Specify the marquee's scroll speed (larger is faster)
//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>'
document.write('<marquee scrollAmount='+speed+' style="width:300">'+marqueecontents+'</marquee>')
<ilayer width=300 height=20 name="cmarquee01">
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:
Take a peek into their source codes...that's how you learn!
-Have a question on anything in this article? Email me! This is an open-source article, so you may include it on your site for the benefit of others, assuming you email me first.