中文
Lo-Fi 轻音乐 · 03:40
Page & layout · Vanilla JS + CSS

Dynamic island pill

胶囊灵动展开

Expand a compact pill smoothly into a richer panel and shrink it back on collapse.

Also searched as: 灵动岛胶囊展开, 顶部状态胶囊伸缩, 通知胶囊丝滑变宽变高, 灵动岛交互

AI Markdown (.md)
Placement
Navigation, Button
Mood
Playful, Technical
Trigger
点击
Site types
工具和应用, 个人网站, 品牌官网

How it works & accessibility

Measures before and after bounding boxes and animates width, height and border-radius with WAAPI while preserving non-wrapping compact labels and Escape-to-collapse keyboard support; reduced motion toggles states immediately.

Best for: Navigation, Button (点击).

Reduced motion: 跳过宽高伸缩动画,点击或状态变化时直接切换到紧凑或展开内容

Mobile & touch: 展开宽度不超过屏幕安全边距(max-width: calc(100vw - 24px)),点击按钮区域不小于 44px

Key parameters

ParameterKeyDefaultRangeNotes
伸缩时长duration420ms——
缓动easingcubic-bezier(0.22, 1, 0.36, 1)——

Ready-to-paste AI prompt

Add “Dynamic island pill” to the navigation in my project: Measures before and after bounding boxes and animates width, height and border-radius with WAAPI while preserving non-wrapping compact labels and Escape-to-collapse keyboard support; reduced motion toggles states immediately.
Parameters: duration: 420ms, easing: cubic-bezier(0.22, 1, 0.36, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/dynamic-island.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 胶囊灵动展开(Dynamic island pill)样式,配合 dynamic-island.js。 */
.dis-pill {
  position: relative;
  display: inline-grid;
  justify-items: stretch;
  border-radius: 22px;
  background: var(--dis-bg, #141519);
  color: var(--dis-ink, #ffffff);
  overflow: hidden;
  box-shadow: 0 12px 28px -12px rgb(0 0 0 / 0.45);
  max-width: calc(100vw - 24px);
}

.dis-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 38px;
  padding: 6px 14px;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  white-space: nowrap;
  cursor: pointer;
}

.dis-expanded {
  padding: 0 16px 12px;
  display: grid;
  gap: 6px;
  min-width: 210px;
}

.dis-pill:not([data-expanded="true"]) .dis-expanded {
  display: none;
}
JavaScript
// 胶囊灵动展开(Dynamic island pill)参考实现。
// 用法:容器 .dis-pill 内含 .dis-trigger(紧凑态)与 .dis-expanded(展开态)。
// 调 const ctl = dynamicIsland(root, { duration: 420, easing: '...' }); 销毁调 ctl.destroy()。

export function dynamicIsland(root, {
  expanded = false,
  duration = 420,
  easing = 'cubic-bezier(0.22, 1, 0.36, 1)',
  ignoreReducedMotion = false,
} = {}) {
  if (!(root instanceof Element)) return null;
  const compact = root.querySelector('.dis-trigger');
  const panel = root.querySelector('.dis-expanded');
  if (!compact || !panel) return null;

  const dur = Number(duration) || 420;
  const easeStr = String(easing);
  const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
  const isReduced = () => Boolean(reduceQuery?.matches && !ignoreReducedMotion);

  root.classList.add('dis-pill');
  let isExpanded = Boolean(expanded);
  let activeAnim = null;
  let destroyed = false;

  const syncAttr = () => {
    root.dataset.expanded = String(isExpanded);
    compact.setAttribute('aria-expanded', String(isExpanded));
  };

  syncAttr();

  const setExpanded = (next, { animate = true } = {}) => {
    if (destroyed) return;
    const target = Boolean(next);
    if (target === isExpanded) return;

    if (activeAnim) { activeAnim.cancel(); activeAnim = null; }
    const fromRect = root.getBoundingClientRect();
    isExpanded = target;
    syncAttr();
    const toRect = root.getBoundingClientRect();

    if (animate && !isReduced() && fromRect.width > 0 && toRect.width > 0) {
      activeAnim = root.animate(
        [
          { width: `${fromRect.width}px`, height: `${fromRect.height}px`, borderRadius: target ? '22px' : '18px' },
          { width: `${toRect.width}px`, height: `${toRect.height}px`, borderRadius: target ? '18px' : '22px' },
        ],
        { duration: dur, easing: easeStr, fill: 'none' },
      );
      activeAnim.finished.then(() => { if (!destroyed) activeAnim = null; }).catch(() => undefined);
    }

    if (destroyed) return;
    root.dispatchEvent(new CustomEvent('islandchange', { bubbles: true, detail: { expanded: isExpanded } }));
  };

  const onCompactClick = () => setExpanded(true);
  const onPanelClick = (ev) => {
    if (ev.target instanceof Element && ev.target.closest('[data-dis-collapse]')) setExpanded(false);
  };
  const onKeyDown = (ev) => {
    if (ev.key === 'Escape' && isExpanded) {
      ev.preventDefault();
      setExpanded(false);
      if (compact instanceof HTMLElement && compact.tabIndex !== -1) compact.focus();
    }
  };

  compact.addEventListener('click', onCompactClick);
  panel.addEventListener('click', onPanelClick);
  root.addEventListener('keydown', onKeyDown);

  return {
    setExpanded,
    toggle() { setExpanded(!isExpanded); },
    isExpanded: () => isExpanded,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      if (activeAnim) { activeAnim.cancel(); activeAnim = null; }
      compact.removeEventListener('click', onCompactClick);
      panel.removeEventListener('click', onPanelClick);
      root.removeEventListener('keydown', onKeyDown);
      delete root.dataset.expanded;
      compact.removeAttribute('aria-expanded');
    },
  };
}
HTML
<div class="dis-pill" id="island">
  <button type="button" class="dis-trigger">播放中 · 2:14</button>
  <div class="dis-expanded">
    <div style="display:flex;justify-content:space-between;align-items:center;gap:12px;">
      <strong>Night Walk</strong>
      <button type="button" data-dis-collapse>收起</button>
    </div>
    <small>Lo-Fi · 2:14 / 3:40</small>
  </div>
</div>

<script type="module">
  import { dynamicIsland } from './dynamic-island.js';
  dynamicIsland(document.getElementById('island'));
</script>

More in Page & layout

Browse all 86 effects →

Shown once per release · Always available in changelog