CSS-Tricks

Creating a Slick Auto-Playing Featured Content Slider

*   August 18th, 2008   *

by: Chris Coyier

I love the Coda Slider plugin for jQuery. I’ve used it recently to build a couple of tabbed “widgets”. One here on CSS-Tricks in the sidebar to show Script & Style links, Featured Posts, and Popular Posts. Just kind of a fun way to show lots of content in a small area. I also used it on an article for NETTUTS for a similar purpose.

Both of these examples use the Coda Slider pretty much “out of the box”. Sure the design was heavily altered to fit the job, but the actual functionality wasn’t altered in any way. I recently had the calling to build a “featured content area”. The Coda Slider fit the bill almost perfectly, but it needed a little functionality surgery to do what I wanted it to do. Special thanks to Benjamin Sterling for helping me out and figuring out some quirks for me.

View Demo Download Files

Functionality Checklist

Like I said, the Coda Slider was 90% there already. There is a main content area (panels, if you will), which slide from left to right each with different unique content. There is generally a set number of panels, but the code is written in such a way that adding or removing panels isn’t a huge pain. There are links which act as navigation to quickly jump to any particular panel. These links can be anything (text hyperlink, thumbnail, etc) and link to a unique hash value URL (each panel has a unique URL if need be). Coda Slider provides all of this out of the box.

Here is what we need in addition:

  • Different types of custom content in the panels. We can already put whatever we want in the panels, but to make it easier on ourselves, there will be a couple of different formats ready to go. The main one being an image the size of the entire panel, but featuring a text-overlay.
  • Auto-play. You can still click the thumbnails to jump to any panel, but left onto itself, the slider will slowly cycle through the panels.
  • Arrow indicator. To serve as a visual indication of which panel you are currently viewing, a small arrow will display above the thumbnail pointing into the panel.

Let’s go through the HTML, CSS, and JavaScript.

The HTML

Here is the framework HTML for just the slider itself:

