Zoom Image on Mouseover using JavaScript

Zoom Image on Mouseover using JavaScript
Code Snippet:Magnify image zoom with vanilla Javascript and CSS
Author: Rich
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 21,131
License: MIT
Edit Code online: View on CodePen
Read More

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.

2 thoughts on “Zoom Image on Mouseover using JavaScript”

  1. 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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X