双轴滚动小地图
Two-Axis Scroll Minimap
概览上的视窗框同步反映横纵滚动位置与当前可见区域比例。
适用场景
适合在画布编辑器、大型表格或空间看板中帮助定位。 缩略图应与完整工作区的方向和比例对应,并清晰标出当前视口。普通滚动和键盘移动也要可用。
应用示例
楼层平面图编辑器里,用户向右下方移动画布后,小地图的矩形框也移向右下角。通过这个框,用户可以判断自己在看大堂还是远处的一角。
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;
}
.map-shell { position:relative; contain:inline-size; width:min(100%,290px); height:clamp(112px,75vh,230px); }
.map-viewport { width:100%; height:100%; overflow:scroll; scrollbar-width:thin; scrollbar-color:#a592e1 #192540; overscroll-behavior:contain; border:1px solid #917bdb9e; border-radius:10px; }
.map-picture { position:relative; width:540px; height:375px; background:radial-gradient(ellipse at 65% 56%,#599486 0 12%, transparent 12.5%),radial-gradient(ellipse at 34% 40%,#356177 0 18%,transparent 18.5%),repeating-linear-gradient(0deg,transparent 0 27px,#9dc4cf34 28px 29px),repeating-linear-gradient(90deg,transparent 0 27px,#a5bdca35 28px 29px),#263653; }
.map-picture span { position:absolute; left:var(--x); top:var(--y); color:#fff; background:#64599d; width:23px; aspect-ratio:1; border:1px solid #c6b8ed; border-radius:50%; display:grid; place-items:center; font-size:11px; }
.map-overview { width:62px; aspect-ratio:1.44; position:absolute; right:13px; bottom:13px; pointer-events:none; border:1px solid #adc7d8; border-radius:5px; background:#344e6c; color:#afebe1; }
.map-overview i { position:absolute; width:var(--vw,35%); height:var(--vh,35%); left:var(--px,0%); top:var(--py,0%); border:1px solid currentColor; background:#9fe7d935; }
</style>
</head>
<body>
<main class="demo">
<div class="lab map-shell"><div class="map-viewport" role="region" tabindex="0" aria-label="向两个方向滚动地图"><div class="map-picture" aria-hidden="true"><span style="--x:9%;--y:14%">A</span><span style="--x:70%;--y:17%">B</span><span style="--x:23%;--y:74%">C</span><span style="--x:76%;--y:68%">D</span></div></div><div class="map-overview" aria-hidden="true"><i></i></div></div>
</main>
<script>
const map = document.querySelector('.map-viewport');
const overview = document.querySelector('.map-overview');
function updateMap() {
const width = map.scrollWidth || 1, height = map.scrollHeight || 1;
overview.style.setProperty('--vw', (map.clientWidth / width * 100) + '%');
overview.style.setProperty('--vh', (map.clientHeight / height * 100) + '%');
overview.style.setProperty('--px', (map.scrollLeft / width * 100) + '%');
overview.style.setProperty('--py', (map.scrollTop / height * 100) + '%');
}
map.addEventListener('scroll',updateMap,{passive:true});
new ResizeObserver(updateMap).observe(map);
updateMap();
</script>
</body>
</html>UI Skills 原创示例