<div class="slider-wrap">
	<div id="main-photo-slider" class="csw">
		<div class="panelContainer">

			<div class="panel" title="Panel 1">
				<div class="wrapper">
					<!-- REGULAR IMAGE PANEL -->
					<img src="images/tempphoto-1.jpg" alt="temp" />
					<div class="photo-meta-data">
						Photo Credit: <a href="http://flickr.com/photos/astrolondon/2396265240/">Kaustav Bhattacharya</a><br />
						<span>"Free Tibet" Protest at the Olympic Torch Rally</span>
					</div>
				</div>
			</div>
			<div class="panel" title="Panel 2">
				<div class="wrapper">
					<!-- PANEL CONTENT -->
				</div>
			</div>
			<div class="panel" title="Panel 3">
				<div class="wrapper">
					<!-- EXAMPLE OF OTHER PANEL POSSIBILITIES -->
					<img src="images/scotch-egg.jpg" alt="scotch egg" class="floatLeft"/>

					<h1>How to Cook a Scotch Egg</h1>

					<ul>
						<li>6 hard-boiled eggs, well chilled (i try to cook them to just past soft boiled stage, then stick them in the coldest part of the fridge to firm up)</li>
						<li>1 pound good quality sausage meat (i used ground turkey meat, seasoned with sage, white pepper, salt and a tiny bit of maple syrup)</li>
						<li>1/2 cup AP flour</li>
						<li>1-2 eggs, beaten</li>
						<li>3/4 cup panko-style bread crumbs</li>
						<li>Vegetable oil for frying</li>
					</ul>
				</div>
			</div>
			<div class="panel" title="Panel 4">
				<div class="wrapper">
					<!-- PANEL CONTENT -->
				</div>
			</div>
			<div class="panel" title="Panel 5">
				<div class="wrapper">
					<!-- PANEL CONTENT -->
				</div>
			</div>
			<div class="panel" title="Panel 6">
				<div class="wrapper">
					<!-- PANEL CONTENT -->
				</div>
			</div>

		</div>
	</div>

	<!-- TO MAKE THESE THUMBNAILS ACTUALLY WORK, BOTH THE HASH VALUE (#1, ETC.)
          AND THE CLASS="CROSS-LINK" ARE REQUIRED -->

	<a href="#1" class="cross-link active-thumb"><img src="images/tempphoto-1thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a>
	<div id="movers-row">
		<div><a href="#2" class="cross-link"><img src="images/tempphoto-2thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a></div>
		<div><a href="#3" class="cross-link"><img src="images/tempphoto-3thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a></div>
		<div><a href="#4" class="cross-link"><img src="images/tempphoto-4thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a></div>
		<div><a href="#5" class="cross-link"><img src="images/tempphoto-5thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a></div>
		<div><a href="#6" class="cross-link"><img src="images/tempphoto-6thumb.jpg" class="nav-thumb" alt="temp-thumb" /></a></div>
	</div>

</div>

The CSS

The full CSS for this example has a basic reset, some utility styles and basic structure. Below, I’ll just show you the CSS relevant to the slider itself, but you can view the full CSS here.

.slider-wrap						{ width: 419px; position: absolute; top: 87px; left: 40px; }
.stripViewer .panelContainer
.panel ul						{ text-align: left; margin: 0 15px 0 30px; }
.stripViewer						{ position: relative; overflow: hidden; width: 419px; height: 285px; }
.stripViewer .panelContainer				{ position: relative; left: 0; top: 0; }
.stripViewer .panelContainer .panel			{ float: left; height: 100%; position: relative; width: 419px; }
.stripNavL, .stripNavR, .stripNav			{ display: none; }
.nav-thumb 						{ border: 1px solid black; margin-right: 5px; }
#movers-row						{ margin: -43px 0 0 62px; }
#movers-row div					{ width: 20%; float: left; }
#movers-row div a.cross-link 				{ float: right; }
.photo-meta-data					{ background: url(images/transpBlack.png); padding: 10px; height: 30px;
											  margin-top: -50px; position: relative; z-index: 9999; color: white; }
.photo-meta-data span 					{ font-size: 13px; }
.cross-link						{ display: block; width: 62px; margin-top: -14px;
											  position: relative; padding-top: 15px; z-index: 9999; }
.active-thumb 						{ background: transparent url(images/icon-uparrowsmallwhite.png) top center no-repeat; }

Anything in there that starts with “.strip” is core to the slider itself, getting those panels set up and the wrapper around them, all the CSS magic needed to make a slider do its thing. From “.nav-thumb” on down is stuff specific to this example. The thumbnails have thin black borders. The meta-data section is pushed off the panel by the image, but then is forced back up with a negative top margin and a transparent black background applied.

The “.active-thumb” class is very important too, if a thumbnail has this class, it gets the special background applied, which is the white arrow we wanted to indicate the current panel. This class will get applied and removed in the JavaScript.

The JavaScript

In the header section of your HTML, you’ll need to include jQuery and all the plugin files. The bare minimum basics look like this:

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery-easing-1.3.pack.js"></script>
<script type="text/javascript" src="js/jquery-easing-compatibility.1.2.pack.js"></script>
<script type="text/javascript" src="js/coda-slider.1.1.1.pack.js"></script>

<script type="text/javascript">
	$(function(){

		$("#main-photo-slider").codaSlider();

	});
</script>

That will activate the slider, but remember we have some extra functionality to get cooking here. Instead of altering the plugin itself, we can add the extras by writing our own little function on top of it. In essence, to achieve the “auto-play” effect, we are going to trigger a click event on the next thumbnail in line every 3 seconds. We’ll call the function “theInterval”, and we’ll need a couple of variables, so we’ll set those up first.

var theInt = null;
var $crosslink, $navthumb;
var curclicked = 0;

theInterval = function(cur){
	clearInterval(theInt);

	if( typeof cur != 'undefined' )
		curclicked = cur;

	$crosslink.removeClass("active-thumb");
	$navthumb.eq(curclicked).parent().addClass("active-thumb");
		$(".stripNav ul li a").eq(curclicked).trigger('click');

	theInt = setInterval(function(){
		$crosslink.removeClass("active-thumb");
		$navthumb.eq(curclicked).parent().addClass("active-thumb");
		$(".stripNav ul li a").eq(curclicked).trigger('click');
		curclicked++;
		if( 6 == curclicked )
			curclicked = 0;

	}, 3000);
};

We aren’t done yet though. We need to call our function, for one thing. But we also need to handle a manual click event on one of our thumbnails properly. We want a manual click event to use the same function, so that the “current” thumbnail can be reset properly and we don’t see any weird jumping around. Right after our function, we can now add this final code, to be fired when the DOM is ready:

$(function(){

	$("#main-photo-slider").codaSlider();

	$navthumb = $(".nav-thumb");
	$crosslink = $(".cross-link");

	$navthumb
	.click(function() {
		var $this = $(this);
		theInterval($this.parent().attr('href').slice(1) - 1);
		return false;
	});

	theInterval();
});

Couple more things to note. Notice the number “6″ is hardcoded in our function. If you change the number of thumbs, you’ll have to change it here as well. Also, you may have noticed the seemingly strange element that click event is being triggered on (”.stripNav ul li a”… where is that?!). You’ll notice that isn’t anywhere in our HTML. Actually, the Coda Slider plugin automatically generates this unordered list from the number of panels when it runs. Very handy sometimes we we have hidden it in the CSS in our example. We are using them in our function though, as they will be the most reliable elements for the click event. They will exist no matter what, and this example will continue to work and be auto-play regardless if we use the thumbnail navigation or not.

View Demo Download Files

I’d like to get a “stop” button in there at some point!


Bookmark at Delicious Digg this! Stumble this! Submit to DesignFloat Submit to DZone

Responses


  1. Gravatar

    Tommy Day
    August 18, 2008
    [ permalink ]

    That’s awesome, and was exactly the thing I was looking for right now. Thanks!

  2. Gravatar

    Fouad Masoud
    August 18, 2008
    [ permalink ]

    sweet. i have made something like this a year ago.
    http://jeeran.com/tour.aspx?lang=e

    this is very challenging when its Arabic.

  3. Gravatar

    Shanna
    August 18, 2008
    [ permalink ]

    Very cool. I was recently looking for something like this for a photog section of my site. Thanks Chris.

    OK, but more importantly… my breakfast totally sucked this morning and then I sit down to browse thru my feeds and see this egg! So, where can I get the full recipe. ;)

  4. Gravatar

    pavs
    August 18, 2008
    [ permalink ]

    YES!!
    I have been looking for something like this for so long after I saw asimilar widget on the header on lifehacker.com. But this one has auto-scroll. This is awesomeness right there.

    Thanks!

  5. Gravatar

    pavs
    August 18, 2008
    [ permalink ]

    I forgot to mention asylum.com has something similar. But I don’t think they use coda

  6. Gravatar

    Anthony
    August 18, 2008
    [ permalink ]

    This is pretty awesome. Made one of these in flash and hated it. This seems to do the trick in a rather painless way.

    Thanks!

  7. Gravatar

    Peter
    August 18, 2008
    [ permalink ]

    Could you implement some way to (temporarily) pause the slider. e.g. on hover or click ‘n’ hold ? I’m having trouble reading the text-intensive items -> very considerate of you to feature the Scotch Egg recipe above this post ;-)

  8. Oh My… That’s great. I was thinking where Could I find it? Now… I’m going implement this in one of my projects.
    Thanks a lot.

  9. Gravatar

    Geert
    August 18, 2008
    [ permalink ]

    Yes this is the best, I like to add some stuff like autoplay stop after cycling for some time and add trigger for mouseover…how to do that?

  10. Gravatar

    Brian Lang
    August 18, 2008
    [ permalink ]

    Very nice. I’ve used the Cycle plugin for Jquery to similar effect. It has a pause onHover feature so you can read the text.
    Some suggestions:
    1. Pause/Stop and Play buttons
    2. Configurable timer variable - ie. 4 seconds/slide or whatever.
    3. Variable slide timer - ie. if one slide needs to be on for more time (say the first slide; or a text-heavy slide) before switching, that could be set as a variable
    4. Effects variable. I’m not fond of the way this example jumps from the last image back to the first one. I prefer a look that’s more along the lines of a carousel of images/slides instead.

  11. This is great i can imagine this being used for many a project,

    Thanks for the post and download
    Max | Design Shard

  12. Gravatar

    J.
    August 18, 2008
    [ permalink ]

    I second what Brian Lang said.

  13. Gravatar

    Mike Bobiney
    August 18, 2008
    [ permalink ]

    [js/jquery-easing-compatibility.1.2.pack.js]
    Adds compatibility for applications that use the pre 1.2 easing names

    I was wondering why that extra js file was there for.

  14. Gravatar

    Dan
    August 18, 2008
    [ permalink ]

    Can you use more than 6 images? Would be nice if the thumbnail bar had separate, independent scrolling controls. For example, if it shifted the entire thumbnail row over once the 7th picture comes through the auto-play sequence.

    This works great though. I especially love the easing! Great post!

  15. Gravatar

    Fornya
    August 18, 2008
    [ permalink ]

    I’m going to have to say this again and again: “Thank you.” I am a total hack, who’s still working to maintain a website as not just a hobby, but also as a challenge to update and to continue to learn new things. Stumbling across your site, as well as your Script & Style site has been an absolute blessing.

    Thank you for sharing. I love being able to learn new things and apply them, and you really do make it easy.

  16. Gravatar

    Travis
    August 18, 2008
    [ permalink ]

    Definitely going into my bookmark.
    This is something I would like to try out on one of my site!!

  17. Gravatar

    Patrick Sweeney
    August 18, 2008
    [ permalink ]

    Dumb question, but I can integrate this with WordPress correct? I guess the main thing to look out for is making sure I have the paths to the jQuery files correct and obviously a little massaging it, but that should be about it right?

  18. Gravatar

    John S.
    August 18, 2008
    [ permalink ]

    Awesome implementation! Thanks for posting this. I just have one design comment… I think the “arrow” that indicates the content needs to be changed. When the content being displayed is white, the pointer disappears. Maybe add a small stroke? Or, have the pointer point down over the thumbnail (again, with a small stroke)?

  19. Gravatar

    Corey F
    August 20, 2008
    [ permalink ]

    how do you stop this script once it get’s to the end?

  20. Gravatar

    Malcom
    August 20, 2008
    [ permalink ]

    This is awesome, I was looking for something similar to this, after looking at other jquery slide shows this is the best i found, specially the way you did souce files even including the photoshop psd now that is impressive; on msn.co.uk they got the same thing but with the thumbnails on the right vertically instead of horizontal bottom.

    Thanks a lot really appreciated.

  21. Gravatar

    Joseph
    September 2, 2008
    [ permalink ]

    I’ve implemented this and it works great.

    However styling it is a little harder. How come the first thumbnail is out there on its own? It ends up being higher or lower than the other five. Lots of weird negative margins going on which i’m not used to.

  22. Gravatar

    bob
    September 4, 2008
    [ permalink ]

    how could this be implemented into a wordpress plug in or theme?

  23. Gravatar

    gamermk
    September 19, 2008
    [ permalink ]

    I’m finding that jquery-1.2.6.min.js isn’t compatable with lightview :(

  24. Gravatar

    Martin Sarsini
    September 30, 2008
    [ permalink ]

    Very nice example. But to much difficult code and not optimized for other developer to use.

  25. Gravatar

    Jose
    October 12, 2008
    [ permalink ]

    This is cool :) i’ll be adding it very soon to http://gtablog.es/ :)

    Thanks for the great tutorial!

  26. Gravatar

    Jen
    October 13, 2008
    [ permalink ]

    I have this on our internal company’s website for front page news and interesting architecture.

    I’ve changed the code so that the auto playing slows down a little, but it only slows down the first tile to the second, after that, it goes back up to a faster speed.

    The only comments/complaints that I’ve had from employees is that when they’re trying to read something, it goes to the next page. It would be great if there were a pause/play button on the bottom or side.

    Or maybe that when they click on a thumbnail, the auto-playing feature is disabled.

    Thanks for the code! everything else works great!

  27. Thanks! I am a part of a web design company based in New York and this is a very helpful article!! Thanks again!

  28. Gravatar

    Jeff
    October 21, 2008
    [ permalink ]

    Hi,

    I was wondering if I can use this script with image-buttons. I’ve seen some examples of this slider, that uses textlinks or css tabs, but what I would like to use are separate buttons for the active and in-active states. You can see a little example on my website where there are 3 buttons in black&white. When you hover over them, they become colored. What I would like is, when you click on a button, the colored one stays visible.

    Now I can achieve this using Flash, but I want to make my site non-flash for a change =)

    Thanks in advance.
    Jeff

  29. Gravatar

    screaming code monkey
    October 27, 2008
    [ permalink ]

    Hi!
    Ive been a big fan of your work for yonks! and your teaching style rules! Been working on a web assignment lately using alot of jQuery to get a ‘flash’ effect without the flash,,, anyway my comment / question is:
    ‘is there a good way/practice to implement good degradation into jQuery code,, so that if javascript is turned off you wont get the content spewing out the bottom? or a css hack?’
    Again,, Love your work! Inspires me to do better design work!

  30. Gravatar

    Barbaros Alp
    November 4, 2008
    [ permalink ]

    Hi,
    First of all, i want to thank you about your great article and content slider.
    One thing that i couldn’t figure out how to integrate it with my asp.net application. It is easy to create panels in a repeater but when it comes to pager it is a bit confusing to achive this.
    Because one the pager item is out of the “movers-row” div.
    Thank you

  31. Gravatar

    Prashant
    November 8, 2008
    [ permalink ]

    Hi,
    This is nice. I use it for my website but slider moves correctly but the images in top panel not changes,so what happen that? I follow the instruction given here also put the code correctly so what’s going wrong?

  32. Gravatar

    Gazikent
    November 11, 2008
    [ permalink ]

    This is pretty awesome. Made one of these in flash and hated it. This seems to do the trick in a rather painless way.

  33. Gravatar

    andrea
    November 27, 2008
    [ permalink ]

    isn't it possible to stop the slider?

    thanks

  34. Gravatar

    Joe
    December 1, 2008
    [ permalink ]

    Nice tutorial as always, however I have some problem when I add a second row of images, don't know why. So I use your tutorial as an inspiration but doing in a bit different approach to get the same outcome. I used jquery cycle to do this, it's automatically create the thumbnails, which is more easier. Have a look at my work at http://www.thainesia.com.au/image. Any comments are welcome. Thanks


Leave a Comment

Gravatar

Your Name
December 3, 2008