Skip to content

Before and after slider in WordPress

Two stacked photos and a draggable handle to show your renovations, haircuts or gardens before and after.

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-avant-apres-curseur.php
<?php
/**
 * Plugin Name: Forge, Comparateur avant/après à curseur
 * Description: Deux photos superposées et une poignée à faire glisser pour montrer vos chantiers, coupes ou jardins avant et après. Ajoute la composition « Comparateur avant/après à curseur » à 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-avant-apres-curseur
 *
pattern.html
<!-- wp:html -->
<section class="pbac" aria-labelledby="pbac-title" data-pbac-switch>
        <div class="pbac__head">
          <div>
            <h2 id="pbac-title">Nos réalisations, avant et après</h2>
            <p>Faites glisser la poignée, ou utilisez les flèches du clavier, pour comparer l'état initial et le résultat.</p>
          </div>
          <div class="pbac__tabs" role="group" aria-label="Choisir une réalisation" data-pbac-tabs hidden>
          <button type="button" class="pbac__tab" data-pbac-show="salle-de-bains" aria-pressed="true">Salle de bains</button>
          <button type="button" class="pbac__tab" data-pbac-show="coiffure" aria-pressed="false">Coiffure</button>
          <button type="button" class="pbac__tab" data-pbac-show="jardin" aria-pressed="false">Paysagisme</button>
          </div>
script.js
(() => {
  if (!document.querySelector(".pbac")) return;
  document.querySelectorAll("[data-pbac]").forEach((root) => {
    const frame = root.querySelector("[data-pbac-frame]");
    const range = root.querySelector("[data-pbac-range]");

    function set(value) {
      const pos = Math.min(100, Math.max(0, Math.round(value)));
      range.value = String(pos);
      frame.style.setProperty("--pbac-pos", `${pos}%`);
      range.setAttribute("aria-valuetext", `Avant ${pos} %, après ${100 - pos} %`);
    }
style.css
:root {
  --pbac-text: #1c1917;
  --pbac-muted: #57534e;
  --pbac-chip: #ffffff;
  --pbac-line: #d6d3d1;
  --pbac-accent: #0f766e;
  --pbac-accent-text: #ffffff;
  --pbac-frame: #e7e5e4;
  --demo-galerie-avant-apres-curseur-bg: #fafaf9;
  --demo-galerie-avant-apres-curseur-text: #1c1917;
}

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

Galleries and carousels components 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.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • 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