全部效果点击与切换

可拖动分栏

Resizable Split Pane

拖动分隔条重新分配两栏宽度,内容即时响应且两栏都有最小宽度。

拖动分隔条,或聚焦后按左右键

适用场景

用于平衡导航栏、编辑区和详情栏之间的空间。 同一项工作中要同时参考两块内容时,为左右面板各设一个可用的最小宽度。

应用示例

本地化编辑器并排显示原文和译文。审校者拖动分隔条,给较长的译文腾出空间,两侧同步调整,原文仍在旁边可见。

AI 提示词

给【两栏】加可拖动分隔条。以单个受限百分比为状态,在可聚焦 separator 暴露当前值,同时支持方向键、Home、End 和指针捕获。

示例代码

保存为 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;
    }

    .ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
    .ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
    .ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
    .ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
    .ui .muted { color: #cecae9; font-size: 11px; }
    .ui .center { text-align: center; }
    .ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
    .ui p { margin: 0; }
    .ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
    .ui .button:hover { background: #4e4976; }
    .ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
    .ui .primary:hover { background: #e3fff9; }
    .ui .button:disabled { opacity: .6; cursor: default; }
    .ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
    .ui .field::placeholder { color: #c2bcdd; }
    .ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
    .ui button { font: inherit; }
    .ui [hidden] { display: none !important; }
    .ui progress { accent-color: #b8ebe0; }
    @media (prefers-reduced-motion: reduce) {
      .ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
    }
    .split{display:flex;height:99px;overflow:hidden;border:1px solid #7c759f;border-radius:10px;background:#2a2545}.pane{min-width:0;padding:8px;flex:1;overflow:hidden;color:#f0e6ff}.pane:first-child{flex:0 0 calc((100% - 18px) * var(--portion,.43));background:#3d3c67}.pane i{display:block;height:3px;margin:8px 0 0;border-radius:2px;background:#a79dca;width:80%}.pane i:nth-of-type(2){width:65%}.divider{flex:0 0 18px;position:relative;cursor:col-resize;touch-action:none;background:#242037}.divider::after{content:'';position:absolute;top:35%;bottom:35%;left:7px;border-inline:2px solid #b4afd7;width:4px}
  </style>
</head>
<body>
  <main class="demo">
    <div class="ui"><div class="split"><div class="pane" id="left-pane"><strong>目录</strong><i></i><i></i><i></i></div><div class="divider" role="separator" tabindex="0" aria-valuemin="25" aria-valuemax="75" aria-valuenow="43" aria-orientation="vertical" aria-controls="left-pane" aria-label="目录宽度"></div><div class="pane"><strong>草稿</strong><i></i><i></i><i></i></div></div><p class="muted center mono" role="status">43% / 57%</p></div>
  </main>
  <script>
    function dragOn(surface, handlers) {
      let pointer = null;
      let origin = null;
      function finish(event, canceled) {
        if (event.pointerId !== pointer) return;
        const delta = { x: event.clientX - origin.x, y: event.clientY - origin.y };
        pointer = null;
        handlers.end(delta, canceled);
      }
      surface.addEventListener('pointerdown', (event) => {
        if (pointer !== null || event.button !== 0) return;
        pointer = event.pointerId;
        origin = { x: event.clientX, y: event.clientY };
        surface.setPointerCapture(pointer);
        handlers.start?.(event);
      });
      surface.addEventListener('pointermove', (event) => {
        if (event.pointerId === pointer) {
          handlers.move({ x: event.clientX - origin.x, y: event.clientY - origin.y }, event);
        }
      });
      surface.addEventListener('pointerup', (event) => finish(event, false));
      surface.addEventListener('pointercancel', (event) => finish(event, true));
      surface.addEventListener('lostpointercapture', (event) => finish(event, true));
    }
    const clamp = (value, low, high) => Math.min(high, Math.max(low, value));

    const split = document.querySelector('.split');
    const divider = split.querySelector('.divider');
    let percent = 43, start = 43;
    function set(value) { percent = clamp(Math.round(value), 25, 75); split.style.setProperty('--portion', percent / 100); divider.setAttribute('aria-valuenow', percent); document.querySelector('[role=status]').textContent = percent + '% / ' + (100 - percent) + '%'; }
    dragOn(divider, { start() { start = percent; }, move(delta) { set(start + delta.x * 100 / (split.clientWidth - divider.offsetWidth)); }, end(delta, canceled) { if (canceled) set(start); } });
    divider.addEventListener('keydown', (event) => { const changes = { ArrowLeft: percent - 3, ArrowRight: percent + 3, Home: 25, End: 75 }; if (Object.hasOwn(changes, event.key)) { event.preventDefault(); set(changes[event.key]); } });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考