前后对比滑块
Before/After Slider
拖动分界线,改变两张对齐画面的可见比例。
适用场景
直观比较修图、修复或显示设置前后的变化。 先把两张图的角度、裁切和尺寸对齐,拖动时才能把差异看清。
应用示例
照片修复案例把原扫描件与修复后的图叠在一起。把分隔线拖过人物脸部,就能逐处对照哪些划痕已经被修掉。
AI 提示词
在【画框】内叠放对齐的前后画面。用原生范围输入同步裁切位置和分界线,标明两种状态并支持方向键和触屏。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>前后对比滑块</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;
}
.example { width: min(100%, 228px); position: relative; display: grid; gap: 10px; }
.panel { background: #292240; border: 1px solid #675785; border-radius: 11px; padding: 10px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.label { margin: 0; font-size: 13px; font-weight: 650; }
.detail, .caption { margin: 0; color: #d7cbea; font-size: 11px; line-height: 1.35; }
.caption { text-align: center; }
.control { border: 1px solid #8874ae; background: #392e57; border-radius: 7px; padding: 5px 9px; min-height: 32px; color: #f6efff; cursor: pointer; font-size: 11px; font-weight: 650; }
.control:hover { background: #514174; }
.control.primary { color: #261b3f; background: #d1bfff; border-color: #e3d8ff; }
.control.primary:hover { background: #e3d8ff; }
.control:disabled { opacity: .55; cursor: default; }
.field { width: 100%; min-width: 0; padding: 6px 8px; border: 1px solid #875fa8; border-radius: 6px; color: #f6efff; background: #1e1732; font-size: 12px; }
.field::placeholder { color: #c2abd8; }
[hidden] { display: none !important; }
.comparison { --split: 50%; position: relative; height: 84px; overflow: hidden; border-radius: 8px; background: #4b3c68; }
.mountain { position: absolute; inset: 0; background: radial-gradient(circle at 67% 30%, #ffd5a1 0 13px, transparent 14px), linear-gradient(#58599b, #bc8cba); }
.mountain::before, .mountain::after { content: ''; position: absolute; inset: 0; clip-path: polygon(0 100%, 31% 24%, 48% 65%, 63% 44%, 100% 100%); background: #8ce0b6; }
.mountain::after { clip-path: polygon(0 100%, 25% 61%, 51% 100%, 70% 64%, 100% 100%); background: #388e80; }
.monochrome { filter: grayscale(1); }
.color { clip-path: inset(0 0 0 var(--split)); }
.divider { position: absolute; inset: 0 auto 0 var(--split); width: 2px; background: #fff; transform: translateX(-50%); }
.comparison-range { display: block; width: 100%; margin: 4px 0 0; accent-color: #d1bfff; }
@media (prefers-reduced-motion: reduce) {
.example *, .example *::before, .example *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
</style>
</head>
<body>
<main class="demo">
<div class="example"><div class="comparison" aria-hidden="true"><div class="mountain monochrome"></div><div class="mountain color"></div><div class="divider"></div></div><label class="caption">调整前后对比<input class="comparison-range" type="range" min="0" max="100" value="50"></label></div>
</main>
<script>
const image = document.querySelector('.comparison');
const range = document.querySelector('.comparison-range');
range.addEventListener('input', () => image.style.setProperty('--split', range.value + '%'));
</script>
</body>
</html>UI Skills 原创示例