Quantcast
Channel: Envato Tuts+ Web Design
Viewing all articles
Browse latest Browse all 4338

The Perfect Lightbox? Using PhotoSwipe with jQuery

$
0
0

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.
  • Responsive: 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:

  1. Creating the project directories.
  2. Creating an HTML document.
  3. Linking to jQuery. In this tutorial I’m going to use jQuery 2.0, therefore only supporting modern browsers.
  4. Grab the copies of PhotoSwipe library which comprises the files: photoswipe.min.jsphotoswipe-ui-default.min.js (the code that constructs the PhotoSwipe UI), default-skin.cssdefault-skin.pngdefault-skin.svg, andpreloader.gif.
  5. 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:

  1. We first initialize with an empty array in items = [].
  2. Then we loop through each anchor tag with the .each() method.
  3. $href = $(this).attr('href') obtains the value in the href attribute, storing the path of the full image.
  4. $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.
  5. We are able to obtain the value of the width and the height separately with: $width = $size[0] and $height = $size[1].
  6. 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


Viewing all articles
Browse latest Browse all 4338

Trending Articles