![]() |
Welcome to Tom's Web |
First Things First This Was written by Billy Peterson .
Creating a cross browser scroller using
DHTML
-This tutorial is written and contributed by Billy Peterson Please see footnote for more information.
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 For Internet Exploere Don't work in Firefox !!!:
Whether you're using IE4 or NS4, the a scroller works is.
Two completely different concepts are involved.
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:
In IE4, however, 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, many other people, use NS4 to browse the web, so whatever you have planned for IE4 users, they would appreciate seeing it also.
Let's move
on to see how to make a scroller work in NS4.
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.
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 make that into a scroller!
First we define a <layer>
tag and put the text to scroll inside of it.
Then we put all of that in a <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 script to increment the left position of this layer, it moves, like in a scroller.
The <ilayer> tag defines the "scroller window", the viewable area of the scroller. The <layer> tag, on the other hand, defines/contains the scrolling text itself. We want to create a script that will move The text continuously to the left until it reaches the end, 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 The 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 whole scrolling text code
Putting it together, along with some added code, here is the entire script that renders the scroller you saw in the beginning of this article.
<script
language="JavaScript1.2">
<!--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)
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:
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 wORKS IN FOREFOX AND INTERNET EXPLORER !!:
|
BELOW IS A MARQUE THAT WORKD IN INTERNET EXPLOERE BUT NOT IN FIRE FOX !! ;
-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.
To see His article in its entirety Check out this
On the next few pages i will include some marquees that will work in both IE4 & NE4.