🌼 Programmer’s Picnic Lesson

Build an Amazon-Style Image Magnifier (HTML + CSS + JavaScript)

“We don’t copy code. We understand it, then make it our own.”
— Programmer’s Picnic ☕🌿
Magnifier Demo Image
Product Gallery

🎯 What You Will Learn in This Lesson

By the end of this lesson, you will be able to:

This lesson uses a light saffron theme — calm, readable, and distraction-free.


🧠 Concept First: How Image Magnification Really Works

Before writing code, let’s understand the idea.

The magnifier is made of three parts

  1. Small Image
    This is the normal image the user sees.
  2. Lens (Zoom Glass)
    • Follows the mouse
    • Shows a zoomed portion using background-image
  3. Result Box (Preview Window)
    • Shows the same zoomed area
    • At a bigger size for clarity

👉 Important insight:
We are not enlarging the image element.
We are enlarging the background image and shifting it.

This is the same trick Amazon uses.


🧩 Step 1: Create the HTML Structure

We start with simple HTML.

<div class="magnify-wrapper">
  <div class="magnify-container" id="demoContainer">
    <img id="demoImg" class="magnify-small"
         src="YOUR_IMAGE_URL"
         alt="Product Image">
    <div id="demoLens" class="magnify-lens"></div>
  </div>

  <div id="demoResult" class="magnify-result"></div>
</div>

Why this structure?

Think of it as:

Image → Lens → Preview


🎨 Step 2: Style It (Light Saffron Friendly)

Now we add CSS to make it usable and beautiful.

Key ideas in CSS

.magnify-container {
  position: relative;
  width: 300px;
}

.magnify-lens {
  position: absolute;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: none;
  background-repeat: no-repeat;
}

.magnify-result {
  width: 380px;
  height: 380px;
  display: none;
  background-repeat: no-repeat;
}
💡 Teaching Tip:
If you remove border-radius: 50%, the lens becomes square.

⚙️ Step 3: JavaScript — Making the Magic Happen

Step 3.1 — Setup

const img = document.getElementById("demoImg");
const lens = document.getElementById("demoLens");
const result = document.getElementById("demoResult");
const container = document.getElementById("demoContainer");

const zoom = 3;

Step 3.2 — Initialize on Image Load

function init() {
  lens.style.backgroundImage = `url(${img.src})`;
  result.style.backgroundImage = `url(${img.src})`;

  container.addEventListener("mouseenter", show);
  container.addEventListener("mouseleave", hide);
  container.addEventListener("mousemove", moveLens);
}

👉 We wait for image load because we need correct width and height for zoom math.


Step 3.3 — Track Mouse & Clamp the Lens

let x = e.clientX - rect.left - lens.offsetWidth / 2;
let y = e.clientY - rect.top - lens.offsetHeight / 2;

Meaning:

if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > img.width - lens.offsetWidth) x = img.width - lens.offsetWidth;
if (y > img.height - lens.offsetHeight) y = img.height - lens.offsetHeight;

This prevents the lens from going outside the image.


Step 3.4 — Zoom Using Background Math

const bgW = img.width * zoom;
const bgH = img.height * zoom;

lens.style.backgroundSize = `${bgW}px ${bgH}px`;
result.style.backgroundSize = `${bgW}px ${bgH}px`;

lens.style.backgroundPosition = `-${x * zoom}px -${y * zoom}px`;
result.style.backgroundPosition = `-${x * zoom}px -${y * zoom}px`;

Key Insight:
We move the background opposite to the mouse so the correct area appears magnified.


🛠️ Things You Can Now Modify (Homework)


🧘 Final Thought (Programmer’s Picnic)

This lesson teaches more than a feature.

This is front-end engineering, not decoration.

☕🌿 – Programmer’s Picnic