中文

收件箱

3 封新消息

Scroll · Vanilla JS + CSS

Collapsing large title

大标题收缩

Shrink a large hero title smoothly into a compact sticky header as the page scrolls.

Also searched as: 往下滑大标题缩成顶栏小标题, 滚动时导航大标题收进顶栏, iOS大标题吸顶缩小, 滚动收缩顶栏标题

AI Markdown (.md)
Placement
Navigation, Hero heading
Mood
Crisp, Soft
Trigger
Scroll
Site types
个人网站, 博客, 移动端网页, 工具站

How it works & accessibility

Keeps the sticky header bar at a constant layout height with overflow-anchor: none and scroll-padding-top, translating and scaling only the title via compositor properties according to scrollTop / distance while fading out the subtitle.

Best for: Navigation, Hero heading (Scroll).

Reduced motion: 取消平滑缩放插值,在滚动越过一半距离时直接切换大标题与紧凑顶栏状态

Mobile & touch: 移动端最常用;固定顶栏高度并设置 scroll-padding-top,确保键盘聚焦项不被顶栏遮住

Key parameters

ParameterKeyDefaultRangeNotes
收缩距离distance96px——
最小缩放minScale0.56倍——

Ready-to-paste AI prompt

Add “Collapsing large title” to the navigation in my project: Keeps the sticky header bar at a constant layout height with overflow-anchor: none and scroll-padding-top, translating and scaling only the title via compositor properties according to scrollTop / distance while fading out the subtitle.
Parameters: distance: 96px, minScale: 0.56×.
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/collapsing-header.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 大标题收缩(Collapsing large title):配合 collapsing-header.js。
   顶栏本身高度保持恒定(--ch-bar-h),只对标题做 scale / translate 合成层变形,避免触发布局重排与滚动锚定抖动。 */
:where(.ch-view) {
  --ch-bar-h: 42px;
  --ch-min-scale: 0.56;
  --ch-offset-y: 36px;
  overflow-y: auto;
  overflow-anchor: none;
  scroll-padding-top: var(--ch-bar-h);
}

:where(.ch-bar) {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--ch-bar-h);
}

.ch-bar::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--ch-bar-bg, #ffffff);
  border-bottom: 1px solid var(--ch-bar-line, rgb(20 21 25 / 0.12));
  opacity: var(--ch-progress, 0);
  pointer-events: none;
}

.ch-title {
  position: relative;
  z-index: 1;
  transform-origin: left center;
  scale: 1;
  translate: 0px var(--ch-offset-y, 36px);
  will-change: scale, translate;
}

.ch-sub {
  opacity: calc(1 - min(1, var(--ch-progress, 0) * 1.8));
}

@media (prefers-reduced-motion: reduce) {
  .ch-view:not(.ch-force) .ch-title {
    will-change: auto;
  }
}
JavaScript
// 大标题收缩(Collapsing large title)· 原生 JS,不依赖任何库。
// 用法:滚动容器加 class="ch-view",内部吸顶顶栏加 class="ch-bar",大标题加 class="ch-title",
// 在结构里给标题下方留一段空白(.ch-spacer),然后调用 collapsingHeader(title, { distance: 96, minScale: 0.56, root })。
// 返回 { update(), destroy() }。

/**
 * @param {HTMLElement} title
 * @param {{ distance?: number, minScale?: number, offset?: number, root?: Window | HTMLElement | null, ignoreReducedMotion?: boolean }} [options]
 */
export function collapsingHeader(title, {
  distance = 96,
  minScale = 0.56,
  offset = 36,
  root = null,
  ignoreReducedMotion = false,
} = {}) {
  if (!(title instanceof Element)) return null;
  let destroyed = false;
  const dist = Math.max(1, Number(distance) || 96);
  const min = Math.min(0.95, Math.max(0.3, Number(minScale) || 0.56));
  const startY = Number(offset) || 36;
  const bar = title.closest('.ch-bar');
  const view = root || title.closest('.ch-view') || window;
  const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;

  if (view instanceof Element && ignoreReducedMotion) view.classList.add('ch-force');

  const scrollPos = () => {
    if (view === window) return Math.max(0, window.scrollY || 0);
    return Math.max(0, view.scrollTop || 0);
  };

  const update = () => {
    if (destroyed) return;
    const raw = Math.max(0, Math.min(1, scrollPos() / dist));
    const reduced = Boolean(reduceQuery?.matches && !ignoreReducedMotion);
    const p = reduced ? (raw >= 0.5 ? 1 : 0) : raw;
    const scale = Number((1 - (1 - min) * p).toFixed(4));
    const y = Number((startY * (1 - p)).toFixed(2));
    title.style.scale = String(scale);
    title.style.translate = `0px ${y}px`;
    if (bar instanceof HTMLElement) {
      bar.style.setProperty('--ch-progress', String(Number(p.toFixed(3))));
      bar.classList.toggle('is-compact', p >= 0.99);
    }
  };

  let ticking = false;
  const onScroll = () => {
    if (ticking || destroyed) return;
    ticking = true;
    requestAnimationFrame(() => {
      ticking = false;
      update();
    });
  };

  view.addEventListener('scroll', onScroll, { passive: true });
  update();

  return {
    update,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      view.removeEventListener('scroll', onScroll);
      if (view instanceof Element) view.classList.remove('ch-force');
      title.style.removeProperty('scale');
      title.style.removeProperty('translate');
      if (bar instanceof HTMLElement) {
        bar.style.removeProperty('--ch-progress');
        bar.classList.remove('is-compact');
      }
    },
  };
}
HTML
<!-- 大标题收缩:固定高度顶栏 .ch-bar 内的 .ch-title 随滚动缩放并上移收进顶栏 -->
<div class="ch-view" style="height: 420px;">
  <header class="ch-bar" style="--ch-bar-bg: #f4f3f0; padding: 0 20px;">
    <h1 class="ch-title" style="margin: 0; font-size: 28px;">收件箱</h1>
    <button type="button">编辑</button>
  </header>
  <div style="padding: 46px 20px 24px;">
    <p class="ch-sub">3 封未读邮件</p>
    <div style="height: 800px;">……</div>
  </div>
</div>
<script type="module">
  import { collapsingHeader } from './collapsing-header.js';
  collapsingHeader(document.querySelector('.ch-title'), { distance: 96, minScale: 0.56 });
</script>

Shown once per release · Always available in changelog