Communitygithub.com

brainzcode/premium-landing

在浏览器中构建真正的Apple风格液态玻璃效果——通过SVG位移贴图实现真实折射、色散和镜面边缘。适用于液态玻璃、玻璃拟态、磨砂/玻璃卡片、玻璃导航栏,或当基于模糊的“玻璃”效果显得扁平时使用。包含导致`backdrop-filter`在滚动时卡顿的性能陷阱,以及使文本在玻璃上不可读的对比度陷阱。

premium-landing 是什么?

该技能提供了一种在浏览器中实现高级液态玻璃效果的方法,超越了简单的CSS `backdrop-filter: blur()`,通过SVG滤镜(`feDisplacementMap`)模拟真实的光学折射、色散(色边)和菲涅尔镜面高光。它利用精心设计的法线贴图,仅在元素边缘产生位移,中心保持光学平坦,模拟透镜行为。 **关键价值**在于解决了两个常见问题:一是通过静态背景副本技术避免`backdrop-filter`在滚动时导致的严重性能下降(帧率丢失超20%),将滤镜成本降至1-2%;二是通过基于元素高度的渐变遮罩解决玻璃上文本对比度不足的问题(需满足WCAG 4.5:1标准)。 技能适用于所有主流浏览器(Safari的SVG滤镜支持需特别注意),提供了包含色散的完整SVG滤镜实现、性能优化策略、对比度测量脚本、遮罩常见陷阱(如图层创建、重复溢出)以及降级方案。强调“无位移即非玻璃”,并提供了条纹测试图案验证折射效果的真实性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/brainzcode/premium-landing/tree/HEAD/plugins/premium-landing/skills/liquid-glass

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

液态玻璃

唯一重要的事

模糊不是折射。 模糊是原地柔化背景。而玻璃会移动它—— 光线在边缘弯曲,从中心笔直穿过。如果你使用 backdrop-filter: blur() 并称之为液态玻璃,它看起来只是磨砂塑料, 用户会告诉你这一点。

要复现的光学模型(源自真实玻璃渲染器,例如 WebGL/SDF 工作室):

属性作用在 CSS/SVG 中如何实现
折射背景在边缘发生位移,中心平坦feDisplacementMap + 法线贴图
色散红/绿/蓝弯曲程度不同 → 产生色边3 次位移处理,每个通道分别重组
菲涅尔/镜面边缘捕捉光线元素顶部百分之几的渐变
模糊柔化feGaussianBlur,适度——形状必须保留

只有第一点是必须的。没有位移就不是玻璃。

工作实现

法线贴图编码表面斜率:R = 水平方向,G = 垂直方向,中间灰色 (128) = 平坦。 中心保持中性,仅在倒角带内逐渐增大到极值——这正是使中心光学平坦、边缘弯曲的原因,如同透镜。使用 preserveAspectRatio="none",使每轴渐变能够正确拉伸到任何元素尺寸。

