Yet another lightweight image magnifier plugin that helps you to create zoom image on mouseover using JavaScript. It follows the cursor position and dynamically updates the zoomed area of the image.
This simple image zoom plugin is purely built with CSS and JavaScript without any dependency. You can use this to allow users to zoom product images on hover or use it for general-purpose image zoom functionality.
How to Create Zoom Image on Mouseover using JavaScript
1. First of all, create a div element with an id “img-zoomer-box” and place your image inside it with a unique id. Likewise, create another div ( just after the image) with an id img-2 that will show the magnified image. The complete HTML structure for zoom image is as follows:
<div id="img-zoomer-box"> <img src="path/to/your-image.jpg" id="img-1" alt="Zoom Image on Mouseover"/> <div id="img-2"></div> </div>
2. After that, define the CSS style for the zoom box as follows. Place the same image as background inside the “#img-2” selector.
#img-zoomer-box { max-width: 500px; height: auto; position: relative; margin: 10px auto; } #img-1 { width: 100%; height: auto; } #img-zoomer-box:hover, #img-zoomer-box:active { cursor: zoom-in; display: block; } #img-zoomer-box:hover #img-2, #img-zoomer-box:active #img-2 { opacity: 1; } #img-2 { width: 340px; height: 340px; background: url('../img/photo.avif') no-repeat #FFF; box-shadow: 0 5px 10px -2px rgba(0,0,0,0.3); pointer-events: none; position: absolute; opacity: 0; border: 4px solid whitesmoke; z-index: 99; border-radius: 100%; display: block; transition: opacity .2s; }
3. Finally, add the following JavaScript code between the <script> tag before closing the body tag.
//Demo: https://www.codehim.com/demo/zoom-image-on-mouseover-using-javascript/ let zoomer = function (){ document.querySelector('#img-zoomer-box') .addEventListener('mousemove', function(e) { let original = document.querySelector('#img-1'), magnified = document.querySelector('#img-2'), style = magnified.style, x = e.pageX - this.offsetLeft, y = e.pageY - this.offsetTop, imgWidth = original.offsetWidth, imgHeight = original.offsetHeight, xperc = ((x/imgWidth) * 100), yperc = ((y/imgHeight) * 100); //lets user scroll past right edge of image if(x > (.01 * imgWidth)) { xperc += (.15 * xperc); }; //lets user scroll past bottom edge of image if(y >= (.01 * imgHeight)) { yperc += (.15 * yperc); }; style.backgroundPositionX = (xperc - 9) + '%'; style.backgroundPositionY = (yperc - 9) + '%'; style.left = (x - 180) + 'px'; style.top = (y - 180) + 'px'; }, false); }();
That’s all! hopefully, you have successfully integrated this zoom image viewer into your project. If you have any questions or facing any issues, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.
thank you for the above code!
I have used various alternative js and css methods previously, yet yours addresses several issues (magnification of the top image directly overlaying that of the first and the extra margin around the borders) which make the result far more intuitive
for which thank you!
Simon
Hi Simon!
Thanks for your feedback. Keep visiting us For more free web design code & scripts.