用 clip-path 和範圍輸入框製作無依賴的前後圖片比較滑桿
A Dependency-Free Before/After Image Slider With clip-path and a Range Input
以 clip-path 和範圍輸入框,約用 40 行 HTML、CSS 和 JavaScript 即可製作無依賴的前後圖片比較滑桿,並支援鍵盤、螢幕閲讀器及觸控操作。兩張圖片須保持相同尺寸;若 JavaScript 失效,頁面會退化為上下疊放的兩張圖片。
Before/after sliders are everywhere: photo editors, retouching portfolios, image tools. Most tutorials reach for a library, but a good one fits in about 40 lines of HTML, CSS and JavaScript using clip-path and a range input. It is accessible, works with touch, and has no dependencies.
The idea
Stack two images exactly on top of each other. Show the "after" image fully, and clip the "before" image so only its left part is visible. Moving the slider changes how much of the "before" image you see.
Markup
<div class="compare" style="--pos: 50%">
<img src="after.jpg" alt="Edited image" class="after">
<img src="before.jpg" alt="Original image" class="before">
<input type="range" min="0" max="100" value="50"
aria-label="Reveal original image" class="slider">
<span class="handle" aria-hidden="true"></span>
</div>
Using a real <input type="range"> gives us keyboard support and screen-reader semantics for free.
CSS
.compare {
position: relative;
display: inline-block;
max-width: 100%;
line-height: 0;
}
.compare img {
display: block;
width: 100%;
height: auto;
}
.compare .before {
position: absolute;
inset: 0;
clip-path: inset(0 calc(100% - var(--pos)) 0 0);
}
.compare .slider {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: ew-resize;
margin: 0;
}
.compare .handle {
position: absolute;
top: 0;
bottom: 0;
left: var(--pos);
width: 2px;
background: #fff;
box-shadow: 0 0 4px rgba(0, 0, 0, .6);
pointer-events: none;
}
clip-path: inset(top right bottom left) hides everything to the right of --pos. The range input sits invisibly on top of the whole image, so dragging anywhere moves the divider.
JavaScript
document.querySelectorAll('.compare').forEach(box => {
const slider = box.querySelector('.slider');
const update = () => box.style.setProperty('--pos', slider.value + '%');
slider.addEventListener('input', update);
update();
});
That is the whole behaviour. Everything else is CSS reading one custom property.
Making it robust
- Same dimensions. Both images must have identical width and height, or the comparison is misleading. Export them from the same source at the same size.
-
Reserve space. Add
widthandheightattributes to the<img>tags so the layout does not jump while images load. -
Focus style. The range input is invisible, so show focus on the container:
.compare:focus-within { outline: 2px solid #4c9ffe; }. - Labels. Put "Before" and "After" captions in the corners if the difference is subtle.
Where it is useful
Retouching portfolios, image compression comparisons, and tools that clean up images. For instance, a page showing before and after comparison results for corner cleanup on AI images makes the effect obvious in a way two separate thumbnails do not.
Extending it
- Vertical comparison: swap to
clip-path: inset(0 0 calc(100% - var(--pos)) 0)and rotate the slider logic. - Hover mode on desktop: update
--posfrompointermoveinstead of the range input. - Zoom: wrap both images in a container with
transform: scale()so users can inspect detail.
No library, no build step, and it degrades to two stacked images if JavaScript fails.
來源:DEV Community · dev.to