25th July 2008

The world's only free and accessible RSS news ticker (PHP and JavaScript)

I'm of the opinion that RSS/news tickers are a uniformly bad idea; they're typically not accessible, not attractive and awfully 90's. However, there are some instances where they're acceptable (and clients tend to like them), but where do you get them from?

I had a look around online for nice free examples that met most of my criteria but no... People were either asking me to pay for them or they were just awful, like DHTML awful - straight out of the 90's. So I set about a really simple exercise of providing the world with a really simple, accessible RSS ticker.

rss ticker

What you need

I'm not even bothering with SimplePie for this, just PHP5's marvelous SimpleXML so you just need a PHP5 enabled server (and an RSS feed to use, in this example I'm using my feed from Seopher.com).

What I'm going to do

I'm going to create a wrapper div with an overflow of hidden and a fixed width, within it I'm going to have an unordered list that contains the titles for each of the items in the RSS feed. I'll display these next to one another and using Javascript I'll animate them to move from one side to another. Make sense? If it doesn't all will become clear shortly.

PHP

All I need to do here is connect to my RSS file, iterate through the titles and create an unordered list with the linked recent articles. As such:

That's pretty self explanatory I think; grab the feed, iterate through each of the items in the channel and write out a list-item (properly marked up with a title tag too). Easy!

XHTML

The XHTML for this is fairly minimal; just a wrapper called 'ticker', a div within that called 'ticker-inner' (which I'll use to set the massive width required for this task). Inside that I'm simply echo'ing out the $html variable I created in PHP at the top of the document. Still very straight forward at this point...

The purpose of the 'noscript' class will become apparent in the CSS section, while 'toggleTicker' will become apparent in the JavaScript bit.

CSS

This is slightly more complicated... What I'm doing is setting a fixed width on the ticker container with an overflow of hidden - this is the box you see that contains the ticker itself. Ticker-inner has a massive width set (far too large) to accomodate quite a few RSS items. I'm also offsetting ticker-inner off to the left of the ticker-container too, allowing items to scroll past the viewable window. There's quite a lot of CSS here so see the comments in the code below for more info:

Okay so we've got the UL positioned absolutely just out of view to the right. Note the use of the 'noscript' class; this allows me to set styles specifically for when JavaScript is disabled. How? Well it's simple. Apply a class of 'noscript' to the container and remove it with JavaScript onDomReady - meaning if JS is enabled then users see it how you intended, otherwise the 'noscript' class remains on the page and it renders nicely. Simple!

JavaScript

Okay so the first thing I'm doing is removing the 'noscript' class (for reasons described above). Again because there's quite a lot of code I've annotated it in the box below:

The eventlistener on 'toggleTicker' is important for accessibility reasons; if you have moving content on the screen you should offer a control to stop the movement. Some visual disabilities mean users can become overly distracted by moving content - making it very hard for them to see anything else but what's moving. Therefore if you have a link on the page that can stop the animation like this does, you're covered.

It even works out how many items are in the list and alters the duration accordingly; meaning you get a nice consistent speed. Only mega (and I mean MEGA) long titles will flick past at an annoyingly quick rate. You could probably work out the duration based on the width of the UL come to think of it and give it a certain number of seconds per 100px. Either way, this isn't too bad as it is.

Accessibility and Degrading

This is the only accessible version I've seen. If you turn off JavaScript you're just presented with a normal unordered list (because I'm using JavaScript to remove the 'noscript' class) so those without this technology enabled can still access all the content. The markup is clean and the user can interupt the animation easily enough. Therefore this is as accessible as you can get a news/RSS ticker.

Conclusion

There you go. It's really simple; the animation isn't difficult and the output isn't anything amazing. It is however one of the only free, accessible RSS tickers available on the Internet. If you've seen a better one let me know and I'll improve mine. Like I said before, this is uber simple yet effective. It's free to use so go nuts.

Tagged with widget, javascript, php, accessibility, css, xhtml