Why 2.0 is different

Version 2.0.0 was a complete rewrite in modern ES6 JavaScript that no longer needs jQuery. The public surface kept its shape — the options, the event names and the methods — so on most sites only the line that creates the lightbox changes.

The 1.x series was a jQuery plugin through and through. Moving to 2.x removes that dependency from pages that only loaded jQuery for the lightbox, and it opens the door to ES modules and bundlers.

Creating the lightbox

// 1.x: jQuery plugin
var lightbox = $('.gallery a').simpleLightbox({ loop: false });

// 2.x: standalone build
var lightbox = new SimpleLightbox('.gallery a', { loop: false });

// 2.x: jQuery build, same call as before
var lightbox = $('.gallery a').simpleLightbox({ loop: false });

If jQuery stays on the page and you do not want to touch the start-up code, load simple-lightbox.jquery.min.js instead of the standalone file. The old call keeps working unchanged.

For bundlers there are two entry points: import SimpleLightbox from "simplelightbox" for the module with Babel transformation, and "simplelightbox/dist/simple-lightbox.esm" for the plain ES module.

Events and methods

Event names are unchanged: show.simplelightbox, closed.simplelightbox and the rest. With the standalone build you listen on the object that new SimpleLightbox() returns:

var lightbox = new SimpleLightbox('.gallery a');

lightbox.on('shown.simplelightbox', function () {
    // the image is visible now
});

open, close, next, prev, destroy and refresh are available as before. One detail is easy to miss: refresh() only works when the lightbox was created with a selector string, because it rebuilds itself from that selector.

Options added in 2.x

Existing options kept their meaning. These were added during the 2.x series and are worth a look while you migrate:

VersionOptionWhat it does
2.1.0rel, rtlGroup images by the rel attribute; right-to-left layout
2.4.0fixedClassFixed elements get right padding when the page scrollbar disappears
2.5.0fadeSpeedDuration of fades, also used for captions
2.6.0uniqueImages, focusSkip duplicate links; focus the lightbox for keyboard and tab control
2.9.0scrollZoom, scrollZoomFactorZoom with the mouse wheel
2.11.0captionClassNow accepts several classes separated by spaces or commas
2.12.0downloadText for a download link below the image

Old browsers

The standalone build targets current browsers. Sites that still have to serve Internet Explorer 11 can use simple-lightbox.legacy.min.js, available since 2.4.1. Remove any IE-specific workarounds you added around the 1.x plugin — the legacy build takes care of that.

Checklist

  1. Replace the old script and stylesheet with the 2.x files.
  2. Change $(…).simpleLightbox() to new SimpleLightbox(), or load the jQuery build.
  3. Attach event handlers to the returned object.
  4. Create the lightbox with a selector string wherever you call refresh().
  5. Test swiping, pinch zoom and closing on a phone.

The gallery below runs on 2.x with two of the newer options, fadeSpeed and scrollZoom: