About this snippet

Allow customers to see product details clearly with this zoom effect. The script waits for the image to load, then creates a lens that reveals a high-resolution version of the photo when you hover over it.

Size
3.0 KB
Uses
HTML, CSS, JavaScript
Loads
placehold.co
Views
527
<div class="img-magnifier-container">
  <img id="myimage" src="https://placehold.co/600x400/10b981/ffffff?text=Product+Zoom" alt="Product">
</div>
.img-magnifier-container {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  position: relative;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}
#myimage {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
  cursor: crosshair;
}
.img-magnifier-glass {
  position: absolute;
  border: 3px solid #fff;
  border-radius: 50%;
  cursor: none;
  width: 100px;
  height: 100px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.3);
  display: none;
  background-repeat: no-repeat;
  background-color: #fff;
  pointer-events: none;
  z-index: 10;
}
function magnify(imgID, zoom) {
  const img = document.getElementById(imgID);
  const glass = document.createElement("DIV");
  glass.setAttribute("class", "img-magnifier-glass");
  img.parentElement.insertBefore(glass, img);

  const initMagnifier = () => {
    // Using unquoted URL to prevent SQL escaping errors. Safe because img.src is URL encoded.
    glass.style.backgroundImage = "url(" + img.src + ")";
    glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px";
    
    const bw = 3;
    const w = glass.offsetWidth / 2;
    const h = glass.offsetHeight / 2;

    // Mouse Events
    glass.addEventListener("mousemove", moveMagnifier);
    img.addEventListener("mousemove", moveMagnifier);
    img.addEventListener("mouseenter", () => glass.style.display = "block");
    img.addEventListener("mouseleave", () => glass.style.display = "none");

    // Touch Events for Mobile
    glass.addEventListener("touchmove", moveMagnifier);
    img.addEventListener("touchmove", moveMagnifier);
    img.addEventListener("touchstart", () => glass.style.display = "block");
    img.addEventListener("touchend", () => glass.style.display = "none");

    function moveMagnifier(e) {
      e.preventDefault();
      const pos = getCursorPos(e);
      let x = pos.x;
      let y = pos.y;
      
      if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);}
      if (x < w / zoom) {x = w / zoom;}
      if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
      if (y < h / zoom) {y = h / zoom;}
      
      glass.style.left = (x - w) + "px";
      glass.style.top = (y - h) + "px";
      glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
    }

    function getCursorPos(e) {
      let a, x = 0, y = 0;
      e = e || window.event;
      a = img.getBoundingClientRect();
      
      // Handle touch or mouse
      const pageX = e.pageX || e.touches[0].pageX;
      const pageY = e.pageY || e.touches[0].pageY;

      // Calculate cursor position relative to the image
      x = pageX - a.left - window.scrollX;
      y = pageY - a.top - window.scrollY;
      return {x : x, y : y};
    }
  };

  if (img.complete) {
    initMagnifier();
  } else {
    img.addEventListener("load", initMagnifier);
  }
}

magnify("myimage", 2);
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save