I recently came across a JavaScript library called PhotoSwipe, and I am truly impressed. It is a brilliant library which overlays an image or a group of images on your current page, an effect popularly known as lightbox. PhotoSwipe is currently developed by Dmitry Semenov–he rebuilt it last year when he took over the project. Improvements include:

Modular : PhotoSwipe is built into several modules, allowing you to ditch particular features you don’t need. This keeps the library very lightweight.

: PhotoSwipe is built into several modules, allowing you to ditch particular features you don’t need. This keeps the library very lightweight. Responsive : an indispensable feature by all means. Photoswipe serves the appropriate image size based on the current viewport.

: an indispensable feature by all means. Photoswipe serves the appropriate image size based on the current viewport. Touch gesture: PhotoSwipe supports some touch gestures which allow users to interact through Tap and Pinch, giving them pop-up, zoom, and close features.

The only downside is that PhotoSwipe won’t run out of the box. As the developer states:

“PhotoSwipe is made simple and fast for end users, not for developers. It ain't a simple jQuery plugin, at least basic JavaScript knowledge is required to install.

But it can be used in conjunction with other libraries. In this tutorial, we are going to look at how to use PhotoSwipe with jQuery. We’ll make the whole process easily comprehensible, even if you’re not well-versed in pure JavaScript.

Let’s make a start.

1. Getting Started

We’ll begin with:

Creating the project directories. Creating an HTML document. Linking to jQuery. In this tutorial I’m going to use jQuery 2.0, therefore only supporting modern browsers. Grab the copies of PhotoSwipe library which comprises the files: photoswipe.min.js , photoswipe-ui-default.min.js (the code that constructs the PhotoSwipe UI), default-skin.css , default-skin.png , default-skin.svg , and preloader.gif . Arrange the files in your preferred directory order and link them within the HTML file.

2. Structuring the HTML

The HTML structure is the most essential part since it will eventually drive how we write the JavaScript. Here we will assume that we have a couple of images, as follows:

Data Attributes

Aside from the class and the Schema attributes (which will help with SEO), we’ve also added a data-* attribute in the anchor tag wrapping the image. This stores the full size (width x height) of the image.

More Markup

Now we add the markup that PhotoSwipe will use to overlay the full images. Add this just before the closing body tag, </body> .

We are all set with the HTML, so now we can proceed with the JavaScript. As mentioned we will be using jQuery.

3. jQuery

Our images are grouped within a div with the picture class. There may be several more image groups within the page, hence we loop through each .picture element.

Details

PhotoSwipe requires us to provide the full image source along with its size. To take care of this we create a function that will loop through the anchor tag to retrieve these details, as follows:

Let’s break the this code downs into several pieces:

We first initialize with an empty array in items = [] . Then we loop through each anchor tag with the .each() method. $href = $(this).attr('href') obtains the value in the href attribute, storing the path of the full image. $size = $(this).data('size').split('x') will obtain the value of the data-size attribute through the jQuery .data() method. We then use the the split() method to separate the value. We are able to obtain the value of the width and the height separately with: $width = $size[0] and $height = $size[1] . Using the push() method we collect al this information in an Array.

Test in Console

We now have the image source and the image size stored in the items variable. To verify, you may run the variable through the console.log() and you should find the following data structure in the DevTools.

Click

Next, we bind the figure element with the click event, construct a PhotoSwipe instance, pass the items along with the configuration options, and finally initialize PhotoSwipe.

The reason we use the figure element to click on, rather than the anchor tag, is so that we are able to easily retrieve the index (or the subsequent order). This allows us to inform PhotoSwipe of the correct index to overlay the corresponding image.

Conclusion

We have just implemented PhotoSwipe using jQuery. The code is comparably slimmer and more easily understandable than when written in vanilla JavaScript.

Now you’re started, you can also adjust PhotoSwipe with a handful options provided and even preload the image so you will see the full image instantly upon clicking. I will leave the styles to your imagination since there are no strict rules for those.

I hope you’ve learned something from this tutorial, and find it useful at times. Don't forget to check out the live demo and leave any questions or thoughts in the comments!

Further Resources