Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Develop an image magnifier effect similar to e-commerce product zoom features using…

  1. User

    Develop an image magnifier effect similar to e-commerce product zoom features using HTML, CSS, and JS.

  2. Assistant
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Image Magnifier - E-commerce Zoom Effect</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          display: flex;
          justify-content: center;
          align-items: center;
          min-height: 100vh;
          margin: 0;
          background-color: #f5f5f5;
        }
    
        .magnify {
          position: relative;
          display: inline-block;
          cursor: crosshair;
        }
    
        .magnify-image img {
          display: block;
          max-width: 500px;
          height: auto;
          border-radius: 8px;
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
    
        .magnify-lens {
          position: absolute;
          display: none;
          width: 150px;
          height: 150px;
          border: 3px solid rgba(255, 255, 255, 0.9);
          border-radius: 50%;
          box-shadow: 0 0 15px rgba(0, 0, 0, 0.4);
          pointer-events: none;
          background-color: rgba(255, 255, 255, 0.1);
        }
    
        .magnify-zoom {
          position: absolute;
          display: none;
          width: 400px;
          height: 400px;
          top: 0;
          left: calc(100% + 20px);
          border: 1px solid #ddd;
          border-radius: 8px;
          background-repeat: no-repeat;
          background-position: 0 0;
          box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
          z-index: 10;
        }
    
        /* Responsive adjustments */
        @media (max-width: 900px) {
          .magnify-zoom {
            position: fixed;
            top: 20px;
            right: 20px;
            left: auto;
          }
        }
    
        /* Hide zoom on very small screens */
        @media (max-width: 600px) {
          .magnify-zoom {
            display: none !important;
          }
        }
      </style>
    </head>
    <body>
      <div class="magnify">
        <div class="magnify-image">
          <!-- Replace src with your product image URL -->
          <img id="product-image" 
               src="https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=500&h=500&fit=crop&crop=center" 
               alt="Product Image" 
               loading="lazy">
        </div>
        <div class="magnify-lens"></div>
        <div class="magnify-zoom"></div>
      </div>
    
      <script>
        const magnify = document.querySelector('.magnify');
        const img = document.getElementById('product-image');
        const lens = document.querySelector('.magnify-lens');
        const zoom = document.querySelector('.magnify-zoom');
        let ratio = 0;
        let active = false;
    
        function initZoom() {
          ratio = zoom.offsetWidth / lens.offsetWidth;
          zoom.style.backgroundImage = `url(${img.src})`;
          zoom.style.backgroundSize = `${img.offsetWidth * ratio}px ${img.offsetHeight * ratio}px`;
        }
    
        magnify.addEventListener('mouseenter', () => {
          lens.style.display = 'block';
          zoom.style.display = 'block';
          active = true;
          initZoom();
        });
    
        magnify.addEventListener('mouseleave', () => {
          lens.style.display = 'none';
          zoom.style.display = 'none';
          active = false;
        });
    
        magnify.addEventListener('mousemove', (e) => {
          if (!active) return;
    
          const rect = img.getBoundingClientRect();
          const x = e.clientX - rect.left;
          const y = e.clientY - rect.top;
    
          // Position lens centered on cursor, clamped to image bounds
          const lensHalfW = lens.offsetWidth / 2;
          const lensHalfH = lens.offsetHeight / 2;
          let lensX = x - lensHalfW;
          let lensY = y - lensHalfH;
    
          lensX = Math.max(0, Math.min(lensX, rect.width - lens.offsetWidth));
          lensY = Math.max(0, Math.min(lensY, rect.height - lens.offsetHeight));
    
          lens.style.left = `${lensX}px`;
          lens.style.top = `${lensY}px`;
    
          // Calculate background position using lens center
          const cx = lensX + lensHalfW;
          const cy = lensY + lensHalfH;
          const bgX = -(cx * ratio);
          const bgY = -(cy * ratio);
    
          zoom.style.backgroundPosition = `${bgX}px ${bgY}px`;
        });
    
        // Re-init on image load/resize (handles dynamic images)
        if (img.complete) {
          initZoom();
        } else {
          img.addEventListener('load', initZoom);
        }
    
        window.addEventListener('resize', () => {
          if (active) {
            initZoom();
          }
        });
      </script>
    </body>
    </html>
    

    Features:

    • Hover to Zoom: Move your mouse over the image to reveal a circular magnifier lens. The zoomed view appears to the right (or fixed top-right on smaller screens).
    • Smooth Tracking: The lens centers on your cursor and clamps to image bounds. The zoomed area precisely matches the lens position.
    • Customizable Magnification: ~2.5x by default (adjust width/height of .magnify-zoom and lens for different ratios).
    • Responsive: Adapts to screen size; hides zoom on mobile for better UX.
    • Performance: Uses CSS background-image and background-size/position for hardware-accelerated rendering—no canvas or extra images.
    • Plug-and-Play: Replace the src in #product-image with your image. Works with any size/aspect ratio.

    Customization Tips:

    • Magnification Level: Change zoom div size (e.g., 500px for stronger zoom).
    • Lens Style: Edit border, border-radius: 0 for square, or add blur.
    • Zoom Position: Modify .magnify-zoom top/left or use bottom: 0; right: 0;.
    • Touch Support: Add touchmove/touchstart/touchend events for mobile (similar logic).
    • Multiple Images: Duplicate the structure and init per instance.

    Copy-paste into an HTML file and open in a browser to test! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.