Archive Navigator Title Image

Be sure to check out part two of this tutorial afterwards!

Last week I received an email from fellow reader Jaap asking if I could provide a link to a plugin that would implement something like my very cool Archive Navigator you see to the right.

I did some searching and found a jQuery plugin that is capable of the same effect called Coda-Slider. The reason this plugin is called 'Coda-Slider', is because this effect was made popular on Panic's product site for their web development software called Coda.

[Javascript required to view Flash movie, please turn it on and refresh this page]

If you're looking for a quick solution to pull this off, head on over to Niall Doherty's blog (the author of the Coda-Slider plugin) and download it. There's enough documentation there to pull off the effect in a matter of minutes if you know what you're doing.

But if you're looking to improve on your web developing skills, I'll show you how to pull this off from scratch and customize it for WordPress using just the jQuery library.

Warning: The following will require intermediate HTML and CSS knowledge.

Step 1: Building the Navigator

First, we're going to build out the elements needed for the Navigator. Open up your favorite text editor and create a couple of divs which will nest like this:

<div class="crop">
        <div class="widearea">
                <div id="divone">
                        Hello World
                </div>
                <div id="divtwo">
                        Hello Earth
                </div>
                <div id="divthree">
                        Hello Planet
                </div>
        </div>
</div>

With CSS, we will try to achieve a layout like the following diagram:

Here's some sample CSS code that will go along with the HTML above:

div.crop {
        width: 300px;
        height: 400px;
        overflow: hidden;
}

div.widearea {
        width: 1000px;  /*make it as wide as needed to fit all child divs
                        in one row*/

        height: 400px;
}

div.widearea div {
        width: 300px;
        height: 400px;
        display: block;
        float: left;
}

Since the .crop div is a parent div of the other divs, specifying fixed dimensions as well as assigning hidden to the overflow attribute will effectively crop out anything not inside its dimensions. Although this example only has three divs, you can create as many as you want as long as you make the .widearea div wide enough.

Next, we'll need to add some controllers available to the user so that they can interact with this. For simplicity's sake, let's add three buttons:

<div class="crop">
        ...
</div>

<button id="buttonone">Go to divone</button>
<button id="buttontwo">Go to divtwo</button>
<button id="buttonthree">Go to divthree</button>

Now that we've built out the HTML, we will start getting into the fun stuff.

Effects with jQuery

The first thing that needs to happen before we can start writing javascript for the jQuery library is to reference it. I recommend downloading it and referencing it off your own host rather than referencing it elsewhere.

Once you've dropped the library into your host, add a reference to it in your html like so:

<script type="text/javascript" src="jquery-1.2.3.min.js"></script>

Next, create a new js file with your text editor. This will hold all the custom javascript we will write. Let's take a step back and try and figure out what our objectives are before we start writing code:

  1. Add a click event handler to our buttons
  2. Create one (or more) functions which will produce the sliding effect

So for the first objective, creating a click event handler (also known as onClick) is real simple using jQuery:

$("#buttonone").click(function() {
        enter your function here
});

Hopefully it's intuitive enough that you clued in that to create the other click event handlers for the other two buttons, you just had to copy and paste that line of code and replace buttonone with buttontwo or buttonthree. To explain this a little better: we are targeting the button with id buttonone (as done with $("#buttonone")) and then assigning a click event handler which will call a function.

Next, we will create the functions inside these click event handlers which will "scroll" the .widearea div horizontally:

$("#buttonone").click(function() {
        $(".widearea").animate({
                marginLeft: "0px"
        }, 500);
});

In our function, we are instructing the .widearea div to animate to margin-left (note how jQuery's syntax is a little different from CSS) of 0. If you got this set up, you'll notice clicking on the button doesn't do anything, that's because by default, we are already seeing divone so there's no reason to move the .widearea div. Let's set it up for buttontwo:

$("#buttontwo").click(function() {
        $(".widearea").animate({
                marginLeft: "-300px"
        }, 500);
});

If you add this in and click on buttontwo, the .widearea div should move 300px to the left so that #divtwo is in view. If you're wondering what the "500" is for, it specifies how long the animation should last in milliseconds.

Putting it all together now...

Eventually, after coding everything up, you'd end up with something like this:

<html>
<head>
        <title>Slider</title>
        <style type="text/css" media="screen">
                div.crop {
                        width: 300px;
                        height: 400px;
                        overflow: hidden;
                }

                div.widearea {
                        width: 1000px;
                        height: 400px;
                }

                div.widearea div {
                        width: 300px;
                        height: 400px;
                        display: block;
                        float: left;
                }
        </style>
</head>
<body>
<div class="crop">
        <div class="widearea">
                <div id="divone">
                        Hello World
                </div>
                <div id="divtwo">
                        Hello Earth
                </div>
                <div id="divthree">
                        Hello Planet
                </div>
        </div>
</div>
<button id="buttonone">Go to divone</button>
<button id="buttontwo">Go to divtwo</button>
<button id="buttonthree">Go to divthree</button>

<script type="text/javascript" src="jquery-1.2.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
        $("#buttonone").click(function() {
                $(".widearea").animate({
                        marginLeft: "0px"
                }, 500);
        });
        $("#buttontwo").click(function() {
                $(".widearea").animate({
                        marginLeft: "-300px"
                }, 500);
        });
        $("#buttonthree").click(function() {
                $(".widearea").animate({
                        marginLeft: "-600px"
                }, 500);
        });
});
</script>
</body>
</html>