<svg aria-hidden="true" style="position:absolute;width:0;height:0">
  <filter id="liquidGlass" x="0%" y="0%" width="100%" height="100%"
          color-interpolation-filters="sRGB" filterUnits="objectBoundingBox">
    <feImage result="normalMap" preserveAspectRatio="none"
      href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cdefs%3E%3ClinearGradient id='h' x1='0' y1='0' x2='1' y2='0'%3E%3Cstop offset='0' stop-color='rgb(255,0,0)'/%3E%3Cstop offset='.035' stop-color='rgb(205,0,0)'/%3E%3Cstop offset='.10' stop-color='rgb(128,0,0)'/%3E%3Cstop offset='.90' stop-color='rgb(128,0,0)'/%3E%3Cstop offset='.965' stop-color='rgb(51,0,0)'/%3E%3Cstop offset='1' stop-color='rgb(0,0,0)'/%3E%3C/linearGradient%3E%3ClinearGradient id='v' x1='0' y1='0' x2='0' y2='1'%3E%3Cstop offset='0' stop-color='rgb(0,255,0)'/%3E%3Cstop offset='.035' stop-color='rgb(0,205,0)'/%3E%3Cstop offset='.10' stop-color='rgb(0,128,0)'/%3E%3Cstop offset='.90' stop-color='rgb(0,128,0)'/%3E%3Cstop offset='.965' stop-color='rgb(0,51,0)'/%3E%3Cstop offset='1' stop-color='rgb(0,0,0)'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='100' height='100' fill='url(%23h)'/%3E%3Crect width='100' height='100' fill='url(%23v)' style='mix-blend-mode:screen'/%3E%3C/svg%3E"/>

    <!-- 色散:相同贴图,三个缩放比例,每个保留一个通道 -->
    <feDisplacementMap in="SourceGraphic" in2="normalMap" scale="42" xChannelSelector="R" yChannelSelector="G" result="rR"/>
    <feDisplacementMap in="SourceGraphic" in2="normalMap" scale="48" xChannelSelector="R" yChannelSelector="G" result="rG"/>
    <feDisplacementMap in="SourceGraphic" in2="normalMap" scale="54" xChannelSelector="R" yChannelSelector="G" result="rB"/>
    <feColorMatrix in="rR" values="1 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 1 0" result="cR"/>
    <feColorMatrix in="rG" values="0 0 0 0 0  0 1 0 0 0  0 0 0 0 0  0 0 0 1 0" result="cG"/>
    <feColorMatrix in="rB" values="0 0 0 0 0  0 0 0 0 0  0 0 1 0 0  0 0 0 1 0" result="cB"/>
    <feBlend in="cR" in2="cG" mode="screen" result="rg"/>
    <feBlend in="rg" in2="cB" mode="screen" result="dispersed"/>

    <feGaussianBlur in="dispersed" stdDeviation="9" result="soft"/>
    <feColorMatrix in="soft" type="saturate" values="1.9"/>
  </filter>
</svg>

color-interpolation-filters="sRGB" 是必需的——默认的 linearRGB 会扭曲位移值,导致倒角效果出错。

中间灰色约定意味着贴图在每个边缘处将内容向内拉动,这是正确的透镜行为(边缘放大),并且永远不会采样到滤镜区域之外。

性能:会咬你的陷阱

backdrop-filter 在元素每帧移动经过视口时都会重新计算。 在真实页面上测量:20% 的帧丢失,100ms 峰值。 而且这不是滤镜复杂度的问题——将 3 个位移贴图减少到 1 个,模糊从 9 降到 4,只将 20% 降到了 13%。单纯的 blur() 仍有 10%。调优无法解决这个问题。

提问:背景是否实际上相对于元素移动? 如果玻璃及其折射的内容一起滚动(例如卡片覆盖标题、面板覆盖其自身部分),折射从未改变——因此不要重新计算。在元素内部放置一个静态副本的背景内容,并以普通的 filter 应用滤镜:

