Creating a cross browser scroller using DHTML

There's something about text moving across the screen that fascinates people. Since it was first possible to create moving text in a webpage, by 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? For example, the below scroller 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+. 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).

<ilayer 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:

scroller.gif (3993 bytes)

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)

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

//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 language="JavaScript1.2">

<!--Script by Billy Pete ( -->
<!--Idea based on scroller found at -->

//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){

function scrollit(){
if (document.cmarquee01.document.cmarquee02.left>=thelength*(-1)){


<ilayer width=300 height=20 name="cmarquee01">
<layer name="cmarquee02"></layer>

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 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.


  • Very good!

    Posted by Legacy on 01/03/2000 12:00am

    Originally posted by: John Zhang

    Very good work. It is small, clean, and includes everything I (everyone?) need to know.


  • MFC?

    Posted by Legacy on 12/18/1999 12:00am

    Originally posted by: Christian Skovdal Andersen

    Nice article, but exactly how is it related to MFC?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date