折叠面包屑
导航元素HTML/CSS / React / Vue已复制 0 次
给编辑器、资产管理器和后台用的路径导航:中间层级收进省略号,点一下就在原地展开下拉,还带每层的子项数;当前项固定在末尾、做成一张白卡,所以折的永远是中间,不会把终点折掉。折叠数按屏幕上的位置数算,省略号也算一格——设成 4 就是「首项 + 省略号 + 末尾两项」。窄屏自动再收一档,宁可少给上下文也不让面包屑掉行。分隔符与前一个节点包在一起,换行时跟着自己走,不会在行首留一个孤零零的斜杠;下拉贴住省略号,靠右溢出时自动改成右对齐并把箭头挪过去。强调色、层级深度、可见格数、分隔符形态(箭头/斜杠/圆点)都可改。给的是三框架源码,参数集中在开头的 CONFIG。
复制代码
代码跟着右侧参数走:你调的每个值都已经写进代码
<header class="toolbar">
<div class="dots-win" aria-hidden="true"><i></i><i></i><i></i></div>
<span class="tb-sep" aria-hidden="true"></span>
<div class="crumb-bar">
<nav class="crumb" id="crumb" data-sep="chevron" aria-label="面包屑"></nav>
</div>
<div class="tb-right">
<span class="badge"><i></i>SYNCED</span>
<button class="ico-btn" type="button" aria-label="搜索">
<svg width="15" height="15" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<circle cx="7.2" cy="7.2" r="4.4" stroke="currentColor" stroke-width="1.5"/>
<path d="M10.6 10.6 14 14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
</svg>
</button>
<button class="ico-btn" type="button" aria-label="更多">
<svg width="15" height="15" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<circle cx="3.4" cy="8" r="1.2" fill="currentColor"/>
<circle cx="8" cy="8" r="1.2" fill="currentColor"/>
<circle cx="12.6" cy="8" r="1.2" fill="currentColor"/>
</svg>
</button>
</div>
</header>
<main class="canvas">
<span class="eyebrow">Asset browser</span>
<h1>层级再深,也只占一行。</h1>
<p>中间层级收进省略号,点开就在原地展开。当前项固定在末尾、做成一张白卡,所以折的永远是中间,不会把终点折掉。</p>
<section class="files" aria-label="素材文件">
<a class="file" href="#" style="--fc:#2563eb">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path d="M2.4 5.2c0-.9.7-1.6 1.6-1.6h3.1l1.6 1.9h5.5c.9 0 1.6.7 1.6 1.6v6.2c0 .9-.7 1.6-1.6 1.6H4c-.9 0-1.6-.7-1.6-1.6z" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
</svg>
</span>
<span><b>navigation-patterns.fig</b><small>2 min ago · 1.2 MB</small></span>
</a>
<a class="file" href="#" style="--fc:#8b5cf6">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path d="M4.6 2.4h5.6l3.4 3.4v9.8H4.6z" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
<path d="M10 2.6v3.4h3.4" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
</svg>
</span>
<span><b>collapse-rules.md</b><small>18 min ago · 6 KB</small></span>
</a>
<a class="file" href="#" style="--fc:#0ea5e9">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<rect x="2.4" y="3.4" width="13.2" height="11.2" rx="2" stroke="currentColor" stroke-width="1.4"/>
<path d="M2.6 7.2h12.8" stroke="currentColor" stroke-width="1.4"/>
</svg>
</span>
<span><b>toolbar-light.png</b><small>1 h ago · 240 KB</small></span>
</a>
<a class="file" href="#" style="--fc:#f59e0b">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path d="M2.6 5.4 9 2.2l6.4 3.2v7.2L9 15.8l-6.4-3.2z" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
<path d="M2.6 5.4 9 8.6l6.4-3.2M9 8.6v7.2" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
</svg>
</span>
<span><b>separator-set.svg</b><small>Yesterday · 14 KB</small></span>
</a>
<a class="file" href="#" style="--fc:#10b981">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<circle cx="9" cy="9" r="6.6" stroke="currentColor" stroke-width="1.4"/>
<path d="M6.4 9.3 8.2 11l3.4-3.6" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</span>
<span><b>a11y-checklist.md</b><small>2 days ago · 3 KB</small></span>
</a>
<a class="file" href="#" style="--fc:#ef4444">
<span class="thumb">
<svg width="15" height="15" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path d="M9 2.6 16 6.2v5.6L9 15.4 2 11.8V6.2z" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>
<circle cx="9" cy="9" r="2.1" stroke="currentColor" stroke-width="1.4"/>
</svg>
</span>
<span><b>release-45.zip</b><small>3 days ago · 8.4 MB</small></span>
</a>
</section>
<div class="tip">
<h3>参数里改 maxVisible,看它在 2 到 6 格之间怎么折</h3>
<p>这个数指的是<strong>屏幕上占的位置数</strong>,省略号也算一格。所以设成 4 就是「首项 + 省略号 + 末尾两项」。折掉的中间层级不会消失,点省略号就在原地摊开,还带每层的子项数。</p>
</div>
</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: '#2563eb', depth: 5, maxVisible: 4, sep: 'chevron', credit: true }
var S = Object.assign({}, CONFIG)
var PATH = ['Home', 'Works', '2026', 'Component library', 'Navigation', 'Breadcrumb', 'Collapse']
var COUNT = [12, 8, 5, 24, 3, 4];
var crumb = document.getElementById('crumb')
var credit = document.getElementById('credit')
var cur = null // 当前节点(用于定位下拉)
var pop = null // 下拉元素
var open = false // 展开状态,跨重绘保留
var lastMax = 0 // 上一次算出的"屏幕位置数",变化才重绘
var FI = '<svg class="fi" width="13" height="13" viewBox="0 0 18 18" fill="none" aria-hidden="true">' +
'<path d="M2.4 5.2c0-.9.7-1.6 1.6-1.6h3.1l1.6 1.9h5.5c.9 0 1.6.7 1.6 1.6v6.2c0 .9-.7 1.6-1.6 1.6H4c-.9 0-1.6-.7-1.6-1.6z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/></svg>'
/* 折叠:max 是屏幕上的"位置数",含省略号本身 */
function collapse(items, max) {
if (items.length <= max) {
return { kept: items.map(function (v, i) { return { v: v, i: i } }), hidden: [] }
}
var tail = Math.max(1, max - 2) // 首项 1 + 省略号 1
var kept = [{ v: items[0], i: 0 }]
var hidden = []
for (var i = 1; i < items.length - tail; i++) hidden.push({ v: items[i], i: i })
for (var j = items.length - tail; j < items.length; j++) kept.push({ v: items[j], i: j })
return { kept: kept, hidden: hidden }
}
function closePop() {
if (!pop) return
pop.classList.remove('is-open')
if (cur) cur.setAttribute('aria-expanded', 'false')
open = false
}
function openPop(dotsBtn, hidden) {
closePop()
var p = document.createElement('div')
p.className = 'pop'
var hd = document.createElement('div')
hd.className = 'pop-hd'
hd.textContent = hidden.length + ' 个上级'
p.appendChild(hd)
hidden.forEach(function (h) {
var a = document.createElement('a')
a.href = '#'
a.innerHTML = FI + '<span>' + h.v + '</span>' +
'<em>' + (COUNT[h.i % COUNT.length]) + '</em>'
a.addEventListener('click', function (e) { e.preventDefault(); closePop() })
p.appendChild(a)
})
dotsBtn.appendChild(p)
/* 下一帧再加类,让 transform 过渡有起点 */
requestAnimationFrame(function () {
p.classList.add('is-open')
/* 右边缘溢出时改右对齐,箭头也跟着挪 */
var r = p.getBoundingClientRect()
var over = r.right - (window.innerWidth - 12)
if (over > 0) {
p.style.left = 'auto'
p.style.right = '0'
p.style.transformOrigin = 'calc(100% - 20px) 0'
p.style.setProperty('--arrow-left', 'auto')
}
})
dotsBtn.setAttribute('aria-expanded', 'true')
pop = p
cur = dotsBtn
open = true
}
function render() {
var items = PATH.slice(0, S.depth)
/* 窄屏再折一档:宁可少给上下文,也不要让面包屑掉行 */
var narrow = window.innerWidth < 700
var max = narrow ? Math.min(S.maxVisible, 3) : S.maxVisible
lastMax = max
var r = collapse(items, max)
var html = ''
r.kept.forEach(function (node, k) {
var last = k === r.kept.length - 1
if (k === 1 && r.hidden.length) {
html += '<button class="node dots" type="button" aria-expanded="false" aria-label="展开被折叠的 ' +
r.hidden.length + ' 个上级"><b></b><b></b><b></b></button>'
}
if (last) {
html += '<span class="node is-cur">' + FI + '<span>' + node.v + '</span></span>'
} else {
html += '<a class="node" href="#">' + FI + '<span>' + node.v + '</span></a>'
}
})
crumb.innerHTML = html
pop = null
cur = null
var dotsBtn = crumb.querySelector('.dots')
if (dotsBtn) {
dotsBtn.addEventListener('click', function (e) {
e.stopPropagation()
if (open) { closePop(); return }
openPop(dotsBtn, r.hidden)
})
/* 重绘前是展开的,就原位重新展开 */
if (open) openPop(dotsBtn, r.hidden)
} else {
open = false
}
markLineStarts()
}
/* 换行时给行首节点打标,隐藏它的前导分隔符 */
function markLineStarts() {
var nodes = [].slice.call(crumb.querySelectorAll('.node'))
var prevTop = null
nodes.forEach(function (n) {
var top = Math.round(n.getBoundingClientRect().top)
var start = prevTop !== null && top > prevTop + 2
n.classList.toggle('at-start', start)
prevTop = top
})
}
function apply() {
document.documentElement.style.setProperty('--accent', S.accent)
crumb.dataset.sep = S.sep === 'slash' || S.sep === 'dot' ? S.sep : 'chevron'
credit.style.display = S.credit ? '' : 'none'
render()
}
document.addEventListener('click', function (e) {
if (!open) return
if (cur && cur.contains(e.target)) return
closePop()
})
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && open) closePop()
})
/* 只在"能放几格"变化时重绘 —— 否则窗口一抖就把下拉关掉了 */
var ro = null
window.addEventListener('resize', function () {
var narrow = window.innerWidth < 700
var max = narrow ? Math.min(S.maxVisible, 3) : S.maxVisible
if (max !== lastMax) render()
else markLineStarts()
})
apply()
})()
</script>
相似组件
按标签重合度推荐,不是运营手配的榜单。



