Creating a Simple Photo Album with jQuery Similar to Facebook

For the javascript part, I’ve decided to use jQuery as well as the jquery.cycle and jquery.lightbox plugins.

HTML structure

The HTML code needed for a gallery like this is actually rather simple. All we have to do, is to print the images as plain old linked HTML images and wrap image collections in container elements (DIVs).

<h1>My Photo Collection</h1>
<p id="intro">
  This is my photo album...yaddi-yaddi-ya...
<div class="album">
  <h2>Wicked Album</h2>
  <a href="photos/image1.jpg">
    <img src="photos/thumb_image1.jpg" alt="Lorem" />
  <a href="photos/image2.jpg">
    <img src="photos/thumb_image2.jpg" alt="Ipsum" />
  <a href="photos/image3.jpg">
    <img src="photos/thumb_image3.jpg" alt="Lorem" />
<div class="album">
  <h2>Wickier Album</h2>
  <a href="photos/image4.jpg">
    <img src="photos/thumb_image4.jpg" alt="Ipsum" />
  <a href="photos/image5.jpg">
    <img src="photos/thumb_image5.jpg" alt="Yadi" />
  <a href="photos/image6.jpg">
    <img src="photos/thumb_image6.jpg" alt="Yadi" />
  <a href="photos/image7.jpg">
    <img src="photos/thumb_image7.jpg" alt="Ya" />
<div class="album">
  <h2>Wickiestest Album</h2>
  <a href="photos/image8.jpg">
    <img src="photos/thumb_image8.jpg" alt="Lorem" />
  <a href="photos/image9.jpg">
    <img src="photos/thumb_image9.jpg" alt="Ipsum" />

The Magic of jQuery Plug-Ins

In order to turn this into something a bit more exciting than a bunch of linked images with a blue border, we add some JavaScript and CSS to the page.

First, make sure to include jQuery, jQuery Cycle and jQuery Lightbox to the page. Then add the following JavaScript code to another .js-file that is loaded into the same page:

$(document).ready( function() {
  // Dynamically add nav buttons as these are not needed in non-JS browsers
  var prevNext = '<div id="album-nav"><button ' +
                   'class="prev">&laquo; Previous' +
                   '</button><button>' +
                   'Next &raquo;</button></div>';
  // Add a wrapper around all .albums and hook jquery.cycle onto this
  $('.album').wrapAll('<div id="photo-albums"></div>');
    fx:     'turnDown',
    speed:  500,
    timeout: 0,
    next:   '.next',
    prev:   '.prev'
  // Remove the intro on first click -- just for the fun of it
  $('.prev,.next').click(function () {
  // Add lightbox to images
  $('.album a').lightBox();

You may of course remove the cycling effect.
The only JavaScript you’d then need is $('.album a').lightBox();.

Adding Style Info

Last, we add some style info to make our albums look at least somewhat good.

/** for this example, I don't give a rats (*) about IE */
.album {
  width: 600px !important;
  clear: both;
.album h2 {
  clear: both;
.album a {
  display: block;
  float: left;
  margin-right: 1em;
  padding: 0.5em;
  border: 1px solid black;
  background-color: #eee;
  text-align: center;
.album a:hover {
  border-color: blue;
.album img {
  width: 72px;
  height: 100px;
  border: 0;
#album-nav {
  margin-top: 1em;
  max-width: 500px;
.prev, .next {
  border: 1px outset #ccc;
  color: #000075;
  background-color: white;
  padding: 0.25em;
.prev:hover,.next:hover {
  border-style: inset;
.prev {
  float: left;
.next {
  float: right;


2 thoughts on “Creating a Simple Photo Album with jQuery Similar to Facebook

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s