Skip to content

Image Magnifier

The Image Magnifier component provides an interactive way to zoom in on parts of an image. It creates a magnifying glass effect that follows the cursor, allowing users to see details of the image at a higher zoom level.

<div data-role="image-magnifier">
<img src="image.jpg" alt="Image to magnify">
</div>
// Initialize with default options
Metro.makePlugin("#element", "image-magnifier");
// Initialize with custom options
Metro.makePlugin("#element", "image-magnifier", {
lensSize: 150,
lensType: "circle",
magnifierZoom: 3,
magnifierMode: "glass"
});
ParameterTypeDefaultDescription
imageMagnifierDeferrednumber0Delay before initialization (milliseconds)
widthstring”100%“Width of the component (CSS value or percentage)
heightstring”auto”Height of the component (CSS value, percentage, or aspect ratio like “16/9”, “21/9”, “4/3”)
lensSizenumber100Size of the magnifier lens in pixels
lensTypestring”square”Shape of the lens: “square” or “circle”
magnifierZoomnumber2Zoom level for the magnified view
magnifierModestring”glass”Mode of operation: “glass” (magnification in the lens) or “zoom” (magnification in a separate element)
magnifierZoomElementstring/objectnullElement to display the zoomed image when in “zoom” mode (if null, a new element is created)
clsMagnifierstring""Additional CSS class for the magnifier container
clsLensstring""Additional CSS class for the lens element
clsZoomstring""Additional CSS class for the zoom element (when in “zoom” mode)
onMagnifierMovefunctionMetro.noopCallback function triggered when the magnifier is moved
onImageMagnifierCreatefunctionMetro.noopCallback function triggered when the component is created
  • destroy() - Removes the component and its event handlers.
const magnifier = Metro.getPlugin('#myImage', 'image-magnifier');
magnifier.destroy();
EventDescription
onMagnifierMoveTriggered when the magnifier lens is moved over the image
onImageMagnifierCreateTriggered when the component is created

The Image Magnifier component uses the following CSS classes for styling:

ElementStyleDescription
.image-magnifierposition: relative; display: block; cursor: none;Container for the magnifier
.image-magnifier imgwidth: 100%; height: auto;Image to be magnified
.image-magnifier-glassposition: absolute; border: 4px solid rgba(255,255,255,.7);Magnifier lens
/* Custom styling for a specific image magnifier */
#myImageMagnifier .image-magnifier-glass {
border: 3px solid rgba(0, 0, 0, 0.5); /* Dark border for the lens */
}
/* Custom styling for the zoom element in zoom mode */
.my-zoom-element {
border: 1px solid #ccc;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
  • .image-magnifier - Main container class for the component
  • .image-magnifier-glass - Class for the magnifier lens element

In “glass” mode, the magnified view appears directly in the lens that follows the cursor. This is useful for inline magnification without requiring additional screen space.

In “zoom” mode, the lens acts as a selection area, and the magnified view appears in a separate element. This allows for a larger magnified view and is useful when more detail is needed.

The Image Magnifier component enhances the viewing experience for users who need to see details in images. Consider the following for better accessibility:

  • Provide descriptive alt text for the image being magnified
  • Ensure sufficient color contrast for the magnifier lens border
  • Consider adding keyboard navigation options for users who cannot use a mouse
  1. Use high-quality images to ensure details are visible when magnified
  2. Choose an appropriate lens size and zoom level for your content
  3. For complex images with many details, consider using a higher zoom level
  4. In “zoom” mode, position the zoom element in a way that doesn’t obscure important content
  5. Test the component on different screen sizes to ensure it works well on all devices