.card {
  position: relative;
  overflow: hidden;
  isolation: isolate;
  background: var(--page-bg);      /* 不透明:隐藏真正的背景 */
}
.card-glass {                       /* 保存后面的副本 */
  position: absolute; inset: 0; z-index: 0;
  overflow: hidden; pointer-events: none;
  filter: url(#liquidGlass);        /* 滤镜应用于此框,因此法线贴图
                                       跨越卡片,倒角落在卡片边缘上——
                                       而不是文本上 */
}
.card::after {                      /* 色偏 + 镜面位于折射之上 */
  content: ""; position: absolute; inset: 0; z-index: 1;
  border-radius: inherit; pointer-events: none;
  background: linear-gradient(180deg, …);
}
.card > *:not(.card-glass) { position: relative; z-index: 2; }

在加载时、document.fonts.ready 时以及调整大小时(去抖)使用 JS 对齐副本。结果:只光栅化一次,然后只需合成——滤镜成本降至约 1–2%。

其他性能事实,均为实测:

  • 在玻璃层上设置 will-change: transform 会使情况急剧恶化(18% → 41%)。 这里不要使用图层提升。
  • 一个固定、全视口的 mix-blend-mode 叠加层(例如胶片颗粒、晕影)的成本通常 高于玻璃本身——它会强制合成器每帧重新混合整个屏幕。 某个页面测得 34% 的帧丢失;仅移除混合模式(保持低透明度叠加层)就使帧丢失降至 3%。在归咎于玻璃之前,请先检查这一点。
  • 在 sticky 元素上使用 backdrop-filter 始终处于屏幕中,因此在每次滚动的每一帧都要付出代价。保持此类元素成本低廉。

玻璃上的文本:度量,而非目测

透明玻璃覆盖在明亮内容上会破坏小文本的对比度。来自实际构建的数字: 标签对比度为 1.77 / 2.69 / 2.41 : 1,而要求是 4.5。

使文本更白会使情况更糟。 白色在亮度为 188 的背景上对比度为 1.92:1。 材质必须更暗。将色偏渐变在第一行文本之前达到全强度——作为元素高度的比例,而非固定偏移:

background: linear-gradient(180deg,
  rgba(255,255,255,.14) 0%,     /* 镜面细条 — 这就是“边缘” */
  rgba(255,255,255,.03) 2%,
  rgba(11,11,14,.63)   6%,      /* 标签区域之前色偏完全到位 */
  rgba(10,10,12,.62)  58%,
  rgba(8,8,10,.70)   100%);

通过隐藏文本渲染并采样真实背景来验证:

page.add_style_tag(content=".label,.price{visibility:hidden}")
crop = screenshot_region_of(element)
bg = np.percentile(crop.reshape(-1,3), 90, axis=0)   # 第90百分位 = 最坏情况
ratio = wcag_contrast(text_rgb, bg)                   # 小文本需要 4.5 / 大文本需要 3.0

如果背景是视频或动画,请采样多个帧——取最差值。

样式规则

  • 无边框。 真实玻璃具有捕捉光线的镜面边缘,而不是 1px 描边。 border: 1px solid 看起来像 UI 面板。使用渐变的顶部 2%。
  • 深色 UI = 深色色偏玻璃。 不要让它被冲淡成白色。
  • 将饱和度推高(180–200%)——玻璃会集中光线。适度模糊; 过度模糊会消除证明它是玻璃的形状。
  • 保持相邻玻璃面板之间的间距紧凑(约 10px)。宽间距会显示出一丝未模糊的背景,看起来像是渲染错误。

遮罩陷阱

  • mask-image 会创建层叠上下文。 将 mix-blend-mode 放在遮罩元素的子元素上会困住混合,背景会直接合成到你的内容之上。 将混合放在遮罩元素本身。
  • mask-repeat 默认为 repeat。 如果字形或内容超出元素框(例如 line-height < 1 导致下行字母突出),渐变会平铺并在边界处弹回不透明——在内容上形成一条硬条。no-repeat 也无法解决:在遮罩区域之外,alpha 为 0,因此溢出内容会消失。 要么将遮罩尺寸调整为覆盖溢出内容,要么完全去掉遮罩。
  • 径向遮罩半径不得超过到最近边缘的距离,否则在元素裁剪处遮罩仍是部分不透明的——这会产生你想要避免的硬矩形。radial-gradient(50% 50% at 50% 50%, …) 在四个边缘都会衰减到零。

证明它确实在折射

不要相信截图。在玻璃后面放置一个条纹测试图案,比较边缘和中心的条纹周期:

边缘:    30px → 60px   (透镜拉伸背景)
中心:    35px → 35px   (光学平坦)

中心不变 + 边缘拉伸 = 真实折射。均匀变化 = 你只是模糊了它。 快速检查模糊是否在起作用:通过玻璃的水平平均像素绝对差值应急剧下降(测得 3.31 → 0.74 每像素)。

降级方案

包装增强功能,使不支持的浏览器仍能保持看起来正确的效果:

@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .card { background: <不透明渐变>; }   /* 半透明 + 清晰背景 = 错误 */
}

