This example demonstrates how to implement a scrolling banner, similar to the MARQUEE element in Internet Explorer, that uses methods and properties contained in the DOM 1 and 2 recommendations of the W3C. It also works in Internet Explorer 5.0 without invoking the proprietary MARQUEE element.
As you examine the Script code, the following points should be kept in mind. At the end of each item there is a color bar which mark the relevant points in the Script code listing. The item with this coloring pertains only to the previous version of this script coding.
Show Script Code Show Previous Script Code
<SCRIPT LANGUAGE="Javascript">
<!--
var msgWidth = 570, bannerWidth;
var timerID;
/* This function centers the banner window in the main browser window */
function centerBanner() {
if (bannerWidth == null) setBannerWidth();
var el = document.getElementById('canvas');
var scrnWidth = (document.all ? document.body.clientWidth : window.innerWidth);
el.style.left = parseInt(0.5*(scrnWidth - bannerWidth)) + "px";
el.style.visibility = 'visible';
}
/* Gets the banner window width from the select box value */
function setBannerWidth() {
var selEL = document.getElementById('banWidth');
bannerWidth = parseInt(selEL.options[selEL.selectedIndex].text);
document.getElementById('canvas').style.width = bannerWidth + "px";
document.getElementById('canvasText').style.left = bannerWidth + "px";
centerBanner();
}
/* Function to start and stop scrolling */
function scroll(type) {
if (type == 'start') {
scrollIt(bannerWidth, 10, 250);
} else
if (timerID != null) clearTimeout(timerID);
}
/* Function to reset the text layer for next scroll */
function clearText() {
scroll('stop');
var el = document.getElementById('canvasText');
/* Reposition the scrolling text */
el.style.left = bannerWidth + "px";
}
/* Function which toggles the display of the script code.
* Rather than going through the textNode creation shown here
* the node value can be directly set to accomplish the
* same thing */
function showCode() {
var toggleEL = document.getElementById('toggleCode');
var scrpEL = document.getElementById('scriptContainer');
if (toggleEL.childNodes[0].nodeValue.indexOf('Show') != -1) {
var eTEXT = document.createTextNode("Hide Script Code");
toggleEL.replaceChild(eTEXT, toggleEL.childNodes[0]);
scrpEL.className = 'scrShow';
} else {
var eTEXT = document.createTextNode("Show Script Code");
toggleEL.replaceChild(eTEXT, toggleEL.childNodes[0]);
scrpEL.className = 'scrHidden';
}
return false;
}
/* Function that does the actually scrolling */
function scrollIt(Left, dx, speed) {
var text = document.getElementById('canvasText');
Left -= dx;
text.style.left = Left + "px";
if (Left > -msgWidth)
timerID = setTimeout("scrollIt(" + Left + ", " + dx + ", " + speed + ")", speed);
}
//-->
</SCRIPT><SCRIPT LANGUAGE="Javascript"> <!-- var first, msgWidth = 570, bannerWidth; var timerID, leftSide; /* This function centers the banner window in the main browser window */ function centerBanner() { if (bannerWidth == null) setBannerWidth(); var el = document.getElementById('canvas'); var scrnWidth = (document.all ? document.body.clientWidth : window.innerWidth); el.style.left = parseInt(0.5*(scrnWidth - bannerWidth)) + "px"; el.style.visibility = 'visible'; } /* Gets the banner window width from the select box value */ function setBannerWidth() { var selEL = document.getElementById('banWidth'); bannerWidth = parseInt(selEL.options[selEL.selectedIndex].text); document.getElementById('canvas').style.width = bannerWidth + "px"; document.getElementById('canvasText').style.left = bannerWidth + "px"; centerBanner(); } /* Function to start and stop scrolling */ function scroll(type) { if (type == 'start') { first = true; // set some initial variables /* msgWidth is not really needed for IE other than its value needs * to be large enough so that the string text contained in * 'canvasText' occupies a single line without wrapping */ leftSide = msgWidth; scrollIt(bannerWidth, 10, 250); } else if (timerID != null) clearTimeout(timerID); } /* Function to reset the text layer for next scroll */ function clearText() { scroll('stop'); var el = document.getElementById('canvasText'); /* The next statement not needed for IE */ el.style.clip = "rect(0px, " + msgWidth + "px, 0px, 0px)"; el.style.left = bannerWidth + "px"; } /* Function which toggles the display of the script code. * Rather than going through the textNode creation shown here * the node value can be directly set to accomplish the * same thing */ function showCode() { var toggleEL = document.getElementById('toggleCode'); var scrpEL = document.getElementById('scriptContainer'); if (toggleEL.childNodes[0].nodeValue.indexOf('Show') != -1) { var eTEXT = document.createTextNode("Hide Script Code"); toggleEL.replaceChild(eTEXT, toggleEL.childNodes[0]); scrpEL.className = 'scrShow'; } else { var eTEXT = document.createTextNode("Show Script Code"); toggleEL.replaceChild(eTEXT, toggleEL.childNodes[0]); scrpEL.className = 'scrHidden'; } return false; } /* Function that does the actually scrolling */ function scrollIt(Left, dx, speed) { var rightSide; var text = document.getElementById('canvasText'); Left -= dx; if (document.all) { // This branch is for the IE version if (first) { text.style.top = -1 + "px"; msgWidth = 570; text.style.width = msgWidth + "px"; first = false; } text.style.left = Left + "px"; } else { // This branch is for the version in Gecko if (Left < 0) { rightSide = -Left; leftSide = (leftSide > 0 ? leftSide - dx : 0); text.style.clip = 'rect(0px, '+ leftSide + 'px, 0px, ' + rightSide + 'px)'; text.style.left = Left + "px"; } else { if (first) text.style.visibility = 'hidden'; leftSide -= dx; text.style.clip = 'rect(0px, ' + leftSide + 'px, 0px, 0px)'; text.style.left = Left + "px"; if (first) { text.style.top = -0.5 + "px"; // this statement not needed in M12 text.style.visibility = 'visible'; first = false; } } } if (Left > -msgWidth) timerID = setTimeout("scrollIt(" + Left + ", " + dx + ", " + speed + ")", speed); } //--> </SCRIPT>