Methods
mediumZoom() returns a zoom instance. Use its methods to control the zoom, attach images, and manage listeners.
import mediumZoom from 'medium-zoom'
const zoom = mediumZoom('[data-zoomable]')open()
zoom.open(options?) opens the first attached image. Returns: Promise<Zoom>.
await zoom.open()To open a specific attached image, pass it as target:
const images = zoom.getImages()
await zoom.open({ target: images[1] })open fires when opening starts. opened fires when the animation completes. Use events to observe these transitions.
close()
zoom.close() closes the zoom. Returns: Promise<Zoom>.
await zoom.close()close fires when closing starts. closed fires when the animation completes. If you close an image while it is opening, the pending open() promise resolves and the image closes.
toggle()
zoom.toggle(options?) opens the zoom when closed and closes it when open. Returns: Promise<Zoom>.
await zoom.toggle()Like open(), toggle() accepts an attached image as target:
await zoom.toggle({ target: zoom.getImages()[1] })attach()
zoom.attach(...selectors) attaches images to the instance. Returns: the zoom instance.
After adding images to the page, attach them with a selector or collection of elements:
zoom.attach('#new-photo', 'article img')Each image is attached once, even if several selectors match it.
detach()
zoom.detach(...selectors) detaches selected images. Returns: the zoom instance.
zoom.detach('#cover')Call detach() with no arguments to detach all images:
zoom.detach()If the instance has an open image, detach() starts closing it. detach fires on each detached image. To wait for a closing animation, listen for closed.
update()
zoom.update(options) merges options into the current settings. Returns: the zoom instance.
zoom.update({ margin: 48, background: '#111113' })Coordinate objects passed as container are merged with the current container settings. update fires on each attached image.
clone()
zoom.clone(options?) creates an instance with the current options and any overrides. Returns: the new zoom instance.
Attach images to the new instance separately:
const galleryZoom = zoom.clone({ margin: 48 })
galleryZoom.attach('.gallery img')Each instance manages its own options, images, and listeners.
on()
zoom.on(type, listener, options?) registers a listener on the instance's images. Returns: the zoom instance.
zoom.on('opened', event => {
console.log('Image opened:', event.target)
})The event name is unprefixed. The third argument accepts native event listener options:
zoom.on('closed', () => console.log('Image closed'), { once: true })See Events for names, timing, and event details.
off()
zoom.off(type, listener, options?) removes a registered listener. Returns: the zoom instance.
Pass the same listener reference and capture setting used with on():
const onOpened = () => console.log('Image opened')
zoom.on('opened', onOpened)
zoom.off('opened', onOpened)getOptions()
zoom.getOptions() returns the current options.
const options = zoom.getOptions()
console.log(options.margin)getImages()
zoom.getImages() returns an array of the attached image elements.
const images = zoom.getImages()
console.log(images.length)getZoomedImage()
zoom.getZoomedImage() returns the original image currently zoomed, or null when closed.
const image = zoom.getZoomedImage()
console.log(image)