全部效果点击与切换

底部面板停靠点

Bottom Sheet Detents

底部面板跟随拖动,在几个预设高度停靠,内容随展开状态改变。

上下拖动把手,也可以用 Tab 找到高度按钮

适用场景

在保留地图或主页面的同时展示可展开筛选和详情。 低档位置适合扫一眼摘要,高档位置可以容纳需要浏览或操作的内容。

应用示例

徒步地图上点一个起点,底部面板先停在低档,只显示名称和距离。向上拖把手,松手后面板停在高档,露出交通指引和路线说明。

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;
    }

    .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; }
    }
    .sheet-viewport{height:146px;border-radius:10px;overflow:hidden;position:relative;background:#243650}.map{position:absolute;inset:0;background:repeating-linear-gradient(28deg,#49bdb629 0 2px,transparent 2px 20px),repeating-linear-gradient(-48deg,#8cafed40 0 1px,transparent 1px 22px),linear-gradient(130deg,#223752,#2f5461)}.sheet{position:absolute;bottom:0;width:100%;height:94px;display:grid;align-content:start;gap:5px;overflow:hidden;transition:height .22s ease-out}.sheet[data-drag]{transition:none}.handle{justify-self:center;width:62px;height:17px;border:0;border-radius:4px;background:linear-gradient(#a5a0c8,#a5a0c8) center/27px 4px no-repeat;cursor:ns-resize;touch-action:none}.sheet [aria-pressed=true]{background:#b8ebe0;color:#201e37}
  </style>
</head>
<body>
  <main class="demo">
    <div class="ui"><div class="sheet-viewport"><div class="map" aria-hidden="true"></div><div class="surface sheet"><button class="handle" type="button" aria-label="切换面板高度"></button><div class="bar"><strong>工作室筛选</strong><span class="mono" role="status">1 / 3</span></div><div class="actions"><button class="button" data-stop="0" aria-pressed="true">低位</button><button class="button" data-stop="1" aria-pressed="false">半屏</button><button class="button" data-stop="2" aria-pressed="false">展开</button></div><p class="muted">河边附近有 2 处地点</p></div></div></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 sheet = document.querySelector('.sheet');
    const handle = sheet.querySelector('.handle');
    const heights = [94, 118, 144];
    let index = 0;
    let dragged = false;
    function setStop(stop) {
      index = stop; sheet.style.height = heights[index] + 'px';
      sheet.querySelector('.muted').hidden = index === 0;
      sheet.querySelector('[role=status]').textContent = (index + 1) + ' / 3';
      for (const option of sheet.querySelectorAll('[data-stop]')) option.setAttribute('aria-pressed', String(Number(option.dataset.stop) === index));
    }
    setStop(index);
    for (const option of sheet.querySelectorAll('[data-stop]')) option.addEventListener('click', () => setStop(Number(option.dataset.stop)));
    handle.addEventListener('click', () => { if (!dragged) setStop((index + 1) % heights.length); dragged = false; });
    dragOn(handle, {
      start() { dragged = false; sheet.setAttribute('data-drag', ''); },
      move(delta) { dragged ||= Math.abs(delta.y) > 4; sheet.style.height = clamp(heights[index] - delta.y, heights[0], heights[2]) + 'px'; },
      end(delta, canceled) { sheet.removeAttribute('data-drag'); const goal = heights[index] - delta.y; setStop(canceled ? index : heights.reduce((best, height, i) => Math.abs(height - goal) < Math.abs(heights[best] - goal) ? i : best, index)); },
    });
  </script>
</body>
</html>

UI Skills 原创示例

相关参考