this script is pretty straight forward. some people may have uses for it, others may not. i created this for a specific request and i’m still making improvements to it.
so here it is. a news scroll for your site! w00t! this is straight up JavaScript, so no plug-in’s or anything like that needed. works in IE 5+ and FF 1+. it’s very simple to insert into your site. view the demo HERE. you can modify the look and feel all you want. more <3 for CSS. let us get started shall we?
here be the JavaScript!!!
//newsticker.js
TICKER_CONTENT = document.getElementById("TICKER").innerHTML;
TICKER_RIGHTTOLEFT = false;
TICKER_SPEED = 2;
TICKER_STYLE = "font-family:Arial; font-size:12px; color:#FFFFFF";
TICKER_PAUSED = false;
ticker_start();
function ticker_start() {
var tickerSupported = false;
TICKER_WIDTH = document.getElementById("TICKER").style.width;
var img = "
";
// Firefox
if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1) {
document.getElementById("TICKER").innerHTML = "
";
tickerSupported = true;
}
// IE
if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
document.getElementById("TICKER").innerHTML = "
"+img+""+img+"
";
tickerSupported = true;
}
if(!tickerSupported) document.getElementById("TICKER").outerHTML = ""; else {
document.getElementById("TICKER").scrollLeft = TICKER_RIGHTTOLEFT ? document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth : 0;
document.getElementById("TICKER_BODY").innerHTML = TICKER_CONTENT;
document.getElementById("TICKER").style.display="block";
TICKER_tick();
}
}
function TICKER_tick() {
if(!TICKER_PAUSED) document.getElementById("TICKER").scrollLeft += TICKER_SPEED * (TICKER_RIGHTTOLEFT ? -1 : 1);
if(TICKER_RIGHTTOLEFT && document.getElementById("TICKER").scrollLeft < = 0) document.getElementById("TICKER").scrollLeft = document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth;
if(!TICKER_RIGHTTOLEFT && document.getElementById("TICKER").scrollLeft >= document.getElementById("TICKER").scrollWidth - document.getElementById("TICKER").offsetWidth) document.getElementById("TICKER").scrollLeft = 0;
window.setTimeout("TICKER_tick()", 30);
}
to call the ticker to your page, the .JS DOES NOT go in the HEAD section of your document. it trails right below the actual content in the body.
EXAMPLE:
<div id="ticker" style="overflow:hidden; width:520px" onmouseover="TICKER_PAUSED=true" onmouseout="TICKER_PAUSED=false">
This is news. As is this. And This. This is what will display in the actual ticker. And this as well. DO NOT USE <br /> tags as it will cause all kinds of crazy, unless that's what you're looking for. Feel free to add any HTML markup to seperate your "news" or what not.
</div>
<script type="text/javascript" src="newsticker.js" language="javascript"></script>
make sure that the JS is being called correctly (absolute path would be nice). in this example, i have it set to pause when someone mouses over a headline, and un-pause on mouseout. now let us edit some variable in the actual .JS to make it more “yours”.
TICKER_RIGHTTOLEFT
pretty self-explanatory. do you want it scrolling left to right or right to left?
TICKER_SPEED
set the speed of the scroll. 1 = slowest, 10 = fastest
TICKER_STYLE
set your styles here. font-family, color, all the fun stuff.
TICKER_PAUSED
when this variable is set to true, the scrolling ticker is paused. this value can be changed at runtime, for example when the mouse is over the ticker.
what i really use this for is to announce site news, via an RSS or XML feed. this is really easy to set-up.
instead of hard-coded text, we’re going to use a php include (you can use whatever suits your purpose). so in-between the DIV tags i put this:
< ? include "newsfeed.php" ?> //you can call this whatever you want
that file is just a php file that parses the data from an RSS or XML feed and converts it into plain HTML, thus letting the ticker read the new info. i find this to work the best, but that’s just me.
i’m not going into how to do this at this time, if you’re really lost, feel free to comment. the best solution would be using something like MagpieRSS to help you with your parsing needs.
and with that, i’m going to go cry in my beer now.
until next time….