Skip to content

Hover and keyboard image zoom in WordPress

A product photo that zooms in under the pointer, and from the keyboard with Enter and the arrow keys, to inspect the details.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

galerie-zoom-survol.php
<?php
/**
 * Plugin Name: Forge, Zoom sur la photo au survol et au clavier
 * Description: Une photo de fiche produit qui s’agrandit sous le pointeur, et au clavier avec Entrée et les flèches, pour examiner les détails. Ajoute la composition « Zoom sur la photo au survol et au clavier » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-galerie-zoom-survol
 *
pattern.html
<!-- wp:html -->
<section class="gzs" data-gzs aria-labelledby="gzs-titre">
        <div class="gzs-media">
          <button class="gzs-stage" type="button" aria-pressed="false" aria-describedby="gzs-aide" data-gzs-stage>
            <span class="gzs-img" role="img" aria-label="Théière en fonte noire, surface martelée et anse courbe" data-gzs-img style="--gzs-bg: repeating-radial-gradient(circle at 50% 50%, rgb(255 255 255 / 0.06) 0 2px, transparent 2px 7px), radial-gradient(ellipse 34% 30% at 50% 58%, #3f3f46 0 80%, #18181b 81% 92%, transparent 93%), radial-gradient(ellipse 9% 5% at 50% 30%, #27272a 0 90%, transparent), linear-gradient(115deg, transparent 0 74%, #27272a 74% 80%, transparent 80%) 0 0/100% 60% no-repeat, linear-gradient(180deg, #e7e2d8 0 72%, #c8bfae 72%)"></span>
            <span class="gzs-sr">Zoomer sur la photo</span>
          </button>
          <p class="gzs-hint" id="gzs-aide">Survolez la photo pour zoomer. Au clavier : Entrée pour zoomer, flèches pour vous déplacer, Échap pour revenir.</p>
        </div>
        <div class="gzs-info">
          <p class="gzs-eyebrow">Maison Oribe · arts de la table</p>
          <h2 class="gzs-title" id="gzs-titre">Théière Tetsubin 0,8 l</h2>
script.js
(() => {
  if (!document.querySelector(".gzs")) return;
  document.querySelectorAll("[data-gzs]").forEach((root) => {
    const on = (el, type, fn) => el.addEventListener(type, fn);
    const stage = root.querySelector("[data-gzs-stage]");
    const img = root.querySelector("[data-gzs-img]");
    let x = 50;
    let y = 50;
    const pressed = () => stage.getAttribute("aria-pressed") === "true";
    const place = () => {
      img.style.transformOrigin = `${x}% ${y}%`;
    };
style.css
:root {
  --gzs-card: #ffffff;
  --gzs-text: #1c1917;
  --gzs-muted: #57534e;
  --gzs-accent: #0f766e;
  --demo-galerie-zoom-survol-bg: #efece6;
  --demo-galerie-zoom-survol-text: #1c1917;
}

@media (prefers-color-scheme: dark) {
  :root.forge-follows-system:not([data-theme="light"]) {
    --gzs-card: #1a1917;

Free component: copy it freely, including for commercial projects.

Galleries and carousels components in WordPress
  • Building site photos sorted by dated step on a vertical timeline, with the current step highlighted.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • YouTube videos shown as thumbnails: the player only loads on click, to respect privacy and keep the page light.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A slideshow that fills the whole screen, browsed with the keyboard or a finger swipe, and closed with Escape.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Projects sorted by type of work, arranged in tabs that work with the keyboard and screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component