Communitygithub.com

liquid-glass

在瀏覽器中建立真正的 Apple 風格液態玻璃效果 — 透過 SVG 置換貼圖實現真實折射、色散與光澤邊緣。適合用於液態玻璃、玻璃擬態、磨砂/玻璃卡片、玻璃導航列,或當純粹的模糊「玻璃」效果過於平板時使用。包含導致 `backdrop-filter` 在滾動時卡頓的效能陷阱,以及讓玻璃上文字難以閱讀的對比陷阱。

liquid-glass 是什麼?

這份技能文件詳細說明了如何在不依賴 WebGL 的情況下,僅使用 CSS 與 SVG 篩選器,在瀏覽器中實現逼真的液態玻璃效果。核心技術在於利用 `feDisplacementMap` 搭配法線貼圖,模擬光線通過玻璃時邊緣彎曲、中心穿透的真實折射行為,而非僅用 `backdrop-filter: blur()` 產生模糊塑膠感。同時,透過三次不同尺度的置換貼圖並分別提取 R/G/B 通道再混合,實現色散效果(邊緣色邊)。文件也強調了重要的效能陷阱:`backdrop-filter` 會在元素滾動時重新計算,可能導致高達 20% 的影格遺失;解決方案是將背景內容的靜態副本放入元素內部,並對該副本套用一般 `filter`,僅需在載入或縮放時更新一次,即可將濾鏡成本降至 1-2%。此外,還詳細說明了玻璃上文字對比的測量與調整方法、階層問題的遮蔽陷阱,以及如何透過條紋測試圖案驗證效果的真實折射。此技巧支援所有現代瀏覽器,但需特別注意 Safari 對 SVG 篩選器在 `backdrop-filter` 中的支援狀況。

相容平台~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 + 法線貼圖
色散R/G/B 各色光彎曲程度不同 → 產生色邊三次置換處理,各取一個通道再混合
菲涅爾 / 鏡面高光邊緣捕捉光線在元素頂端少許百分比處加上漸層
模糊柔化feGaussianBlur,適度——形狀必須保留

只有第一項是沒得妥協的。沒有位移,就不是玻璃。

可運作的實作

法線貼圖編碼了表面斜率:R = 水平方向,G = 垂直方向,中灰色 (128) = 平坦。 中心保持中性,只在斜邊帶(bevel band)內快速變化——這正是讓中心光學平坦、邊緣彎曲的原因,如同鏡片。使用 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)   # 90th pct = 最糟狀況
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 per px)。

降級方案

將增強功能包裝起來,讓不支援的瀏覽器仍能呈現正確的效果:

@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .card { background: <不透明的漸層>; }   /* 半透明 + 清晰的背景 = 壞掉的畫面 */
}

Safari 對於在 backdrop-filter 中使用 SVG (url()) 篩選器的支援,是整個技巧中最不穩定的部分——CSS.supports() 可能會回報 true,但實際上渲染錯誤。在實際 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.

相關技能