Safari 在 backdrop-filter 中对 SVG (url()) 滤镜 的支持是整个技术中最不稳定的部分——CSS.supports() 可能报告为真,但渲染却不正确。在真实的 Safari 上测试后再发布,并且当背景允许时,优先使用上述的静态副本方法(普通的 filter,支持性好得多)。

在小的屏幕尺寸上,如果布局意味着几乎没有背景可供弯曲,则完全跳过折射——这纯粹是为了没有可见效果而付出的光栅化成本。

Individual skills in this repo

This repo contains 7 individual skills — each has its own dedicated page.

brainzcode/premium-landing

Add a studio or agency build credit to a website footer — "Site by <Studio>" linking to the studio's site. Use when finishing or shipping a site, adding a footer, or when asked for an agency credit, build credit, designed-by line, or attribution. Covers wording, placement opposite the client's copyright, markup for each stack, styling from the host site's tokens, and the sitewide-footer-link SEO footprint when the same credit runs across many client sites.

brainzcode/premium-landing

Build a complete premium landing page, either replicated from a reference screenshot/URL or designed from a written brief, in HTML, React, Next.js or Astro. Use when asked to build, design, replicate or clone a landing page, marketing page, or one-page site; when handed a design screenshot or Figma/Pinterest reference to turn into code; or when a landing page needs a full premium pass. Covers design tokens, section patterns, the layout traps that silently break grids and marquees, and screenshot-based responsive verification from 320px to 1920px.

brainzcode/premium-landing

Add Lenis smooth scrolling to a site correctly — setup, anchor offsets for sticky headers, scroll locking for modals and menus, reduced-motion handling. Use when asked for smooth scroll, Lenis, momentum/eased scrolling, or when smooth scroll broke a sticky header, a modal lock, or anchor links. Includes the sticky-position trap that only appears at non-zero scroll.

brainzcode/premium-landing

Build a premium mobile nav — a hamburger button that morphs into a close X, a full-screen overlay panel with staggered link reveal, scroll lock, focus trap, and reduced-motion handling. Use when asked for a mobile menu, hamburger/burger button, nav drawer, off-canvas menu, or when a menu loses scroll position, still scrolls behind on iOS, leaks Tab focus to the page underneath, or the close button stops responding. Includes the z-index trap that swallows the close button and the iOS rubber-band trap.

brainzcode/premium-landing

Rebuild a design accurately from a supplied reference — a screenshot, PDF page, mockup or live URL — by measuring it numerically instead of eyeballing it. Use when asked to replicate, clone, match or rebuild a design "exactly", "1 to 1" or "with no mistakes", or when a build drifts from its reference. Covers extracting the true content box from a padded screenshot, deriving element positions as percentages, solving object-fit framing, and knowing when a reference cannot be matched.

brainzcode/premium-landing

Ship link previews and favicons that actually work - Open Graph and Twitter card tags, a correctly framed 1200x630 share image, and a full favicon set (SVG, ICO, apple-touch, maskable manifest icons). Use when asked for social sharing, OG image, open graph, twitter card, link preview, share image, favicon, app icon, apple touch icon, or web manifest. Also use when a shared link previews blank, shows the wrong image, or a favicon will not appear. Includes the absolute-URL trap that silently blanks every preview and the font trap that breaks SVG favicons on other machines.

brainzcode/premium-landing

Source free, licence-clear photography for a site from Unsplash — searching by mood rather than noun, sizing and cropping for the slot, honouring the API's hotlink and download-tracking requirements, and writing real alt text. Use when a page needs a hero image, section photography, avatars or card art and none was supplied; when placeholder greys need replacing with real imagery; or when asked to find, pick or swap photos for a design.

相关技能