Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Creating a Filterable Portfolio with jQuery (0 Comments)
Admin: Posted Date: April 4, 2010

If you have worked in your field for a while, there is a pretty good chance that you have a rather extensive portfolio. To make it a little easier to navigate, you will probably be tempted to break them into different categories. In this tutorial, I will show you how to make “filtering by category” a little more interesting with just a little bit of jQuery.

Creating a “Filterable” Portfolio with jQuery

The Markup

Our portfolio is nothing more than a simple unordered list:

  1.  <ul id="portfolio">  
  2.     <li><a href="#"><img src="images/a-list-apart.png" alt="" height="115" width="200" />A List Aparta>li>  
  3.     <li><a href="#"><img src="images/apple.png" alt="" height="115" width="200" />Applea>li>  
  4.     <li><a href="#"><img src="images/cnn.png" alt="" height="115" width="200" />CNNa>li>  
  5.     <li><a href="#"><img src="images/digg.png" alt="" height="115" width="200" />Digga>li>  
  6.     <li><a href="#"><img src="images/espn.png" alt="" height="115" width="200" />ESPNa>li>  
  7.     <li><a href="#"><img src="images/facebook.png" alt="" height="115" width="200" />Facebooka>li>  
  8.     <li><a href="#"><img src="images/google.png" alt="" height="115" width="200" />Googlea>li>  
  9.     <li><a href="#"><img src="images/netflix.png" alt="" height="115" width="200" />Netflixa>li>  
  10.     <li><a href="#"><img src="images/nettuts.png" alt="" height="115" width="200" />NETTUTSa>li>  
  11.     <li><a href="#"><img src="images/twitter.png" alt="" height="115" width="200" />Twittera>li>  
  12.     <li><a href="#"><img src="images/white-house.png" alt="" height="115" width="200" />White Housea>li>  
  13.     <li><a href="#"><img src="images/youtube.png" alt="" height="115" width="200" />YouTubea>li>  
  14. ul>  

Note: I was by no means a part of creating these wonderful sites; I am just using them as examples.

Categorizing the Portfolio

We are going to assume that our portfolio can be broken down into 5 categories:

  • Design
  • Development
  • CMS
  • Integration
  • Information Architecture

In order to use the categories we have defined, we will convert them to lowercase and replace all spaces with hyphens:

  • Design = design
  • Development = development
  • CMS = cms
  • Integration = integration
  • Information Architecture = information-architecture


We are going to assume that each portfolio item could be in one or many categories, so we are going to randomly add our newly created categories as classes to the list items:

  1. <ul id="portfolio">  
  2.     <li class="cms integration">  
  3.         <a href="#"><img src="images/a-list-apart.png" alt="" height="115" width="200" />A List Aparta>  
  4.     li>  
  5.     <li class="integration design">  
  6.         <a href="#"><img src="images/apple.png" alt="" height="115" width="200" />Applea>  
  7.     li>  
  8.     <li class="design development">  
  9.         <a href="#"><img src="images/cnn.png" alt="" height="115" width="200" />CNNa>  
  10.     li>  
  11.     <li class="cms">  
  12.         <a href="#"><img src="images/digg.png" alt="" height="115" width="200" />Digga>  
  13.     li>  
  14.     <li class="design cms integration">  
  15.         <a href="#"><img src="images/espn.png" alt="" height="115" width="200" />ESPNa>  
  16.     li>  
  17.     <li class="design integration">  
  18.         <a href="#"><img src="images/facebook.png" alt="" height="115" width="200" />Facebooka>  
  19.     li>  
  20.     <li class="cms information-architecture">  
  21.         <a href="#"><img src="images/google.png" alt="" height="115" width="200" />Googlea>  
  22.     li>  
  23.     <li class="integration development">  
  24.         <a href="#"><img src="images/netflix.png" alt="" height="115" width="200" />Netflixa>  
  25.     li>  
  26.     <li class="information-architecture">  
  27.         <a href="#"><img src="images/nettuts.png" alt="" height="115" width="200" />NETTUTSa>  
  28.     li>  
  29.     <li class="design information-architecture cms">  
  30.         <a href="#"><img src="images/twitter.png" alt="" height="115" width="200" />Twittera>  
  31.     li>  
  32.     <li class="development">  
  33.         <a href="#"><img src="images/white-house.png" alt="" height="115" width="200" />White Housea>  
  34.     li>  
  35.     <li class="cms design">  
  36.         <a href="#"><img src="images/youtube.png" alt="" height="115" width="200" />YouTubea>  
  37.     li>  
  38. ul>  

