Scroll Comparison Seam
滚动驱动版本对比分界
Vertical scroll moves a straight seam between two treatments of a single image.
Where to use it
Compare two grades or design treatments during a guided case study. Align both versions precisely and choose the seam's direction based on the details readers need to compare.
Practical example
A photography tutorial uses one street portrait to compare warm and neutral grades. As the reader scrolls, a straight seam moves across the face and jacket, revealing the same pixels in the alternate grade.
AI prompt
Add Scroll Comparison Seam to [target]. Keep both versions fixed and aligned. Map scroll progress to the upper layer’s inset clip and move the divider by the same value. Support ordinary scrolling in both directions. Keep the controls keyboard accessible and provide a reduced-motion state.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scroll Comparison Seam</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.scroll-port {
--frame: clamp(112px, 75vh, 242px);
--p: 0;
width: min(100%, 290px); height: var(--frame);
overflow-y:auto; overflow-x:hidden; scroll-behavior:auto;
overscroll-behavior:contain; scrollbar-width:thin;
scrollbar-color:#a79bd5 #20223d; border-radius:12px;
border:1px solid #a79bd567; background:#20233c; color:#f5f2ff;
}
.scroll-port:focus-visible { outline-offset: 3px; }
.scroll-track { position:relative; min-height:calc(var(--frame) + 235px); }
.scroll-pin { position:sticky; top:0; height:var(--frame); overflow:hidden; }
.scroll-pin .tag { position:absolute; z-index:4; left:8px; bottom:9px; font-size:10px; color:#ecfcff; padding:4px 6px; background:#17152ce8; border-radius:4px; }
.scroll-panel { min-height:62px; padding:9px; border:1px solid #adb2e345; border-radius:8px; background:linear-gradient(120deg,#314861,#3e346a); }
.scene-art {
position: absolute; inset: 0;
background:
radial-gradient(circle at 68% 33%, var(--sun, #ffdbac) 0 11%, transparent 11.8%),
linear-gradient(26deg, var(--land, #435b77) 0 28%, transparent 28.6%),
linear-gradient(-33deg, var(--ridge, #736ca9) 0 32%, transparent 32.5%),
linear-gradient(145deg, var(--horizon, #151b48), var(--sky, #7c729a));
}
.scene-art::after {
position: absolute; inset: 0; content: '';
background-image: repeating-linear-gradient(0deg, #fff0 0 10px, #ccceff20 11px, #fff0 12px);
mask-image: linear-gradient(transparent 45%, black 100%);
}
.comparison-top { --sun:#fae3e4; --ridge:#a16b81; --land:#624c68; --horizon:#391a41; --sky:#ac6678; clip-path:inset(0 calc(100% - (8% + var(--p) * 84%)) 0 0); }
.comparison-seam { position:absolute; inset-block:0; left:calc(8% + var(--p) * 84%); width:2px; background:#fff; box-shadow:0 0 6px #eedee8; }
.comparison-seam::after { position:absolute; content:'↔'; top:50%; left:50%; transform:translate(-50%,-50%); display:grid; place-items:center; width:21px; aspect-ratio:1; border-radius:50%; background:white; color:#1e2547; font-size:13px; }
@media (prefers-reduced-motion:reduce) { .scroll-pin { --p:.5; } }
@media (prefers-reduced-motion: reduce) {
.lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
animation: none !important;
transition: none !important;
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main class="demo">
<div class="lab scroll-port" role="region" tabindex="0" aria-label="Scroll to move the comparison seam"><div class="scroll-track"><div class="scroll-pin">
<div class="scene-art" aria-hidden="true"></div><div class="scene-art comparison-top" aria-hidden="true"></div><i class="comparison-seam" aria-hidden="true"></i><span class="tag" aria-hidden="true">Compare treatments</span></div></div></div>
</main>
<script>
const port = document.querySelector('.scroll-port');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const listeners = [];
let scheduled = 0;
function sample() {
const max = Math.max(1,port.scrollHeight - port.clientHeight);
return Math.max(0,Math.min(1,port.scrollTop / max));
}
function update() {
scheduled = 0;
const progress = sample();
port.style.setProperty('--p', progress.toFixed(4));
for (const callback of listeners) callback(progress);
}
function schedule() { if (!scheduled) scheduled = requestAnimationFrame(update); }
function onScroll(callback) { listeners.push(callback); callback(sample()); }
port.addEventListener('scroll', schedule, {passive:true});
new ResizeObserver(schedule).observe(port);
reduced.addEventListener('change', schedule);
update();
</script>
</body>
</html>Original example by UI Skills