Accessible Product Zoom/360° View Feature
Description:
Here’s an example of how to create an accessible product zoom/360° view feature with proper focus management, keyboard controls, and ARIA attributes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Product Zoom and 360° View</title>
<style>
/* Basic styling for layout */
.product-viewer {
position: relative;
max-width: 400px;
margin: auto;
}
.product-image {
width: 100%;
height: auto;
}
.controls {
display: flex;
justify-content: center;
margin-top: 10px;
}
.control-button {
background-color: #333;
color: #fff;
border: none;
padding: 10px;
margin: 0 5px;
font-size: 1em;
cursor: pointer;
border-radius: 5px;
}
.control-button:focus {
outline: 2px solid #005fcc;
}
/* Hide visually for screen readers */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
</style>
</head>
<body>
<section aria-labelledby="product-view-title">
<h2 id="product-view-title">Product Zoom and 360° View</h2>
<div class="product-viewer" role="region" aria-live="polite">
<img src="images/product-1.jpg" alt="Brown leather handbag - Front view" id="product-image" class="product-image">
<p id="image-description" class="sr-only">Current view: Front of the handbag</p>
</div>
<!-- Controls for zoom and rotate -->
<div class="controls" aria-label="Product view controls">
<button class="control-button" id="zoom-in" aria-label="Zoom in on product">Zoom In</button>
<button class="control-button" id="zoom-out" aria-label="Zoom out of product">Zoom Out</button>
<button class="control-button" id="rotate-left" aria-label="Rotate product left">Rotate Left</button>
<button class="control-button" id="rotate-right" aria-label="Rotate product right">Rotate Right</button>
</div>
</section>
<script>
// Image sources for different 360° views
const images = [
{ src: 'images/product-1.jpg', alt: 'Brown leather handbag - Front view', description: 'Front of the handbag' },
{ src: 'images/product-2.jpg', alt: 'Brown leather handbag - Side view', description: 'Side of the handbag' },
{ src: 'images/product-3.jpg', alt: 'Brown leather handbag - Back view', description: 'Back of the handbag' },
{ src: 'images/product-4.jpg', alt: 'Brown leather handbag - Top view', description: 'Top of the handbag' }
];
let currentImageIndex = 0;
const productImage = document.getElementById('product-image');
const imageDescription = document.getElementById('image-description');
document.getElementById('rotate-left').addEventListener('click', () => rotateImage(-1));
document.getElementById('rotate-right').addEventListener('click', () => rotateImage(1));
function rotateImage(direction) {
currentImageIndex = (currentImageIndex + direction + images.length) % images.length;
updateImage();
}
function updateImage() {
const image = images[currentImageIndex];
productImage.src = image.src;
productImage.alt = image.alt;
imageDescription.textContent = `Current view: ${image.description}`;
}
// Simple zoom functionality
let zoomLevel = 1;
document.getElementById('zoom-in').addEventListener('click', () => zoomImage(0.2));
document.getElementById('zoom-out').addEventListener('click', () => zoomImage(-0.2));
function zoomImage(step) {
zoomLevel = Math.max(1, zoomLevel + step); // Prevent zoom level below 1
productImage.style.transform = `scale(${zoomLevel})`;
}
</script>
</body>
</html>
Explanation #
- Image Rotation for 360° View:
- The
rotateImagefunction changes the product image based on the current angle of view (e.g., front, side, back, top) by updating thesrcandaltattributes. - The
aria-live="polite"attribute on the image container ensures that screen readers announce each new view description, updating users on the current view.
- The
- Descriptive
altText and Image Descriptions:- Each image has unique
alttext describing the specific angle of the product (e.g., “Front view,” “Back view”). - The
<p>element withid="image-description"provides further context, updated with each rotation to describe the current view, and hidden from visual display with the.sr-onlyclass for screen reader users.
- Each image has unique
- Zoom Functionality:
- The
zoomImagefunction adjusts the image’sscaleusing CSStransform. Zooming is limited to prevent zoom levels below 1. - Buttons for “Zoom In” and “Zoom Out” are labeled with
aria-labelattributes for clear descriptions to screen reader users.
- The
- Keyboard Navigation:
- Each control button (Zoom In, Zoom Out, Rotate Left, Rotate Right) is focusable and has a clear focus indicator.
- The buttons use descriptive
aria-labelsso screen readers can announce each control’s function clearly.
- Accessible Labels and Focus Management:
- The main section is labeled with
aria-labelledby="product-view-title", which points to the section’s heading, providing context for screen readers. - The
aria-live="polite"attribute on the image container (role="region") ensures screen readers announce updates to the view description without interrupting the user.
- The main section is labeled with
Key Takeaways #
- Use Descriptive
altText: Each view should have uniquealttext that describes the specific angle, so users with screen readers understand the context. - Provide Clear Controls with ARIA Labels: Use
aria-labelon control buttons to provide clear, descriptive text for screen reader users. - Update Live Descriptions: Use
aria-liveregions and hidden descriptions (sr-only) to provide dynamic feedback for users when the view or zoom level changes. - Keyboard-Accessible Controls: Ensure all interactive elements (Zoom In, Zoom Out, Rotate Left, Rotate Right) are focusable and accessible by keyboard.
Screen reader: NVDA
Video: