Use the React wrapper for js-image-zoom
masterFor React applications, use the dedicated wrapper package react-image-zoom instead of the vanilla JavaScript library.
npm install react-image-zoomrepository·master·Indexed 42 days ago
https://github.com/malaman/js-image-zoomA pure JavaScript utility for desktop browsers that enables image zooming on mouse hover. Version 0.6.0 provides a dependency-free ImageZoom class with configurable options for scale, position, and custom styling, as well as setup() and kill() methods for lifecycle management.
For React applications, use the dedicated wrapper package react-image-zoom instead of the vanilla JavaScript library.
npm install react-image-zoomTo add js-image-zoom to your project, install it using npm:
npm install js-image-zoom --saveTo use js-image-zoom, create a new instance of ImageZoom by passing a container DOM element and an optional configuration object. The library provides hover-based zoom functionality for desktop browsers without external dependencies.
If the image dimensions are unknown or you want the image to adapt to its parent, use the fillContainer: true option.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF8">
<title>Title</title>
<script src="https://unpkg.com/js-image-zoom@0.7.0/js-image-zoom.js" type="application/javascript"></script>
</head>
<body>
<div id="img-container" style="width: 400px">
<img src="https://github.com/malaman/js-image-zoom/blob/master/example/1.jpg?raw=true" />
</div>
<script>
var options2 = {
fillContainer: true,
offset: {vertical: 0, horizontal: 10}
};
new ImageZoom(document.getElementById("img-container"), options2);
</script>
</body>
</html>The ImageZoom constructor accepts two arguments:
| Option | Type | Description |
|---|---|---|
width | number | Width of the source image (optional) |
height | number | Height of the source image (optional) |
fillContainer | boolean | If true, takes the size of the container (useful if dimensions are unknown) (optional) |
zoomWidth | number | Width of the zoomed image. Height is automatically set to match source image height (optional) |
img | string | URL of the source image. Use this if the container does not contain an <img> tag (optional) |
scale | number | Zoom scale. If not provided, scale is calculated as natural image size / image size (optional if zoomWidth is provided) |
offset | object | {vertical: number, horizontal: number}. Defines the offset of the zoomed image (optional) |
zoomContainer | node | DOM node where the zoomedImage will be appended (defaults to the image's container) |
zoomStyle | string | Custom CSS style applied to the zoomed image (e.g., 'opacity: 0.1; background-color: white;') |
zoomPosition | string | Position of the zoomed image. Options: top, left, bottom, original, or right (default) |
zoomLensStyle | string | Custom CSS style applied to the zoom lens (e.g., 'opacity: 0.1; background-color: white;') |
The ImageZoom constructor returns an object containing methods to control the lifecycle of the zoom instance:
setup(): Re-initializes the zoom functionality.kill(): Removes event listeners and cleans up DOM nodes (the lens and the zoomed image) created by the library.const zoom = ImageZoom(container, options);
// Later, when you want to remove the zoom effect and clean up the DOM
zoom.kill();