Hide-on-scroll header
下滑隐藏顶栏
Hide the header on the way down; return it on the way up.
Also searched as: 往下滑导航栏藏起来,往上滑又出来, 滚动时顶部导航自动隐藏, 看文章时顶栏收起来让出地方, 智能隐藏的顶栏
- Placement
- Navigation
- Mood
- Crisp
- Trigger
- Scroll
- Site types
- 个人网站, 博客, 作品集
How it works & accessibility
Compare successive scroll positions and translate the header out of view when scrolling down and back when scrolling up.
Best for: Navigation (Scroll).
Reduced motion: 不滑动,改成淡出淡入;藏着时鼠标点不到,键盘选中顶栏里的链接时马上出现
Mobile & touch: 手机上最有用(屏幕小,让出地方给内容);位置限制在能滚的范围里,iOS 滚过头回弹不会让它冒出来
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 时长 | duration | 300ms | — | — |
| 缓动 | easing | cubic-bezier(0.2, 0.7, 0.3, 1) | — | — |
Ready-to-paste AI prompt
Add “Hide-on-scroll header” to the navigation in my project: Compare successive scroll positions and translate the header out of view when scrolling down and back when scrolling up.
Parameters: duration: 300ms, easing: cubic-bezier(0.2, 0.7, 0.3, 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/hide-on-scroll-header.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 下滑隐藏顶栏(Hide-on-scroll header)的样式,配合 hide-on-scroll-header.js。
改写自 Animation Handbook 的 Hide-on-Scroll Header(MIT License,Copyright (c) 2026 Matin Monshizadeh)。
顶栏加 class="autohide",默认用 position: sticky 放在顶上;顶栏自己写了 position(比如 fixed)就用它自己的(这几样默认值写在 :where() 里,
什么选择器都盖得过它;用 fixed 的话页面上面要留出顶栏的高度)。
藏起来是往上推出它自己的高度再多 2px(下边框一起出去;顶栏有往下的影子时,把 2px 改成影子伸出来的距离,不然藏起来时屏幕顶上会留一道影子),
只动 translate,不盖掉顶栏原来的 transform。
- 键盘选中顶栏里的链接时一直在(:focus-within)。顶栏里有悬停展开的菜单时,菜单开着别藏:把 :hover(或者菜单打开时加的 class)
并进下面的 :not(),比如 .autohide.is-away:not(:focus-within, :hover)。
- 点页内链接跳到某个标题时别被顶栏挡住:给 html 加 scroll-padding-top,值是顶栏的高度。
- 顶栏里有这一页最要紧的按钮(结账、保存)时,别用这个效果。
- 这个 class 会重写顶栏的 transition(transition 只能写一行,后写的盖掉先写的):顶栏原来就有过渡的,把原来的几项并进这一行。
- 开启"减少动态"时:不滑动,淡出淡入;藏着时鼠标点不到(pointer-events: none),键盘照样能选中、选中就出现。 */
:where(.autohide) {
position: sticky;
top: 0;
z-index: 10;
}
.autohide {
--autohide-duration: 300ms;
--autohide-easing: cubic-bezier(0.2, 0.7, 0.3, 1);
transition: translate var(--autohide-duration) var(--autohide-easing);
}
.autohide.is-away:not(:focus-within) {
translate: 0 calc(-100% - 2px);
}
@media (prefers-reduced-motion: reduce) {
.autohide { transition: opacity var(--autohide-duration) linear; }
.autohide.is-away:not(:focus-within) { translate: none; opacity: 0; pointer-events: none; }
}JavaScript
// 下滑隐藏顶栏(Hide-on-scroll header)· 原生 JS,不依赖任何库。
// 改写自 Animation Handbook 的 Hide-on-Scroll Header(MIT License,Copyright (c) 2026 Matin Monshizadeh):
// 往下滑把顶栏整条推出屏幕,往上滑拉回来;离页面顶上不到两条顶栏高时一直在。滑动不到 threshold 像素的抖动(触控板、手指常有)不算,
// 只有算数的那一次才记下位置,所以慢慢滑也会累积起来;位置先限制在 0 到能滚的最大值之间(iOS 滚过头回弹的那一下不算往上滑)。
// 默认的时长和缓动来自它。
// bar 是顶栏(配合 hide-on-scroll-header.css):顶栏加 class="autohide",用 position: sticky(或 fixed)放在顶上。
// 脚本只切换 class="is-away"、把时长和缓动写到顶栏的 CSS 变量上;滑动本身是 CSS 过渡,只动 translate,不引起重新排版。
// 键盘选中顶栏里的链接时顶栏一直在(CSS 的 :focus-within)。root 是滚动的容器,默认整个页面(window)。
// 返回 { update(), destroy() }。
// 开启"减少动态"时:脚本照常判断往哪边滑,CSS 让顶栏淡出淡入、不滑动。
/**
* @param {HTMLElement} bar
* @param {{ duration?: number, easing?: string, threshold?: number, root?: Window | HTMLElement }} [options]
*/
export function hideOnScroll(bar, {
duration = 300,
easing = 'cubic-bezier(0.2, 0.7, 0.3, 1)',
threshold = 6,
root = window,
} = {}) {
bar.style.setProperty('--autohide-duration', `${duration}ms`);
bar.style.setProperty('--autohide-easing', easing);
// 现在滚到哪里:限制在 0 到能滚的最大值之间
const position = () => {
const y = root === window ? scrollY : root.scrollTop;
const max = root === window ? document.documentElement.scrollHeight - innerHeight : root.scrollHeight - root.clientHeight;
return Math.min(Math.max(y, 0), Math.max(max, 0));
};
let near = bar.offsetHeight * 2; // 离顶上多近时一直显示:两条顶栏高
let away = false;
let last = position();
const set = (on) => {
if (on === away) return;
away = on;
bar.classList.toggle('is-away', on);
};
const update = () => {
const y = position();
if (y <= near) { set(false); last = y; return; }
const dy = y - last;
if (Math.abs(dy) < threshold) return; // 抖动不算;last 不变,慢慢滑会累积起来
set(dy > 0);
last = y;
};
let ticking = false;
const schedule = () => {
if (ticking) return;
ticking = true;
requestAnimationFrame(() => {
ticking = false;
update();
});
};
root.addEventListener('scroll', schedule, { passive: true });
// 顶栏变高(换行、字体换了)时,"离顶上多近"跟着变
const resized = new ResizeObserver(() => { near = bar.offsetHeight * 2; });
resized.observe(bar);
update();
return {
update,
destroy() {
root.removeEventListener('scroll', schedule);
resized.disconnect();
bar.classList.remove('is-away');
bar.style.removeProperty('--autohide-duration');
bar.style.removeProperty('--autohide-easing');
},
};
}HTML
<!-- 下滑隐藏顶栏:顶栏加 class="autohide"(放在 body 的最前面,sticky 才贴得住顶上);往下滑藏起来,往上滑出现 -->
<header class="autohide" style="display: flex; align-items: center; gap: 24px; height: 56px; padding: 0 24px; background: #fff; border-bottom: 1px solid #eee">
<a href="/">北煜ii</a>
<nav style="display: flex; gap: 16px; margin-left: auto">
<a href="/posts/">文章</a>
<a href="/works/">作品</a>
<a href="/about/">关于</a>
</nav>
</header>
<main>……</main>
<style>
/* 页内链接跳到标题时不被顶栏挡住 */
html { scroll-padding-top: 56px; }
</style>
<script type="module">
import { hideOnScroll } from './hide-on-scroll-header.js';
hideOnScroll(document.querySelector('.autohide'));
</script>