收件箱
3 封新消息
大标题收缩
Collapsing large title
页面往下滑时,首屏大标题按滚动进度顺滑缩放并收进顶栏成为小标题
常见大白话搜法:往下滑大标题缩成顶栏小标题、滚动时导航大标题收进顶栏、iOS大标题吸顶缩小、滚动收缩顶栏标题
- 适用位置
- 导航、首屏标题
- 气质感觉
- 利落、柔和
- 触发方式
- 滚动
- 适合网站
- 个人网站、博客、移动端网页、工具站
实现原理与无障碍底线
固定高度的吸顶顶栏通过 overflow-anchor: none 与 scroll-padding-top 避免滚动锚定抖动与焦点遮挡;脚本在滚动时按 scrollTop / distance 计算进度,仅更新标题元素的 scale 与 translate(从首屏位置缩放到 minScale 并上移对齐顶栏中心),同步淡出副标题并显示顶栏底线;减少动态时跳过过渡并在跨过阈值后直接切换状态。
适用场景:移动端导航、收件箱、个人主页或长文目录页:首屏突出页面名称,滑入正文后收成紧凑顶栏腾出阅读空间
减少动态(prefers-reduced-motion):取消平滑缩放插值,在滚动越过一半距离时直接切换大标题与紧凑顶栏状态
移动端与触屏:移动端最常用;固定顶栏高度并设置 scroll-padding-top,确保键盘聚焦项不被顶栏遮住
关键可调参数
| 参数 | 键名 | 默认值 | 建议范围 | 说明 |
|---|---|---|---|---|
| 收缩距离 | distance | 96px | — | — |
| 最小缩放 | minScale | 0.56倍 | — | — |
可直接复制给 AI(Cursor / Claude / v0)的提示词
请在我的项目里给导航加上「大标题收缩」(Collapsing large title)效果:固定高度的吸顶顶栏通过 overflow-anchor: none 与 scroll-padding-top 避免滚动锚定抖动与焦点遮挡;脚本在滚动时按 scrollTop / distance 计算进度,仅更新标题元素的 scale 与 translate(从首屏位置缩放到 minScale 并上移对齐顶栏中心),同步淡出副标题并显示顶栏底线;减少动态时跳过过渡并在跨过阈值后直接切换状态。
参数:收缩距离 96px,最小缩放 0.56倍。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/collapsing-header.md(原理、参数和参考代码)原生无依赖参考代码
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>