demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

A Simple JQuery News Ticker With CSS3 & Generated Content

  • Improve pagination
  • Work on comment system
  • Add favorites
  • Allow users to add a profile
  • Redesign blog theme

Problem: you have too many items in a group – Twitter/Facebook messages, news headlines, spotlighted articles, etc. Including all of the items on a page would overwhelm the design, but you don’t wish to limit the items to a selection of just two or three.

Solution: limit the visible area of the group via CSS, and change the order of the items via JavaScript over time.

First, the basic HTML:

  1. <h3>To-Do List For demosthenes.info</h3>
  2. <ul id=to-do-list">
  3. <li>Improve pagination <a href="to-do.html#pagination"></a></li>
  4. <li>Work on comment system <a href="to-do.html#comments"></a></li>
  5. <li>Add favorites <a href="to-do.html#favorites"></a></li>
  6. <li>Allow users to add a profile <a href="to-do.html#profiles"></a></li>
  7. <li>Redesign blog theme <a href="to-do.html#redesign"></a></li>
  8. </ul>

We would normally never place height on an element (the body tag, images and container div being exceptions) but in this case we want to show a limited number of items, and hide the rest:

  1. ul#to-do-list {
  2. border: 2px solid #000; padding: 1em;
  3. height: 3em; overflow: hidden; width: 16em;
  4. }

Note that you could “even out” the perception of padding around the three visible list items by using a CSS3 count selector to influence the third list item:

  1. ul#to-do-list li:nth-child(3) { margin-bottom: 1.2em; }

Normally we want to direct the user to read more information on each news item on another page. If I wanted those words to accompany each link, I could use some generated content:

  1. ul#to-do-list li a:before { content: "read more"; }

Now we need to remove the first list item using JavaScript. We’ll load in JQuery, then add an embedded script that does just that (HTML5 formatting is used on tags for brevity)

  1. <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js>
  2. </script>
  3. <script>
  4. $(document).ready(function(){
  5. function removeFirst(){
  6. first = $('ul#to-do-list li:first').html();
  7. $('ul#to-do-list li:first')
  8. .fadeOut('slow', function()
  9. {$(this).remove();});
  10. addLast(first);
  11. }
  12. interval = setInterval(removeFirst, 3000);
  13. });
  14. </script>

The first variable will be used to contain the content of the first list item, which will then be faded out. This will occur every 3 seconds (3000 milliseconds).

If you ran the code as it exists right now, you would find that the list was gradually consumed from the top until it disappears; you would also find that the JavaScript console window in your browser supplied a repeated error message that it could not find an addLast function. The role of that function is to take the content that we removed from the top (first) and append it to the bottom of the list. Let’s add that function above removeFirst():

  1. <script>
  2. $(document).ready(function(){
  3. function addLast(first) {
  4. last = '<li>'+first+'</li>';
  5. $('ul#to-do-list ').append(last);
  6. }
  7. function removeFirst(){
  8. first = $('ul#to-do-list li:first').html();
  9. $('ul#to-do-list li:first')
  10. .fadeOut('slow', function()
  11. {$(this).remove();});
  12. addLast(first);
  13. }
  14. interval = setInterval(removeFirst, 3000);
  15. });
  16. </script>

The addLast() function takes the content of the first list item as a variable called first, concatenates <li> tags around it, and adds the result to the bottom of the list. In this way, the animation of the list is an infinite loop.

You could easily make the list items appear side-by-side, replace the text in the list with images, move the list elsewhere on your page, etc: that is all CSS and markup, not JavaScript.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.