Focus cards
聚焦虚化卡片
Keep one card sharp while its neighbors recede.
Also searched as: 鼠标停在一张卡片上,其他卡片变模糊, 其余的卡片变淡、虚化,只有这一张是清楚的, 一组照片里看哪张,别的就虚掉, 悬停聚焦,其他变暗, 聚焦卡片
- Placement
- Card list, Image
- Mood
- Refined, Soft
- Trigger
- 悬停
- Site types
- 作品集, 博物馆和展览, 个人网站
How it works & accessibility
When a card is hovered or focused, blur and dim the other cards while leaving the active card sharp.
Best for: Card list, Image (悬停).
Reduced motion: 照样变模糊、变淡,但不做过渡,直接到位
Mobile & touch: 手机上没有悬停:手指按住一张时其余的变淡(:active),松开恢复,点一下照常打开;iOS Safari 要页面上有触摸监听才认 :active
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 时长 | duration | 300ms | — | — |
| 其余卡片的模糊 | blur | 4px | — | — |
| 其余卡片的不透明度 | dim | 60% | — | — |
| 缓动 | easing | cubic-bezier(0.16, 1, 0.3, 1) | — | — |
Ready-to-paste AI prompt
Add “Focus cards” to the card list in my project: When a card is hovered or focused, blur and dim the other cards while leaving the active card sharp.
Parameters: duration: 300ms, blur: 4px, dim: 60%, easing: cubic-bezier(0.16, 1, 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/focus-cards.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 聚焦虚化卡片(Focus cards)· 只用 CSS,不需要脚本。
思路来自 Aceternity UI 的 Focus Cards(鼠标停在一张上,其余的变模糊、缩小一点);没有用它的代码,这里用 :has() 自己写。
给一组卡片的外框加上 class="focus-cards"(卡片是它的直接子元素)。鼠标停在一张上、或者键盘选中一张(或它里面的链接)时,
其余的变模糊(--focus-blur)、变淡(--focus-dim)、缩小一点;离开时都恢复。
- 鼠标在卡片之间的空隙里时,没有停在任何一张上,都恢复(过渡会把这一下抹平,不闪)。
- 悬停只在能悬停的设备上生效(手机点一下以后 :hover 会一直留着);手机上手指按住一张时(:active)其余的变淡,松开恢复。
iOS Safari 要页面上有触摸监听才认 :active:页面里别处已经有就不用管,没有的话给 body 加 ontouchstart(空的就行)。
- 想让某一张一直处在聚焦的样子(比如用脚本挑一张),加 class="is-focus"。
- 不认 :has() 的旧浏览器上什么都不发生,卡片照常显示。
- 卡片(外框的直接子元素)的 transition 会被重写(transition 只能写一行,后写的盖掉先写的):元素原来就有过渡的(比如悬停变色),把原来的几项并进这一行。
- 开启"减少动态"时:照样变模糊、变淡,不做过渡。 */
.focus-cards {
--focus-duration: 300ms;
--focus-blur: 4px;
--focus-dim: 60%;
--focus-easing: cubic-bezier(0.16, 1, 0.3, 1);
}
.focus-cards > * {
transition:
filter var(--focus-duration) var(--focus-easing),
opacity var(--focus-duration) var(--focus-easing),
scale var(--focus-duration) var(--focus-easing);
}
/* 有一张"被看着"时,其余的变淡。被看着:鼠标停着、键盘选中了它或它里面的东西、加了 is-focus、手指按着 */
@media (hover: hover) {
.focus-cards:is(:has(> :hover), :has(:focus-visible), :has(> .is-focus)) > :not(:hover, :focus-within, .is-focus) {
filter: blur(var(--focus-blur));
opacity: var(--focus-dim);
scale: 0.98;
}
}
@media (hover: none) {
.focus-cards:is(:has(> :active), :has(:focus-visible), :has(> .is-focus)) > :not(:active, :focus-within, .is-focus) {
filter: blur(var(--focus-blur));
opacity: var(--focus-dim);
scale: 0.98;
}
}
@media (prefers-reduced-motion: reduce) {
.focus-cards > * { transition: none; }
}HTML
<!-- 聚焦虚化卡片:一组卡片放在同一个外框里,外框加上 class="focus-cards"(卡片是它的直接子元素),不需要脚本 -->
<div class="gallery focus-cards">
<a class="photo" href="/photos/mountain">
<img src="mountain.jpg" alt="雪山">
<span>山</span>
</a>
<a class="photo" href="/photos/sea">
<img src="sea.jpg" alt="傍晚的海">
<span>海</span>
</a>
<a class="photo" href="/photos/city">
<img src="city.jpg" alt="夜里的街道">
<span>城</span>
</a>
</div>