Map and motion
Compare slider
Drag a handle to reveal a before image over an after image. Works with mouse, touch and keyboard.
Demo
It plays a short hint sweep once when it scrolls into view.
Structure
<div class="compare-slider" data-compare data-compare-start="50">
<img class="compare-after" src="after.jpg" alt="Describe the after image">
<img class="compare-before" src="before.jpg" alt="Describe the before image">
<span class="compare-tag compare-tag-before">Before</span>
<span class="compare-tag compare-tag-after">After</span>
<span class="compare-handle" aria-hidden="true">
<span class="compare-knob"><svg class="icon"><use href="#i-arrow-left"></use></svg><svg class="icon"><use href="#i-arrow-right"></use></svg></span>
</span>
<input class="compare-range" type="range" min="0" max="100" value="50" aria-label="Drag to compare">
</div> Put the after image first and the before image second. The before image is cropped from the right as you move the handle. Both images should have the same size and framing. Load assets/js/site.min.js (behavior 19). The slider works without extra libraries.
Instructions
- Start position:
data-compare-start="30"(0 to 100). - Shape: add
is-wide(21:9),is-squareor setstyle="--compare-ratio: 16 / 9". - Labels: change the tag text, for example Day / Dusk or Plan / Built.
- No hint sweep: add
data-compare-still. The sweep is also skipped for reduced motion. - Accessibility: the range input is the control. Keep its
aria-labeland write real alt text for both images. - Card with figures: wrap in
.cmp-cardand add.cmp-card-bodybelow the slider. Seeindex-compare.html.
Classes and attributes
| Name | Purpose |
|---|---|
.compare-slider | Wrapper. Add data-compare to activate it. |
data-compare-start | Start position in percent. Default 50. |
data-compare-still | Turns off the hint sweep. |
.compare-after / .compare-before | The two images. Before sits on top and is cropped. |
.compare-tag-before / -after | Corner labels. |
.compare-handle / .compare-knob | The divider line and round grip. |
.compare-range | Invisible range input that handles mouse, touch and keyboard. |
.is-wide / .is-square / --compare-ratio | Shape of the slider. |
.cmp-card | Card with a slider on top and a caption and figures below. |