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.

After Before BeforeAfter
After Before DayDusk

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

Classes and attributes

NamePurpose
.compare-sliderWrapper. Add data-compare to activate it.
data-compare-startStart position in percent. Default 50.
data-compare-stillTurns off the hint sweep.
.compare-after / .compare-beforeThe two images. Before sits on top and is cropped.
.compare-tag-before / -afterCorner labels.
.compare-handle / .compare-knobThe divider line and round grip.
.compare-rangeInvisible range input that handles mouse, touch and keyboard.
.is-wide / .is-square / --compare-ratioShape of the slider.
.cmp-cardCard with a slider on top and a caption and figures below.