How to Build a Super Duper News Scroller
This week, we'll learn how to combine PHP, SimplePie, and jQuery to build a simple news scroller widget for your website. It's much easier than you might think; so let's begin.
Note that I modified the code slightly after recording this screencast. Don't worry, they're just minor changes; but as with anything, you should continuously refactor your code.



Final NewsScroll Plugin
1 |
|
2 |
(function($) { |
3 |
|
4 |
$.fn.newsScroll = function(options) { |
5 |
|
6 |
return this.each(function() { |
7 |
|
8 |
var
|
9 |
$this = $(this), |
10 |
|
11 |
defaults = { |
12 |
speed: 400, |
13 |
delay: 3000, |
14 |
list_item_height: $this.children('li').outerHeight() |
15 |
},
|
16 |
|
17 |
settings = $.extend({}, defaults, options); |
18 |
|
19 |
setInterval(function() { |
20 |
$this.children('li:first') |
21 |
.animate({ |
22 |
marginTop : '-' + settings.list_item_height, |
23 |
opacity: 'hide' }, |
24 |
|
25 |
settings.speed, |
26 |
|
27 |
function() { |
28 |
$this
|
29 |
.children('li:first') |
30 |
.appendTo($this) |
31 |
.css('marginTop', 0) |
32 |
.fadeIn(300); |
33 |
}
|
34 |
); // end animate |
35 |
}, settings.delay); // end setInterval |
36 |
});
|
37 |
}
|
38 |
|
39 |
})(jQuery); |
With Commenting
1 |
|
2 |
// Create a self-invoking anonymous function. That way,
|
3 |
// we're free to use the jQuery dollar symbol anywhere within.
|
4 |
(function($) { |
5 |
|
6 |
// We name our plugin "newscroll". When creating our function,
|
7 |
// we'll allow the user to pass in a couple of parameters.
|
8 |
$.fn.newsScroll = function(options) { |
9 |
|
10 |
// For each item in the wrapped set, perform the following.
|
11 |
return this.each(function() { |
12 |
|
13 |
var
|
14 |
// Caches this - or the ul widget(s) that was passed in.
|
15 |
// Saves time and improves performance.
|
16 |
$this = $(this), |
17 |
|
18 |
// If the user doesn't pass in parameters, we'll use this object.
|
19 |
defaults = { |
20 |
speed: 400, // How quickly should the items scroll? |
21 |
delay: 3000, // How long a rest between transitions? |
22 |
list_item_height: $this.children('li').outerHeight() // How tall is each list item? If this parameter isn't passed in, jQuery will grab it. |
23 |
},
|
24 |
// Create a new object that merges the defaults and the
|
25 |
// user's "options". The latter takes precedence.
|
26 |
settings = $.extend({}, defaults, options); |
27 |
|
28 |
// This sets an interval that will be called continuously.
|
29 |
setInterval(function() { |
30 |
// Get the very first list item in the wrapped set.
|
31 |
$this.children('li:first') |
32 |
// Animate it
|
33 |
.animate({ |
34 |
marginTop : '-' + settings.list_item_height, // Shift this first item upwards. |
35 |
opacity: 'hide' }, // Fade the li out. |
36 |
|
37 |
// Over the course of however long is
|
38 |
// passed in. (settings.speed)
|
39 |
settings.speed, |
40 |
|
41 |
// When complete, run a callback function.
|
42 |
function() { |
43 |
|
44 |
// Get that first list item again.
|
45 |
$this.children('li:first') |
46 |
.appendTo($this) // Move it the very bottom of the ul. |
47 |
|
48 |
// Reset its margin top back to 0. Otherwise,
|
49 |
// it will still contain the negative value that we set earlier.
|
50 |
.css('marginTop', 0) |
51 |
.fadeIn(300); // Fade in back in. |
52 |
}
|
53 |
); // end animate |
54 |
}, settings.delay); // end setInterval |
55 |
});
|
56 |
}
|
57 |
|
58 |
})(jQuery); |
Final Page
1 |
|
2 |
<?php |
3 |
|
4 |
require 'simplepie.inc'; |
5 |
$feed = new SimplePie('https://code.tutsplus.com |
6 |
$feed->handle_content_type(); |
7 |
|
8 |
?> |
9 |
|
10 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
11 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
12 |
<head> |
13 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
14 |
<link rel="stylesheet" href="style.css" type="text/css" media="screen" charset="utf-8" /> |
15 |
<title>Super Duper News Scroller</title> |
16 |
</head> |
17 |
|
18 |
<body> |
19 |
|
20 |
<div id="container"> |
21 |
<h1>Super Duper News Scroller: <small>Built With PHP, SimplePie, and jQuery</small</h1> |
22 |
|
23 |
<ul id="widget"> |
24 |
<?php foreach($feed->get_items(0, 15) as $item) : ?> |
25 |
<li> |
26 |
<?php echo $item->get_description(); ?> |
27 |
<h4><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4> |
28 |
<p> |
29 |
<?php echo $item->get_date(); ?> |
30 |
</p> |
31 |
</li> |
32 |
<?php endforeach; ?> |
33 |
</ul> |
34 |
</div><!--end container--> |
35 |
|
36 |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> |
37 |
<script type="text/javascript" src="jquery.newsScroll.js"></script> |
38 |
|
39 |
<script type="text/javascript"> |
40 |
$('#widget').newsScroll({ |
41 |
speed: 2000, |
42 |
delay: 5000 |
43 |
}); |
44 |
|
45 |
// or just call it like: |
46 |
// $('#widget').newsScroll(); |
47 |
</script> |
48 |
|
49 |
</body> |
50 |
</html> |
That's It
In twenty minutes, we were able to build a nice and simple scroller. You're now free to take the plugin and expand it to your needs. What you have here should be considered the first step. How can you improve upon it?
You Also Might Like...
-
Extending SimplePie to Parse Unique RSS Feeds
A few days ago, as I prepared our Create a Slick Flickr Gallery with SimplePie tutorial, it occurred to me that we haven’t posted many articles that covered SimplePie. Considering how fantastic a library it is, I think it’s time to take a closer look.
-
You Still Can't Create a jQuery Plugin?
It’s tough. You read tutorial after tutorial, but they all assume that you know more than you actually do. By the time you’re finished, you’re left feeling more confused than you initially were. Why did he create an empty object? What does it mean when you pass “options” as a parameter? What do “defaultsettings” actually do?
Never fear; I’m going to show you exactly how to build your own “tooltip” plugin, at the request of one of our loyal readers.
-
jQuery for Absolute Beginners
Hi everyone! Today, I posted the final screencast in my “jQuery for Absolute Beginners” series on the ThemeForest Blog. If you’re unfamiliar - over the course of about a month, I posted fifteen video tutorials that teach you EXACTLY how to use the jQuery library. We start by downloading the library and eventually work our way up to creating an AJAX style-switcher.
-
Diving into PHP: Video Series
Today marks the beginning of a brand new series on the ThemeForest blog that will show you EXACTLY how to get started with PHP. Just as with the “jQuery for Absolute Beginners” screencasts, we’ll start from scratch and slowly work our way up to some more advanced topics. If you’ve been hoping to learn this language, be sure to pay a visit and subscribe to the RSS feed to be updated when new videos are posted.