收放导航条
导航组件HTML/CSS / React / Vue已复制 0 次
给暗色产品官网用的顶栏:页面往下滑过 40px,导航条从 76px 收到 57px,同时收紧高度、增强毛玻璃、点亮底边微光、把 CTA 填成实心——四个信号一起变,收起这件事才读得出来,只改高度用户根本注意不到。当前导航项底下有一枚胶囊指示器,鼠标移过哪一项就滑到哪一项,移开回到当前页;宽度和位置都按真实渲染几何量出来,不用等宽假设。收起后的目标高度由 shrink 控制:默认往 44px 走 60%,不是简单乘一个比例——早期版本 76px 只收 14px,肉眼几乎看不出来。强调色、原始高度、收缩幅度、毛玻璃强度、指示器形态(胶囊/下划线)都可改。给的是三框架源码,参数集中在开头的 CONFIG,改数字即生效。
复制代码
代码跟着右侧参数走:你调的每个值都已经写进代码
<header class="hd" id="hd" data-shape="pill">
<a class="hd-logo" href="#"><span class="mark"></span>ATLAS</a>
<nav class="hd-nav" id="nav">
<span class="hd-pill" id="pill"></span>
<a href="#" class="is-on">Platform</a>
<a href="#">Models</a>
<a href="#">Pricing</a>
<a href="#">Docs</a>
</nav>
<div class="hd-right">
<span class="hd-lang"><b>EN</b><s>/</s>中文</span>
<button class="btn-cta" type="button">
Start building
<svg width="11" height="11" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<path d="M2.5 9.5 9.5 2.5M9.5 2.5H4.2M9.5 2.5v5.3" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</div>
</header>
<main>
<section class="wrap hero">
<span class="eyebrow"><i></i>Inference infrastructure</span>
<h1>Ship <em>models</em>, not server rooms.</h1>
<p>不用再算 GPU、不用再拼调度。一条命令接上推理,冷启动 300ms,按秒计费,跑不动时自己横向扩。</p>
<div class="hero-act">
<a class="btn-main" href="#">
Deploy in 60s
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<path d="M2 6h8M6.6 2.4 10.2 6l-3.6 3.6" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</a>
<a class="btn-ghost" href="#">
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<path d="M4 1.8 10.2 6 4 10.2z" fill="currentColor"/>
</svg>
Watch 2 min demo
</a>
</div>
<div class="metrics">
<div><b>312<span>ms</span></b><small>P95 冷启动,7B 模型 / 单卡</small></div>
<div><b>99.98<span>%</span></b><small>过去 90 天调度成功率</small></div>
<div><b>41<span>%</span></b><small>相比自建集群的平均成本下降</small></div>
</div>
</section>
<section class="sect">
<div class="sect-head">
<h2>滚一下,看导航条收起来</h2>
<span>STICKY HEADER / SCROLL BEHAVIOUR</span>
</div>
<div class="cards">
<article class="card">
<div class="ico">
<svg width="17" height="17" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<path d="M3 7.5 10 3.5l7 4v8l-7 4-7-4z" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
<path d="M3 7.5 10 11.5l7-4M10 11.5v8" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
</svg>
</div>
<h3>高度收 22px</h3>
<p>越过 40px 阈值才开始收,回到顶部立刻展开。中途来回抖不会触发。</p>
<code>76px → 54px</code>
</article>
<article class="card">
<div class="ico">
<svg width="17" height="17" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<rect x="2.5" y="4.5" width="15" height="11" rx="2.5" stroke="currentColor" stroke-width="1.4"/>
<path d="M2.5 9.5h15" stroke="currentColor" stroke-width="1.4"/>
</svg>
</div>
<h3>底色转毛玻璃</h3>
<p>透明底换成 72% 近黑加 18px 模糊,压住下面滚过去的内容。</p>
<code>backdrop-filter</code>
</article>
<article class="card">
<div class="ico">
<svg width="17" height="17" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<circle cx="10" cy="10" r="7" stroke="currentColor" stroke-width="1.4"/>
<path d="M10 6v4.2l2.8 1.7" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/>
</svg>
</div>
<h3>指示器只位移</h3>
<p>一颗胶囊在不同项之间滑,形态不变,所以看起来是「移过去」不是「重新出现」。</p>
<code>transform: translateX</code>
</article>
</div>
</section>
<section class="note">
<div>
<h2>为什么这样收,而不是换成小一号的导航条</h2>
<p>很多导航条在滚动后直接换成另一套样式:字号变小、logo 换色、按钮变图标。看起来是"变了",但读者会感觉到一次跳变。</p>
<p>这里只改 <b>高度、底色、底线、CTA 填充</b> 四件事,其中三件是缓慢过渡,一件(底线辉光)是瞬时提示。结构完全不动,所以滚动时视线不会被打断。</p>
<p>指示器同理:宽高都在量完之后写进 <b>transform</b> 和 <b>width</b>,不碰 <b>left</b>,所以每帧不触发重排,60 帧里也不会掉帧。</p>
</div>
<dl>
<dt>THRESHOLD</dt><dd>40px,单向判断,避免边缘抖动</dd>
<dt>EASING</dt><dd>cubic-bezier(.16, 1, .3, 1),先快后慢</dd>
<dt>SHAPE</dt><dd>参数可切 pill(胶囊)/ line(底部长线)</dd>
<dt>DARK FIRST</dt><dd>默认深底,浅底只需把四个变量换成浅色</dd>
</dl>
</section>
</main>
<footer>
<div class="ub-credit-bar" id="credit">
<a href="https://uibits.cn" target="_blank" rel="noopener">
<span class="mk">UI Bits</span><span class="hair"></span><span>uibits.cn</span><span>↗</span>
</a>
<span>组件来自 UI Bits 组件库</span>
</div>
</footer>
<script>
(function () {
'use strict'
var CONFIG = { accent: '#7cf5c4', height: 76, shrink: 60, blur: 18, shape: 'pill', credit: true }
var S = Object.assign({}, CONFIG)
var root = document.documentElement
var hd = document.getElementById('hd')
var nav = document.getElementById('nav')
var pill = document.getElementById('pill')
var credit = document.getElementById('credit')
var links = [].slice.call(nav.querySelectorAll('a'))
var active = links[0]
var THRESHOLD = 40
function measure(el, host) {
var a = el.getBoundingClientRect()
var b = host.getBoundingClientRect()
return { x: a.left - b.left, w: a.width }
}
function moveTo(el, instant) {
if (!el) return
var m = measure(el, nav)
pill.style.transition = instant
? 'opacity .2s'
: 'transform .46s cubic-bezier(.16,1,.3,1), width .36s cubic-bezier(.16,1,.3,1), opacity .22s'
pill.style.width = m.w + 'px'
pill.style.transform = 'translate(' + m.x + 'px, -50%)'
pill.classList.add('is-ready')
}
function apply() {
root.style.setProperty('--accent', S.accent)
root.style.setProperty('--blur', S.blur + 'px')
hd.style.height = S.height + 'px'
hd.dataset.shape = S.shape === 'line' ? 'line' : 'pill'
credit.style.display = S.credit ? '' : 'none'
/* 收起来的目标高度:shrink 就是"往 44px 走多少"。
早期版本写成 height*0.3,76px 只收 14px,肉眼几乎看不出来。 */
var tight = Math.max(44, Math.round(S.height - (S.height - 44) * (S.shrink / 100)))
hd.dataset.tight = tight
applyScroll()
moveTo(active, true)
}
var ticking = false
function applyScroll() {
var y = window.scrollY || window.pageYOffset || 0
var on = y > THRESHOLD
if (on !== hd.classList.contains('is-tight')) hd.classList.toggle('is-tight', on)
hd.style.height = (on ? Number(hd.dataset.tight || 54) : S.height) + 'px'
ticking = false
}
window.addEventListener('scroll', function () {
if (!ticking) {
ticking = true
requestAnimationFrame(applyScroll)
}
}, { passive: true })
links.forEach(function (a) {
a.addEventListener('mouseenter', function () { moveTo(a) })
a.addEventListener('focus', function () { moveTo(a) })
a.addEventListener('click', function (e) {
e.preventDefault()
links.forEach(function (x) { x.classList.remove('is-on') })
a.classList.add('is-on')
active = a
moveTo(a)
})
})
nav.addEventListener('mouseleave', function () { moveTo(active) })
window.addEventListener('resize', function () { moveTo(active, true) })
apply()
})()
</script>
速览
- 类别
- 导航
- 层级
- 组件
- 框架
- HTML/CSS / React / Vue
- 复制
- 0 次
安装
复制对应框架的源码与「样式 CSS」即可,无第三方依赖;可调项集中在开头的 CONFIG。能力
- 支持深色
- 带动效
- 响应式
自定义参数
6 项默认其它参数
尚未归类的可调项相似组件
按标签重合度推荐,不是运营手配的榜单。



