Viewer.js Image Viewer

1.11.7 · active · verified Sun Apr 19

Viewer.js is a robust JavaScript image viewer designed for web applications, currently stable at version 1.11.7. It provides a rich set of features including modal and inline viewing modes, touch gesture support, image movement, zooming, rotation, scaling (flipping), and comprehensive keyboard navigation. The library is actively maintained with frequent minor releases focusing on bug fixes and compatibility improvements. Key differentiators include its extensive API with 53 options, 23 methods, and 17 events, offering fine-grained control over the viewing experience. It ships with TypeScript type definitions, making it suitable for modern TypeScript projects, and offers various build formats (UMD, CommonJS, ES Module) to fit different project setups. While a jQuery wrapper exists, the core library is standalone and dependency-free.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Viewer.js for both a single image and a gallery of images. It shows importing the necessary CSS and the `Viewer` class, setting up basic options like `inline` mode, and utilizing event callbacks such as `viewed`.

import 'viewerjs/dist/viewer.css';
import Viewer from 'viewerjs';

// HTML structure for demonstration
document.body.innerHTML = `
  <div>
    <img id="single-image" src="https://picsum.photos/id/1018/800/600" alt="Single Picture">
  </div>
  <div>
    <ul id="image-gallery">
      <li><img src="https://picsum.photos/id/1015/300/200" alt="Picture 1"></li>
      <li><img src="https://picsum.photos/id/1016/300/200" alt="Picture 2"></li>
      <li><img src="https://picsum.photos/id/1019/300/200" alt="Picture 3"></li>
    </ul>
  </div>
`;

// View a single image in inline mode
const singleImage = document.getElementById('single-image');
if (singleImage) {
  const viewer = new Viewer(singleImage, {
    inline: true,
    viewed() {
      console.log('Single image viewed and ready!');
      viewer.zoomTo(0.8); // Zoom to 80% after viewing
    },
    ready() {
        console.log('Viewer instance for single image is ready.');
    }
  });
  // To programmatically show the viewer in modal mode:
  // viewer.show();
}

// View a list of images (gallery)
const imageGallery = document.getElementById('image-gallery');
if (imageGallery) {
  const galleryViewer = new Viewer(imageGallery, {
    url(image) {
      return image.src; // Use image 'src' attribute for full-size image
    },
    hidden() {
        console.log('Gallery viewer is hidden.');
    },
    viewed() {
        console.log('An image in the gallery was viewed.');
    }
  });
  // To programmatically show the first image in the gallery:
  // galleryViewer.show();
}

console.log('Viewer.js examples initialized.');

view raw JSON →