滑动标签页
导航组件HTML/CSS / React / Vue已复制 0 次
给产品漫游页用的标签页:并列的东西用颜色分开最省事——五个标签各有一支自己的颜色,一枚实心色块滑到当前项,颜色跟着换,下面那页内容也走同一支颜色,读完不会串。指示器是一枚绝对定位的色块,位置按标签在滚动内容里的真实坐标算:横向溢出后滑到右边再点,色块依然贴住标签,不会越滑越偏。标签均分铺满整条,右半边不留一片灰;窄屏溢出时两侧渐隐提示还能滑,选中的标签如果滑出可视区会被拉回来。强调色只管页面标题的强调线,标签各自保留自己的颜色;形态(实心色块/下划线)、滑动时长、是否允许横向溢出都可改。给的是三框架源码,参数集中在开头的 CONFIG。
复制代码
代码跟着右侧参数走:你调的每个值都已经写进代码
<div class="page">
<span class="eyebrow">Product tour</span>
<h1>五个标签,<em>五种颜色</em>,<br>读完不会混。</h1>
<p>指示器是一枚实心色块,滑过去的时候颜色跟着换;下面这一页内容也走同一支颜色。并列的东西,用颜色分开最省事。</p>
<div class="tabs-wrap" id="wrap">
<div class="tabs" id="tabs" role="tablist" aria-label="产品能力">
<span class="pill" id="pill"></span>
<button class="tab" role="tab" aria-selected="true" aria-controls="p0" id="t0" style="--c:#ff6b4a">
<span class="d"></span>Overview
</button>
<button class="tab" role="tab" aria-selected="false" aria-controls="p1" id="t1" style="--c:#3b82f6">
<span class="d"></span>Analytics
</button>
<button class="tab" role="tab" aria-selected="false" aria-controls="p2" id="t2" style="--c:#8b5cf6">
<span class="d"></span>Automations
</button>
<button class="tab" role="tab" aria-selected="false" aria-controls="p3" id="t3" style="--c:#0ea98c">
<span class="d"></span>Security
</button>
<button class="tab" role="tab" aria-selected="false" aria-controls="p4" id="t4" style="--c:#e08a1e">
<span class="d"></span>Billing
</button>
</div>
</div>
<div class="panels" id="panels">
<!-- 01 Overview -->
<section class="panel is-on" id="p0" role="tabpanel" aria-labelledby="t0" style="--c:#ff6b4a">
<div class="visual">
<svg viewBox="0 0 120 120" fill="none" aria-hidden="true">
<circle cx="42" cy="60" r="26" stroke="currentColor" stroke-width="2.4" opacity=".55"/>
<circle cx="60" cy="60" r="26" stroke="currentColor" stroke-width="2.4" opacity=".8"/>
<circle cx="78" cy="60" r="26" stroke="currentColor" stroke-width="2.4"/>
<circle cx="60" cy="60" r="5" fill="currentColor"/>
</svg>
<span class="tag">01 / Overview</span>
</div>
<div class="copy">
<span class="k"><i></i>Overview</span>
<h2>把散在工作里的东西,收成一张总览。</h2>
<p>不用先想清楚要看什么。打开就是一屏现在的状态:谁在做、卡在哪、下一步该谁动。</p>
<ul>
<li>一屏看完本周在跑的所有事项</li>
<li>红黄绿三档状态,不用读文字</li>
<li>点任意一块直接进到明细</li>
</ul>
<a class="go" href="#">
看总览
<svg width="11" height="11" 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>
</div>
</section>
<!-- 02 Analytics -->
<section class="panel" id="p1" role="tabpanel" aria-labelledby="t1" style="--c:#3b82f6">
<div class="visual">
<svg viewBox="0 0 120 120" fill="none" aria-hidden="true">
<path d="M24 88h72" stroke="currentColor" stroke-width="2" opacity=".45" stroke-linecap="round"/>
<rect x="30" y="58" width="13" height="30" rx="3" fill="currentColor" opacity=".55"/>
<rect x="53" y="42" width="13" height="46" rx="3" fill="currentColor" opacity=".8"/>
<rect x="76" y="26" width="13" height="62" rx="3" fill="currentColor"/>
<path d="M30 48 56 34l32-14" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" opacity=".5" stroke-dasharray="4 5"/>
</svg>
<span class="tag">02 / Analytics</span>
</div>
<div class="copy">
<span class="k"><i></i>Analytics</span>
<h2>只看会改变决定的那几个数。</h2>
<p>仪表盘最容易变成数字墙。这里默认只留三条曲线:涨的、跌的、和突然不动的。</p>
<ul>
<li>异常自动标出来,不用自己盯</li>
<li>任一数字可回看到原始事件</li>
<li>同比、环比、目标线一键切换</li>
</ul>
<a class="go" href="#">
打开分析
<svg width="11" height="11" 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>
</div>
</section>
<!-- 03 Automations -->
<section class="panel" id="p2" role="tabpanel" aria-labelledby="t2" style="--c:#8b5cf6">
<div class="visual">
<svg viewBox="0 0 120 120" fill="none" aria-hidden="true">
<circle cx="30" cy="38" r="10" stroke="currentColor" stroke-width="2.2"/>
<circle cx="90" cy="38" r="10" stroke="currentColor" stroke-width="2.2"/>
<circle cx="60" cy="86" r="10" stroke="currentColor" stroke-width="2.2"/>
<path d="M40 40h40M36 47l18 30M84 47 66 77" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" opacity=".7"/>
<circle cx="60" cy="38" r="3" fill="currentColor"/>
</svg>
<span class="tag">03 / Automations</span>
</div>
<div class="copy">
<span class="k"><i></i>Automations</span>
<h2>把重复的判断,写成能看懂的流程。</h2>
<p>不是脚本编辑器。每条规则都是一句话:当什么发生,就做什么,失败找谁。</p>
<ul>
<li>触发、条件、动作三段式,不写代码</li>
<li>跑过几次、哪次失败,一眼看到</li>
<li>失败自动转人工,不会静默丢单</li>
</ul>
<a class="go" href="#">
新建规则
<svg width="11" height="11" 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>
</div>
</section>
<!-- 04 Security -->
<section class="panel" id="p3" role="tabpanel" aria-labelledby="t3" style="--c:#0ea98c">
<div class="visual">
<svg viewBox="0 0 120 120" fill="none" aria-hidden="true">
<path d="M60 22 92 34v26c0 20-14 32-32 38-18-6-32-18-32-38V34z" stroke="currentColor" stroke-width="2.4" stroke-linejoin="round"/>
<path d="M48 60 57 69l16-17" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<span class="tag">04 / Security</span>
</div>
<div class="copy">
<span class="k"><i></i>Security</span>
<h2>权限写在明面上,不藏在设置第三层。</h2>
<p>谁能看、谁能改、谁改过,三件事必须在同一个地方回答完,否则审计的时候要翻三个系统。</p>
<ul>
<li>角色模板开箱可用,也能按字段收</li>
<li>每次改动留痕,含前后值</li>
<li>敏感操作二次确认,可强制双人</li>
</ul>
<a class="go" href="#">
管理权限
<svg width="11" height="11" 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>
</div>
</section>
<!-- 05 Billing -->
<section class="panel" id="p4" role="tabpanel" aria-labelledby="t4" style="--c:#e08a1e">
<div class="visual">
<svg viewBox="0 0 120 120" fill="none" aria-hidden="true">
<rect x="28" y="24" width="64" height="76" rx="6" stroke="currentColor" stroke-width="2.2"/>
<path d="M40 42h40M40 54h40M40 66h26" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" opacity=".65"/>
<path d="M40 82h14" stroke="currentColor" stroke-width="3.4" stroke-linecap="round"/>
</svg>
<span class="tag">05 / Billing</span>
</div>
<div class="copy">
<span class="k"><i></i>Billing</span>
<h2>这个月为什么贵了,账单自己会说。</h2>
<p>按量计费最怕看不懂。所以把每一笔拆到「谁、做了什么、多少量」,而不是给一个总数让人猜。</p>
<ul>
<li>费用变化自动归因到具体项目</li>
<li>预算到 80% 提前提醒,不等到超额</li>
<li>发票、合同、付款方式同一页管完</li>
</ul>
<a class="go" href="#">
查看账单
<svg width="11" height="11" 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>
</div>
</section>
</div>
</div>
<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: '#ff6b4a', shape: 'pill', duration: 420, overflow: true, credit: true }
var S = Object.assign({}, CONFIG)
var wrap = document.getElementById('wrap')
var tabs = document.getElementById('tabs')
var pill = document.getElementById('pill')
var credit = document.getElementById('credit')
var btns = [].slice.call(tabs.querySelectorAll('.tab'))
var panels = [].slice.call(document.querySelectorAll('.panel'))
var active = 0
function measure(btn) {
var a = btn.getBoundingClientRect()
var t = tabs.getBoundingClientRect()
/* pill 是滚动容器内的绝对定位元素,会随内容一起滚。
所以这里要的是「内容坐标」:可视偏移 + scrollLeft。
原先写成减 scrollLeft,等于把滚动量扣了两次;
标签栏往右滑之后,色块就会越来越往左偏。 */
return { x: a.left - t.left + tabs.scrollLeft, w: a.width }
}
function moveTo(i, instant) {
var btn = btns[i]
if (!btn) return
var m = measure(btn)
var c = btn.style.getPropertyValue('--c')
pill.style.transition = instant
? 'opacity .2s'
: 'transform var(--dur) cubic-bezier(.16,1,.3,1), width var(--dur) cubic-bezier(.16,1,.3,1), background var(--dur) ease, opacity .2s'
pill.style.setProperty('--c', c)
pill.style.width = m.w + 'px'
pill.style.transform = 'translateX(' + m.x + 'px)'
pill.classList.add('is-ready')
/* 这里不要再写根 --c —— 会把主题色参数覆盖掉,标签色由 pill/面板各自持有 */
}
/* 溢出提示:能往左/右滑才显示对应渐隐 */
function edges() {
var max = tabs.scrollWidth - tabs.clientWidth
wrap.classList.toggle('can-left', max > 4 && tabs.scrollLeft > 4)
wrap.classList.toggle('can-right', max > 4 && tabs.scrollLeft < max - 4)
}
function select(i, instant) {
if (i === active && !instant) return
active = i
btns.forEach(function (b, k) { b.setAttribute('aria-selected', k === i ? 'true' : 'false') })
panels.forEach(function (p, k) {
if (k === i) {
p.classList.remove('is-on')
/* 强制回流,让入场动画每次都能重播 */
void p.offsetWidth
p.classList.add('is-on')
} else {
p.classList.remove('is-on')
}
})
moveTo(i, instant)
/* 选中的标签如果滑出可视区,把它拉回来 */
var btn = btns[i]
var a = btn.getBoundingClientRect()
var b = tabs.getBoundingClientRect()
if (a.left < b.left + 40) tabs.scrollLeft -= (b.left + 40) - a.left
else if (a.right > b.right - 40) tabs.scrollLeft += a.right - (b.right - 40)
edges()
}
function apply() {
document.documentElement.style.setProperty('--dur', S.duration + 'ms')
tabs.dataset.shape = S.shape === 'under' ? 'under' : 'pill'
wrap.classList.toggle('is-under', tabs.dataset.shape === 'under')
tabs.classList.toggle('is-static', !S.overflow)
credit.style.display = S.credit ? '' : 'none'
/* 主题色管标题区(eyebrow 短线 + 强调词文字与下划线),
五个标签与内容面板各用自己的 --c,互不覆盖 */
document.documentElement.style.setProperty('--accent', S.accent)
select(active, true)
/* 切到 pill 形态后重新量一次,尺寸会变 */
requestAnimationFrame(function () { moveTo(active, true); edges() })
}
btns.forEach(function (b, i) {
b.addEventListener('click', function () { select(i) })
b.addEventListener('keydown', function (e) {
var d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0
if (!d) return
e.preventDefault()
var n = (i + d + btns.length) % btns.length
btns[n].focus()
select(n)
})
})
tabs.addEventListener('scroll', edges, { passive: true })
window.addEventListener('resize', function () { moveTo(active, true); edges() })
apply()
})()
</script>
相似组件
按标签重合度推荐,不是运营手配的榜单。



