全部效果点击与切换

分段选择器

Segmented Control

紧密相连的一组选项,仅突出其中一个选中项。

选择一个时间范围

适用场景

在少量明确的范围或模式之间切换,例如日、周、月。 经常要在少量互斥的视图间来回切换时,把选项并排展示,便于比较和直接选择。

应用示例

课程日历顶部并排放着「日」「周」「月」。点「周」后,高亮移到周视图,下面的日历也切换为一周的排课。

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

    .segments { margin: 0; padding: 4px; display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid #7b659a; border-radius: 9px; background: #211931; }
    .segments legend { padding-inline: 5px; }
    .segments label { position: relative; cursor: pointer; }
    .segments input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
    .segments span { display: grid; min-height: 34px; place-items: center; padding: 3px; border-radius: 6px; font-size: 12px; transition: background .18s ease, color .18s ease; }
    .segments input:checked + span { background: #eccffa; color: #372047; font-weight: 700; }
    .segments input:focus-visible + span { outline: 2px solid #fff; outline-offset: 1px; }

    @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"><fieldset class="segments"><legend class="label">时间范围</legend><label><input type="radio" name="range" checked><span>日</span></label><label><input type="radio" name="range"><span>周</span></label><label><input type="radio" name="range"><span>月</span></label></fieldset></div>
  </main>
</body>
</html>

UI Skills 原创示例