Medium Zoom

1.1.0 · active · verified Sun Apr 19

Medium Zoom is a lightweight, responsive JavaScript library designed to replicate the elegant image zooming experience found on the Medium platform. Currently stable at version 1.1.0, the package sees active development with regular patch releases addressing bugs and minor enhancements, building upon its significant 1.0.0 refactor. Key differentiators include its high performance (aiming for 60fps), support for high-definition image loading on zoom, extensive customization options for margin, background, and scroll offset, and a pluggable architecture allowing for custom features and templates. It is framework-agnostic, working seamlessly across React, Vue, Angular, Svelte, and Solid, and offers comprehensive event handling for various zoom states. The library focuses on providing a smooth, intuitive user experience that is friendly to mouse, keyboard, and touch gestures.

Common errors

Warnings

Install

Imports

Quickstart

This code initializes `mediumZoom` on elements matching a CSS selector, demonstrates attaching additional elements, sets custom options, and logs events during the zoom lifecycle. It also includes the necessary CSS import.

import mediumZoom from 'medium-zoom';
import 'medium-zoom/dist/medium-zoom.css';

document.addEventListener('DOMContentLoaded', () => {
  // Initialize mediumZoom on all images with the class 'zoomable-image'
  const zoom = mediumZoom('.zoomable-image', {
    margin: 24, // Space between the image and the edge of the viewport
    background: 'rgba(0, 0, 0, 0.8)', // Dark overlay background
    scrollOffset: 40 // Scroll offset to close the zoom
  });

  // Attach additional images or selectors dynamically
  zoom.attach('#another-zoomable-image');

  // Listen for events: 'open', 'opened', 'close', 'closed'
  zoom.on('open', (event) => {
    console.log('Zoom effect starting for:', event.target);
    // You can update options after the zoom is opened
    zoom.update({ background: 'rgba(25, 18, 25, 0.95)' });
  });

  zoom.on('closed', () => {
    console.log('Zoom effect finished closing.');
  });

  // Example of programmatic open (returns a Promise since v1.0.0)
  // document.getElementById('open-btn')?.addEventListener('click', async () => {
  //   await zoom.open(document.querySelector('.zoomable-image'));
  //   console.log('Programmatic zoom opened.');
  // });
});

// To make this code runnable, ensure your HTML contains:
// <img class="zoomable-image" src="https://picsum.photos/id/1018/400/300" alt="Forest road" />
// <img id="another-zoomable-image" src="https://picsum.photos/id/1025/400/300" alt="Pug dog" />

view raw JSON →