Adding Category Navigation

Now that we have the portfolio pieces in place, we are going to need some way to navigate through them. Another unordered list should do:

  1. <ul id="filter">  
  2.     <li class="current"><a href="#">Alla>li>  
  3.     <li><a href="#">Designa>li>  
  4.     <li><a href="#">Developmenta>li>  
  5.     <li><a href="#">CMSa>li>  
  6.     <li><a href="#">Integrationa>li>  
  7.     <li><a href="#">Information Architecturea>li>  
  8. ul>  

Since I want the default view of the portfolio to show All items, I have assigned a class of current to the first list item.

You will probably look at that and question me on the accessibility of this example. My thought is that you have 3 options to solve that problem.

  1. When creating a portfolio like this, there is a strong probability that it will be database driven. Thus, you should be able to create a separate page for each category. So if a user does not have JavaScript enabled, they can go to the separate page with the filtered portfolio..
  2. You could always just write in the navigation with JavaScript before the portfolio items:
    1. $(document).ready(function() {  
    2.     $('ul#portfolio').before('All
    3. Design
    4. Development
    5. CMS
    6. Integration
    7. Information Architecture
    8. ');  
    9. });  

Ok, you’ve got my notes on accessibility, so don’t criticize me for not thinking about it.

The CSS

This tutorial is not meant to be about CSS, so I’m going to run through the CSS pretty quickly.

I always start with some basic styles as a sort-of framework, so I’m not going to go over those styles right now. These styles basically just act as a reset and define some styling for basic elements.

To start, I just want to display the categories across the top horizontally with a border between each:

  1. ul#filter {  
  2.     floatleft;  
  3.     font-size16px;  
  4.     list-stylenone;  
  5.     margin-left: 0;  
  6.     width: 100%;  
  7. }  
  8. ul#filter li {  
  9.     border-right1px solid #dedede;  
  10.     floatleft;  
  11.     line-height16px;  
  12.     margin-right10px;  
  13.     padding-right10px;  
  14. }  

Next, I want to remove the border from the last list item (in browsers that support it) and change the display of the links:

  1. ul#filter li:last-child { border-rightnonemargin-right: 0; padding-right: 0; }  
  2. ul#filter a { color#999text-decorationnone; }  

I also want to make sure and differentiate the currently selected category:

  1. ul#filter li.current a, ul#filter a:hover { text-decorationunderline; }  
  2. ul#filter li.current a { color#333font-weightbold; }  

Ok, now that we have the category navigation styled, let’s focus on the actual layout of the portfolio. Let’s plan on floating 3 list items next to each other with a border around each one:

  1. ul#portfolio {  
  2.     floatleft;  
  3.     list-stylenone;  
  4.     margin-left: 0;  
  5.     width672px;  
  6. }  
  7. ul#portfolio li {  
  8.     border1px solid #dedede;  
  9.     floatleft;  
  10.     margin: 0 10px 10px 0;  
  11.     padding5px;  
  12.     width202px;  
  13. }  

Now, we just need to add some basic styling for the images and links:

  1. ul#portfolio a { displayblockwidth: 100%; }  
  2. ul#portfolio a:hover { text-decorationnone; }  
  3. ul#portfolio img { border1px solid #dedededisplayblockpadding-bottom5px; }  

Compensating for Internet Explorer 6

Of course, let’s not forget about our friend IE6. Once you start clicking through some of the filters, the layout gets a little crazy.

 

 

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: