聚光跟随浮层
浮层组件HTML/CSS已复制 0 次
给成员、联系人、作品列表用的追光浮层。列表项有一道从中心展开的指示条和跟着光标走的柔光;详情浮层用真玻璃拟态(背景模糊 + 斜向微光 + 内高光边 + 深投影),带缓动跟随和内部视差,靠近屏幕边缘自动翻到另一侧,不压住正在看的那一行。
头像换成真实人物照片,比首字母色块更像一个能放进产品里的东西。触屏降级为点击展开,Esc 可关,键盘聚焦也能触发。参数覆盖名单、明暗主题、双主色、跟随强度、翻转边距、圆角、模糊、视差、在线标记与浮层底栏。
复制代码
「使用示例」跟随右侧参数实时生成;源码与样式是定值
<!-- spotlight-panel · 来自 UI Bits 组件库。HTML + 内联脚本直接贴进页面;配套样式见「样式 CSS」Tab。头像为示例素材,替换 AV 数组里的地址即可。 -->
<div class="page">
<div class="eyebrow">UI Bits · Overlay</div>
<div class="h1">聚光跟随浮层 <span>spotlight-panel</span></div>
<div class="stage" id="stage">
<div class="people">
<div class="p-head" id="phead">悬停成员条目 · 追光边框 + 玻璃浮层跟随光标(名单为示例)</div>
<div class="p-list" id="grid"></div>
</div>
</div>
<div class="ub-credit" 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>
</div>
<div class="spot-pop" id="pop" role="tooltip" aria-hidden="true">
<div class="accent-line"></div><div class="sheen"></div>
<div class="body-pop" id="popBody">
<div class="p-top"><div class="av"><img id="popAv" alt="头像"></div><div><h4 id="popName"></h4><div class="pr" id="popRole"></div></div></div>
<div class="desc" id="popDesc"></div><div class="tags" id="popTags"></div>
<div class="p-foot" id="popFoot"><span>当前项目 · 设计系统</span><b>查看档案 ↗</b></div>
</div>
</div>
<script>
(function(){
'use strict'
var KEY='spotlightPanel'
var AV=['https://uibits.cn/demo/component/assets/avatars/32.jpg','https://uibits.cn/demo/component/assets/avatars/44.jpg','https://uibits.cn/demo/component/assets/avatars/68.jpg','https://uibits.cn/demo/component/assets/avatars/75.jpg']
var SPEC=[
{key:'heading',label:'列表说明',type:'text',def:'悬停成员条目 · 追光边框 + 玻璃浮层跟随光标(名单为示例)'},
{key:'people',label:'成员(姓名|角色|简介)',type:'textarea',def:'沈知微|产品设计师|负责设计系统与组件规范,把交互决策写进文档而不是留在脑子里。\n陆屿川|前端工程师|专攻动效与性能,信条是能用一个 rAF 解决的事就不引入一个库。\n温以宁|视觉设计师|从品牌做到界面,最近在琢磨怎么让数据可视化有信息量又不像仪表盘。\n方叙白|内容策划|把关内容质量的那个人,一句话能指出解析里哪句是套话。'},
{key:'theme',label:'明暗',type:'select',def:'dark',options:['dark','light']},
{key:'accent',label:'主色',type:'color',def:'#9B8CFA'},
{key:'secondary',label:'强调色',type:'color',def:'#4FC9C1'},
{key:'follow',label:'跟随强度',type:'range',def:14,min:4,max:30,step:1},
{key:'edge',label:'翻转边距',type:'range',def:24,min:8,max:80,step:2},
{key:'radius',label:'浮层圆角',type:'range',def:16,min:4,max:28,step:1},
{key:'blur',label:'背景模糊',type:'range',def:22,min:8,max:32,step:1},
{key:'parallax',label:'内部视差',type:'toggle',def:true},
{key:'showOnline',label:'在线标记',type:'toggle',def:true},
{key:'showMeta',label:'浮层底栏',type:'toggle',def:true},
{key:'credit',label:'品牌位',type:'toggle',def:true}
]
var S={};SPEC.forEach(function(s){S[s.key]=s.def})
try{var q=new URLSearchParams(location.search).get('p');if(q){var o=JSON.parse(q);Object.keys(o).forEach(function(k){if(k in S)S[k]=o[k]})}}catch(e){}
var reduce=matchMedia('(prefers-reduced-motion: reduce)').matches
var el=function(id){return document.getElementById(id)}
/* 在详情页的预览窗里收起自带控件:右侧已经有一套,重复一套只会挤占场地 */
var ctlBox=el('ctl');if(ctlBox&&window.self!==window.top)ctlBox.style.display='none'
var grid=el('grid'),pop=el('pop'),stage=el('stage'),body=el('popBody')
var tx=0,ty=0,cx=0,cy=0,shown=false,raf=null,active=-1,touch=false,people=[]
function clamp(v,a,b){return Math.max(a,Math.min(b,v))}
function parsePeople(){
people=String(S.people||'').split('\n').map(function(l){return l.trim()}).filter(Boolean).map(function(l,i){
var p=l.split('|');return {nm:(p[0]||'').trim(),rl:(p[1]||'').trim(),desc:(p[2]||'').trim(),av:AV[i%AV.length]}
})
}
function renderList(){
grid.innerHTML=''
people.forEach(function(p,i){
var row=document.createElement('div');row.className='person';row.setAttribute('tabindex','0')
row.innerHTML='<div class="av-wrap"><img src="'+p.av+'" alt="'+p.nm+'"></div><div class="meta"><div class="nm">'+p.nm+(S.showOnline?'<i></i>':'')+'</div><div class="rl">'+p.rl+'</div></div><span class="go">预览 ↗</span>'
row.addEventListener('pointermove',function(e){var r=row.getBoundingClientRect();row.style.setProperty('--mx',(e.clientX-r.left)+'px');row.style.setProperty('--my',(e.clientY-r.top)+'px')})
row.addEventListener('mouseenter',function(e){if(!touch)show(i,e.clientX,e.clientY)})
row.addEventListener('mouseleave',function(){if(!touch)hide()})
row.addEventListener('focus',function(){var r=row.getBoundingClientRect();show(i,r.left+r.width/2,r.top)})
row.addEventListener('blur',function(){if(!touch)hide()})
row.addEventListener('click',function(e){touch=true;show(i,e.clientX,e.clientY)})
grid.appendChild(row)
})
}
function fill(i){var p=people[i];if(!p)return
el('popAv').src=p.av;el('popAv').alt=p.nm;el('popName').textContent=p.nm;el('popRole').textContent=p.rl;el('popDesc').textContent=p.desc
el('popTags').innerHTML=(p.rl?[p.rl,'Profile']:['Profile']).map(function(t){return '<span>'+t+'</span>'}).join('')
}
function place(x,y){var w=pop.offsetWidth||296,h=pop.offsetHeight||236,fx
if(window.innerWidth-x>w+S.edge+28)fx=x+w/2+26;else fx=x-w/2-26
tx=clamp(fx,w/2+8,window.innerWidth-w/2-8);ty=clamp(y-8,h/2+8,window.innerHeight-h/2-8)
cx=shown?cx:tx;cy=shown?cy:ty}
function show(i,x,y){if(i!==active){active=i;fill(i);Array.prototype.forEach.call(grid.children,function(r,k){r.classList.toggle('active',k===i)})}
place(x,y);if(!shown){shown=true;cx=tx;cy=ty;pop.classList.add('show');pop.setAttribute('aria-hidden','false');if(!raf)loop()}}
function hide(){shown=false;pop.classList.remove('show');pop.setAttribute('aria-hidden','true');Array.prototype.forEach.call(grid.children,function(r){r.classList.remove('active')})}
function loop(){
var k=reduce?1:clamp(S.follow/30,.08,1);cx+=(tx-cx)*k;cy+=(ty-cy)*k
pop.style.left=cx+'px';pop.style.top=cy+'px'
if(S.parallax){var dx=(tx-cx)*(S.follow/60),dy=(ty-cy)*(S.follow/60);body.style.transform='translate('+clamp(dx,-6,6)+'px,'+clamp(dy,-6,6)+'px)'}
else body.style.transform='none'
if(shown)raf=requestAnimationFrame(loop);else raf=null}
function apply(){
document.body.classList.toggle('light',S.theme==='light')
document.documentElement.style.setProperty('--accent',S.accent)
document.documentElement.style.setProperty('--secondary',S.secondary)
document.documentElement.style.setProperty('--overlay',S.theme==='light'?'rgba(255,255,255,.88)':'rgba(23,22,39,.88)')
document.documentElement.style.setProperty('--blur',S.blur+'px')
document.documentElement.style.setProperty('--radius',S.radius+'px')
document.documentElement.style.setProperty('--dur',Math.round(200+S.follow*12)+'ms')
el('phead').textContent=S.heading;el('popFoot').style.display=S.showMeta?'flex':'none'
el('credit').style.display=S.credit?'flex':'none'
parsePeople();renderList();fill(active<0?0:active)
}
SPEC.forEach(function(s){
var lab=document.createElement('label'),txt=document.createElement('span'),inp
txt.textContent=s.label
if(s.type==='toggle'){inp=document.createElement('input');inp.type='checkbox';lab.className='ck'}
else if(s.type==='select'){inp=document.createElement('select');s.options.forEach(function(o){var op=document.createElement('option');op.value=o;op.textContent=o;inp.appendChild(op)})}
else if(s.type==='textarea'){inp=document.createElement('textarea')}
else{inp=document.createElement('input');inp.type=s.type;if(s.min!==undefined){inp.min=s.min;inp.max=s.max;inp.step=s.step}}
inp.value=String(S[s.key]);if(s.type==='toggle')inp.checked=!!S[s.key]
var out=document.createElement('b');if(s.type!=='textarea'){out.textContent=String(S[s.key])}
inp.addEventListener('input',function(){var v=s.type==='toggle'?inp.checked:(s.type==='range'?+inp.value:inp.value)
S[s.key]=v;if(out)out.textContent=String(v);apply()})
lab.appendChild(txt);lab.appendChild(inp);if(out)lab.appendChild(out);el('ctl').appendChild(lab)
})
stage.addEventListener('pointermove',function(e){var r=stage.getBoundingClientRect();stage.style.setProperty('--sx',(e.clientX-r.left)+'px');stage.style.setProperty('--sy',(e.clientY-r.top)+'px')})
document.addEventListener('pointerdown',function(e){if(touch&&!e.target.closest('.person')){hide();touch=false}})
document.addEventListener('keydown',function(e){if(e.key==='Escape'){hide();touch=false}})
apply()
window.addEventListener('message',function(e){
if(!e.data||e.data.type!=='ub:props'||e.data.key!==KEY||!e.data.props)return
Object.keys(e.data.props).forEach(function(k){if(k in S)S[k]=e.data.props[k]})
var box=el('ctl');if(box)Array.prototype.forEach.call(box.children,function(lab,i){var s=SPEC[i],v=S[s.key],inp=lab.querySelector('input,select,textarea'),out=lab.querySelector('b')
if(!inp)return;if(s.type==='toggle')inp.checked=!!v;else inp.value=String(v);if(out)out.textContent=String(v)})
active=-1;apply()
})
})();
</script>相似组件
按标签重合度推荐,不是运营手配的榜单。





