0. What We Are Building
A product gallery is the image area you see on e-commerce websites. It usually has small thumbnails, one large selected image, and a zoom feature that helps buyers inspect details.
The zoom effect is not magic. We show the same image as a background inside a lens and a result box. Then we move the background in the opposite direction of the pointer.
1. HTML Structure
HTML gives the gallery its skeleton. The important parts are:
.thumbs— contains thumbnail buttons.#ppMainImg— the main selected image.#ppLens— the circular magnifying lens.#ppResult— the large zoom result box.#ppPrev,#ppNext, and#ppFullscreen— control buttons.#ppZoom— the zoom power slider.
<div class="thumbs">
<button data-full="image-1.jpg">
<img src="image-1.jpg" alt="Thumbnail 1">
</button>
</div>
<img id="ppMainImg" src="image-1.jpg" alt="Selected product image">
<div id="ppLens"></div>
<div id="ppResult"></div>
Save the file as index.html and open it in your browser.
You should see a product gallery page with a lesson, thumbnail images, and a large main image.
2. CSS Design
CSS controls the visual design. In this lesson, we use a light saffron theme, rounded cards, shadows, accessible code colors, and responsive layout.
The main trick
The lens and zoom result are both hidden at first:
.magnify-lens,
.magnify-result {
display: none;
background-repeat: no-repeat;
}
When the user hovers or presses the image, JavaScript changes them to
display: block.
Why the container is position relative
.magnify-container {
position: relative;
overflow: hidden;
}
The lens is placed absolutely inside the image container. That means the lens can follow the pointer while staying inside the image boundary.
3. JavaScript Logic
JavaScript gives life to the gallery. The script does five big jobs:
- Collect all required elements using
document.getElementById. - Switch the main image when a thumbnail is clicked.
- Move the magnifying lens according to pointer position.
- Calculate the zoom background position.
- Support keyboard, touch, zoom slider, and fullscreen.
Image switching
function setActive(index) {
activeIndex = (index + thumbs.length) % thumbs.length;
const full = thumbs[activeIndex].dataset.full;
img.src = full;
}
The most important zoom formula
const scaleX = img.naturalWidth / rect.width;
const scaleY = img.naturalHeight / rect.height;
const natX = x * scaleX;
const natY = y * scaleY;
const bgPosX = -(natX * zoom);
const bgPosY = -(natY * zoom);
The browser displays the image at one size, but the original image may be bigger.
naturalWidth and naturalHeight help us map the visible image
position to the real image position.
4. Live Demo
Hover · Touch · Keyboard · FullscreenTry the gallery below. On desktop, hover over the main image. On mobile, press and drag on the image. Use the zoom slider to increase or decrease magnification.
Programmer’s Picnic — Product Gallery
Hover on desktop or press-and-drag on mobile to magnify. Use ← / → keys to change images.
5. Code Map
Part A: Data source
Each thumbnail button stores the large image URL in data-full.
<button data-full="https://example.com/product.jpg">
<img src="https://example.com/product.jpg" alt="Product thumbnail">
</button>
Part B: Selected state
The selected thumbnail uses aria-selected="true". This improves both design and accessibility.
Part C: Pointer math
The pointer gives us clientX and clientY. We subtract the image’s left and top
position to get the pointer location inside the image.
const rect = img.getBoundingClientRect();
const cx = e.clientX - rect.left;
const cy = e.clientY - rect.top;
Part D: Boundary control
We clamp the lens position so it cannot move outside the image.
x = Math.max(0, Math.min(x, rect.width - lensW));
y = Math.max(0, Math.min(y, rect.height - lensH));
6. Checkpoints
Open index.html. You should see the lesson title, saffron layout,
and the product gallery demo.
Click each thumbnail. The main image should change, and the selected thumbnail should get a saffron border.
Hover on the main image using a desktop browser. A circular lens and a zoom result box should appear.
Move the zoom slider. The zoom value should change from 2× to 6×.
Press the left and right arrow keys. The gallery should move to previous and next images.
Click Fullscreen. The image area should enter fullscreen mode if the browser allows it.
7. Experiments
Replace the four image URLs with your own product photos.
Change the lens size from 120px to 160px.
Change the zoom slider maximum from 6 to 10.
Add product title, price, rating, and an affiliate button beside the gallery.
For a real e-commerce page, use high-resolution images. If the original image is small, zooming will not look sharp.
8. Infinity Improvements
Once the basic gallery works, you can extend it into a production-level component.
- Load images from a JSON file.
- Add product variants like color, size, and model.
- Add lazy loading for large product collections.
- Add swipe gestures for mobile users.
- Add a modal gallery with next, previous, and close buttons.
- Add structured data for better product SEO.
- Add analytics to track which image users inspect most.
- Turn the gallery into a reusable Web Component.
A professional gallery is a combination of simple HTML, thoughtful CSS, and precise JavaScript coordinate calculations. Once you understand these three parts, you can build galleries for products, portfolios, courses, travel pages, and affiliate websites.