If you want, you can copy and paste the above into an html file, stick it in a directory with version 1.2.3 of the jQuery library (the latest stable version as of this post) and run it for a quick demo. It functions the exact same as my archive navigator.

Now, you'll notice something new in the javascript. There's this $(document).ready(function(){ wrapping it all and it ensures that no javascript gets run until the page is fully loaded. It prevents any javascript errors getting thrown because if you didn't have this and this javascript gets executed before the button or div involved is loaded, it wouldn't work.

Customizing it for WordPress

If you've ever created or modified a WordPress theme before, you might have come across the template tags. You can use these inside the divs of .widearea to list categories, tags, and more. Here are a few examples:

  • Tag cloud:
    <?php wp_tag_cloud(''); ?>
  • Categories listing:
    <?php wp_list_categories('arguments'); ?>
  • Last five posts:
    <?php query_posts('showposts=5');
            while (have_posts()) : the_post(); ?>
                    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

Still with me?

I tried to make this mini-tutorial as newbie friendly as possible but there are some advanced topics in here that might have gone over your head. If you have any questions, feel free to ask through the comments as I'm sure I might have not explained something very well or left out something.

And if you're a seasoned programmer, I know some of this stuff is bad practice (I realize that the javascript is anti-OO), but I tried to make it accessible as possible.


Possibly Related


27 Comments so far... perhaps you would like to leave one?

well.. i have the jQuery file on my machine, I copy and pasted your code into a blank html page, linked them together and previewed it and the divs wont float and the buttons wont work.

edited some things and now the divs are floating but the script still isnt working. cant get the buttons to do anything.

definitely driving me crazy…

Comment by David Sparks — April 13, 2008 @ 8:57 pm

David, it looks like I failed which version of jQuery to download as there are three links on the jQuery home page. Be sure to download either the jQuery 1.2.3 (94kb, Uncompressed) or jQuery 1.2.3 (29kb, Packed) and if necessary, rename it to jquery-1.2.3.min.js. It sounds like you might have downloaded the minified and gzipped version and you would need to add some code to decompress it before you would be able to use it.

Comment by Sam Lu — April 14, 2008 @ 6:18 am

Thanks! yeah that is what i did. I managed to get it working late into the night hah.

Thank you!

Comment by David Sparks — April 14, 2008 @ 12:10 pm

Hi Sam,

Awesome tutorial and really appreciated! It’s great to understand something rather than just cut & paste!

I wonder if you can add an explanation of scrolling in 2-D? And how this would affect the code?

Thanks.

Jack

Comment by Jack — April 27, 2008 @ 4:19 pm

Jack, to introduce vertical scrolling, I guess you can just add the marginTop parameter to the animation call.

I found a jQuery plugin which does 2D scrolling very well called LocalScroll and is worth checking out.

Comment by Sam Lu — April 27, 2008 @ 5:01 pm

Thanks for the great tutorial, this is the simplest slider I’ve seen so far.

However, it doesn’t seem to work in Safari on OS X. I’ve tested in Firefox (Win and Mac), IE6 (Win), IE7 (Win), and Opera, and it works perfectly in all of those browsers. Safari just doesn’t slide.

Any suggestions or perhaps this is a bug in Safari’s support for jQuery?

Cheers!

Comment by Alex — April 29, 2008 @ 3:25 am

That’s weird Alex, it works fine for me on Safari 3. Does my Archive Navigator near the top right of this page slide for you? What version of Safari are you using?

Comment by Sam Lu — April 29, 2008 @ 6:15 am

[...] Visit Tutorial [...]

Pingback by NETTUTS - Web development tutorials and links - Best of the Web - April — April 30, 2008 @ 8:41 pm

Thanks! Really quick and easy.

Comment by moh — May 1, 2008 @ 7:33 am

Just came across this tutorial. Very nice to see such a quick way of getting this effect working.

For all those having problems with this then if you are copy-pasting directly from this web-site you will probably find the problem you are having is with all the inverted commas being the wrong type. If you delete them all one by one and code them back in again then that should hopefully get rid of some of your headaches! ;-)

Nice tutorial though so thanks.

Best wishes,

Mark

Comment by Mark Bowen — May 1, 2008 @ 12:19 pm

Hey, I have a quick question,
I was wondering if it is possble to control the slider with the keyboard- left and right buttons
?

Comment by Paul — May 2, 2008 @ 1:50 pm

Paul, theoretically it would be possible to control the slider with the keyboard left and right keys by adding event handlers for the left & right keypresses. With that said, almost all the javascript would need to be revamped as you could potentially run it on just two functions; one to move the div left and the other to move it right. You’d then need to set boundaries and control it via incrementing/decrementing a counter.

Hopefully those hints help if you decide to take it on ;)

