Skip to content
On this page

Events

Fancybox exposes custom events that can be hooked on to. To set event listeners, use the on option when initializing Fancybox, or you can use the on() API method.

The first argument each listener receives is a reference to the current Fancybox instance. Use it to retrieve references to DOM elements and a lot of other useful information.

Some events pass the slide object as the second parameter, you can use it to get a reference to the slide wrapping element, content element, etc.

js
Fancybox.bind("[data-fancybox]", {
  on: {
    reveal: (fancybox, slide) => {
      // The content of this slide is loaded and ready to be revealed
      slide.contentEl.style.filter = "brightness(1.25) contrast(1.25)";
    },
  },
});

JSFiddle Example

Note that since the next and previous slides are preloaded by default, some events may be called multiple times, for each slide:

js
Fancybox.bind("[data-fancybox]", {
  on: {
    done: (fancybox, slide) => {
      if (fancybox.isCurrentSlide(slide)) {
        console.log(
          `The content of the current slide #${slide.index} is loaded and revealed`
        );
      } else {
        console.log(
          `The content of the slide #${slide.index} is preloaded and revealed`
        );
      }
    },
  },
});

JSFiddle Example

Available Events

NameDescription
*Any event
Carousel.*Any Carousel event
initInitialization has started, plugins have been added
initLayoutThe layout is initialized
initCarouselThe Carousel is initialized
readyInitialization has been completed
loadingThe content on one of the slides starts loading
loadedContent is loaded on one of the slides but is not yet displayed
errorContent could not be loaded in one of the slides
revealContent is ready to be displayed on one of the slides
doneContent is revealed on one of the slides
setIdleThe idle state is activated
endIdleThe idle state is deactivated
endSlideshowThe slideshow has been deactivated
keydownA keyboard button is pressed
clickSingle click event has been detected
wheelWheel event has been detected
resizeA window resizing event was detected
shouldCloseClosing has begun and can be prevented
startSlideshowThe slideshow is activated
closeClosing is ongoing
destroyInstance is detroyed

Tips and Tricks

If you use * as the name of the event, you can listen to any event. this way you can quickly check what events are available. Here is an example:

js
Fancybox.bind("[data-fancybox]", {
  on: {
    "*": (fancybox, eventName) => {
      console.log(`Fancybox eventName: ${eventName}`);
    },
  },
});

JSFiddle Example

A fairly common need is to do something when a Fancybox starts or when a carousel item changes. For example, you might want to get information from the current trigger element to update an element on the page. You can use Carousel.ready and Carousel.change events for this:

js
Fancybox.bind("[data-fancybox]", {
  on: {
    "Carousel.ready Carousel.change": (fancybox) => {
      // Current slide
      const slide = fancybox.getSlide();

      // The corresponding trigger element
      // (i.e. the link that is clicked to launch Fancybox)
      const triggerEl = slide.triggerEl;

      // Data from the trigger element
      const title = triggerEl.dataset.title;

      alert(title);
    },
  },
});

JSFiddle Example

Here's a simple example that shows how to get a reference to the active slide and call the Panzoom method. An instance of Panzoom is created by the Image plugin for each slide.

js
Fancybox.bind("[data-fancybox]", {
  on: {
    keydown: (fancybox, key) => {
      switch (key) {
        case "+":
          fancybox.getSlide()?.panzoom.zoomIn();
          break;

        case "-":
          fancybox.getSlide()?.panzoom.zoomOut();
          break;
      }
    },
  },
});

JSFiddle Example