Comment by Sam Lu — May 2, 2008 @ 7:59 pm

Thank you so much. I found this tutorial very helpful.
I’m new to jquery and javascript in general so I’m glad when I find such easy to follow tutorials online. Now all I need is to find a way to have a “current” state on the taps (I used an ul with links instead of the buttons). Can you help?

Comment by Sarah Neuber — May 4, 2008 @ 1:07 am

Sarah, to have a current state on the tabs, you will need to make use of the click event handler and toggle class attribute provided by jQuery. Essentially, you will need to create a CSS class which you will assign to the current tab and toggle that when clicked.

Comment by Sam Lu — May 5, 2008 @ 10:44 pm

Thanks for replying. I did this in the meanntime and as far as I can see it works ok. What do you think?

$(”#tabone a”).click(function() {
/* remove open class from other li’s
and add it to this li*/
$(this).parents(”li”).addClass(’open’)
.siblings().removeClass(’open’);
$(”.widearea”).animate({marginLeft: “0px”}, 500);
return false;
});

Comment by Sarah Neuber — May 6, 2008 @ 12:11 am

If that works for you Sarah, then you should use it. Your method is a little bit more creative than mine actually, but since you wrote it, you’ll understand it if the time comes to alter it or fix it if something goes wrong.

Comment by Sam Lu — May 7, 2008 @ 10:17 am

Hello Sam,

This is a terrific tutorial and I’m excited to put it into motion on my site. But, I was wondering something. Is there any way to make this work without javascript enabled? In other words, is there a way to make this degrade nicely?

Thanks,

Tyler

Comment by Tyler — May 10, 2008 @ 8:32 pm

@Tyler: I will write a follow-up post on this as there’s too much to cover in one comment :)

Stay tuned.

Comment by Sam Lu — May 10, 2008 @ 10:43 pm

Thanks! I look forward to reading it.

Comment by Tyler — May 10, 2008 @ 11:06 pm

Hi!

I’ve tried for quit a while now, and I just can’t get it to work… Maybe I’ve just read myself blind on my one mistakes, but can you look at it?
http://sorthe.org/index.phps

I’ve made some changes, put jQuery in the folder /js/ and put the scripts in the head instead of the body, both shouldn’t have anything to say if it works or not…
I’m on Mac OS X, Safari 3 (doesn’t work in FireFox neither)

Comment by Sorthe — May 21, 2008 @ 5:09 pm

Sorthe,

Took a look at your code that you have linked to and nothing seems to be wrong with it from what I can tell. Just make sure you definitely have the CSS file linked to otherwise you will get the contents of the three DIVs showing on the page and when you click any of the buttons then all the content will slide off the page. As long as you have the same CSS as shown above then your code should be working just fine.

Best wishes,

Mark

Comment by Mark Bowen — May 21, 2008 @ 5:35 pm

Hmm…
Well, thanks, I tried to include the CSS directly into the index.php as you did, and then it worked (yey!), so weird that it doesn’t work with an external CSS… I don’t really fancy having my CSS in the actual page, ’cause I like to have it all in one place, if you understand :P

I just have to try again, but thanks for a great tutorial :D

Comment by Sorthe — May 21, 2008 @ 5:52 pm

Hiya Sorthe,

Just as a quick note that it should work with an external CSS file, not too sure why it wouldn’t. Must be something else up there I would think.

Also this isn’t my tutorial, just thought I should point that out ;-) I was just looking at your code and saw that nothing looked wrong with it. I’m not actually anything to do with this site and in no way affiliated with it but rather just someone who happened across the tutorial too. ;-) :-)

Just thought you should know so the props can go the correct person ;-)

Thanks for a great tutorial, sitemaster!

Best wishes,

Mark

Comment by Mark Bowen — May 22, 2008 @ 4:17 am

Nice tutorial you have there! May i ask how do i change css styles for ‘current’ tabs? I have changed it to , but i want it to look a little different when it is at the current tab, are you able to give some pointers? Thanks!

Comment by Hungzai — May 29, 2008 @ 2:10 am

hi there
i’am new to the jquery and i tried your navigator by doing copy paste as you said but it didn’t work as it works in the right panel of this site.in my understanding it should work if i copy and paste in html page.

thanking you

suman

Comment by suman shrestha — July 5, 2008 @ 8:10 am

Suman, jquery just released a new version so be sure the line:

<script type=“text/javascript” src=“jquery-1.2.3.min.js”></script>

references the right file (jquery is up to 1.2.6 now).

Beyond that you should not be experiencing any other problems as I’ve fixed the quotation issue Mark Bowen mentioned earlier so there is nothing wrong with the HTML.

Comment by Sam Lu — July 5, 2008 @ 10:45 am

hi there

thanx for the quick response yeap i put the same you provided in my response and about the procedure i simple copy and paste it in the blank html files.but it’s not floating as it floated in the right bar

Comment by Suman — July 8, 2008 @ 6:54 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>