# 液态玻璃折射（Liquid glass refraction）

- 俗称：苹果液态玻璃；毛玻璃跟随反光；高光玻璃折射卡片；Liquid Glass
- 用在：卡片列表、按钮（品牌官网、工具和应用、个人网站）
- 触发：悬停
- 感觉：高级、科技感
- 版本：1，更新于 2026-12-02

## 原理

面板本身使用 backdrop-filter: blur(blur px) saturate(1.6) 配合半透明描边形成磨砂玻璃基底层，内部放置指针穿透、带 mix-blend-mode: screen 的高光层；监听指针移动（或触屏按住）把局部坐标写入 CSS 变量 --lg-x / --lg-y，渲染直径为 size px、中心亮度为 intensity% 的径向渐变高光跟随指尖折射，关闭动画或脚本未加载时仍保留清晰磨砂玻璃面板与文字。

适合：产品特性卡片、浮层工具栏、音乐控制面板与高端官网交互展台。

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `size` | 折射光径 | 200px | 120–360px |
| `intensity` | 高光强度 | 68% | 25–100% |
| `blur` | 磨砂模糊 | 14px | 4–28px |

## 质量要求

- 开启"减少动态"时：关闭高光淡入淡出过渡，保留清晰磨砂玻璃层与静置态光斑响应。
- 手机上：移动端触按时在指尖位置即时点亮折射光斑，松手后平稳收起，不阻挡滚动。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

## 提示词（可以直接交给 AI）

```text
请在我的项目里给卡片列表加上「液态玻璃折射」（Liquid glass refraction）效果：面板本身使用 backdrop-filter: blur(blur px) saturate(1.6) 配合半透明描边形成磨砂玻璃基底层，内部放置指针穿透、带 mix-blend-mode: screen 的高光层；监听指针移动（或触屏按住）把局部坐标写入 CSS 变量 --lg-x / --lg-y，渲染直径为 size px、中心亮度为 intensity% 的径向渐变高光跟随指尖折射，关闭动画或脚本未加载时仍保留清晰磨砂玻璃面板与文字。
参数：折射光径 200px，高光强度 68%，磨砂模糊 14px。
要求：保留原有的内容和布局；系统开启"减少动态"时直接显示最终状态；脚本出错或没有执行时内容仍然可见。
参考：https://donglang.linter.top/effects/liquid-glass.md（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

```html
<div class="lg-card" data-glass>
  <span class="lg-sheen" aria-hidden="true"></span>
  <div class="lg-body">
    <small>OPTICAL SURFACE</small>
    <b>液态玻璃层</b>
    <p>指针经过时高光沿边缘漫射流转</p>
  </div>
</div>
```

CSS：

```css
/* 注册成可插值的长度：演示里用 Web Animations 沿路径扫光，没注册的话浏览器只会在两个关键帧之间"跳"一下，看起来像抽帧 */
@property --lg-x { syntax: '<length-percentage>'; inherits: true; initial-value: 50%; }
@property --lg-y { syntax: '<length-percentage>'; inherits: true; initial-value: 50%; }
.lg-card {
  position: relative;
  overflow: hidden;
  border-radius: 18px;
  background: rgb(255 255 255 / 0.1);
  border: 1px solid rgb(255 255 255 / 0.28);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.42), 0 18px 40px -20px rgb(0 0 0 / 0.65);
  backdrop-filter: blur(var(--lg-blur, 14px)) saturate(1.6);
  -webkit-backdrop-filter: blur(var(--lg-blur, 14px)) saturate(1.6);
}

.lg-sheen {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  opacity: 0;
  mix-blend-mode: screen;
  background-image: radial-gradient(
    calc(var(--lg-size, 200px) / 2) calc(var(--lg-size, 200px) / 2) at var(--lg-x, 50%) var(--lg-y, 50%),
    rgb(255 255 255 / 0.9),
    rgb(187 230 232 / 0.42) 38%,
    transparent 74%
  );
  transition: opacity 220ms cubic-bezier(0.16, 1, 0.3, 1);
}

.lg-card.is-lit .lg-sheen {
  opacity: var(--lg-alpha, 0.68);
}

.lg-body {
  position: relative;
  z-index: 1;
}

@media (prefers-reduced-motion: reduce) {
  .lg-sheen {
    transition: none;
  }
}
```

JS（原生，不依赖任何库）：

```js
// 液态玻璃折射（Liquid glass refraction）· 原生 JS，不依赖任何库。
// 用法：const ctl = liquidGlass(root, { size: 200, intensity: 68, blur: 14 });
// 支持 pointer 跟随与触屏按压点亮，返回值包含 show(x, y)、move(x, y)、hide()、destroy()。
export function liquidGlass(root, {
  size = 200,
  intensity = 68,
  blur = 14,
  ignoreReducedMotion = false,
} = {}) {
  if (!root) return null;
  const reduce = !ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches;
  const alpha = Math.max(0, Math.min(1, intensity / 100));

  let sheen = root.querySelector('.lg-sheen');
  const createdSheen = !sheen;
  if (!sheen) {
    sheen = document.createElement('span');
    sheen.className = 'lg-sheen';
    sheen.setAttribute('aria-hidden', 'true');
    root.prepend(sheen);
  }

  const prevSize = root.style.getPropertyValue('--lg-size');
  const prevAlpha = root.style.getPropertyValue('--lg-alpha');
  const prevBlur = root.style.getPropertyValue('--lg-blur');
  const prevTransition = sheen.style.transition;

  root.classList.add('lg-card');
  root.style.setProperty('--lg-size', `${size}px`);
  root.style.setProperty('--lg-alpha', String(alpha));
  root.style.setProperty('--lg-blur', `${blur}px`);
  if (reduce) sheen.style.transition = 'none';

  const move = (x, y) => {
    root.style.setProperty('--lg-x', `${x}px`);
    root.style.setProperty('--lg-y', `${y}px`);
  };
  const show = (x, y) => {
    move(x, y);
    root.classList.add('is-lit');
  };
  const hide = () => {
    root.classList.remove('is-lit');
  };

  const local = (e) => {
    const r = root.getBoundingClientRect();
    const kx = r.width ? root.offsetWidth / r.width : 1;
    const ky = r.height ? root.offsetHeight / r.height : 1;
    return [(e.clientX - r.left) * kx - root.clientLeft, (e.clientY - r.top) * ky - root.clientTop];
  };

  const listeners = [
    ['pointerenter', (e) => { if (e.pointerType !== 'touch') show(...local(e)); }],
    ['pointermove', (e) => { if (e.pointerType !== 'touch' || root.classList.contains('is-lit')) move(...local(e)); }],
    ['pointerleave', (e) => { if (e.pointerType !== 'touch') hide(); }],
    ['pointerdown', (e) => { if (e.pointerType === 'touch') show(...local(e)); }],
    ['pointerup', (e) => { if (e.pointerType === 'touch') hide(); }],
    ['pointercancel', (e) => { if (e.pointerType === 'touch') hide(); }],
  ];
  listeners.forEach(([type, fn]) => root.addEventListener(type, fn));

  return {
    show,
    move,
    hide,
    destroy() {
      listeners.forEach(([type, fn]) => root.removeEventListener(type, fn));
      root.classList.remove('is-lit');
      root.style.removeProperty('--lg-x');
      root.style.removeProperty('--lg-y');
      if (prevSize) root.style.setProperty('--lg-size', prevSize);
      else root.style.removeProperty('--lg-size');
      if (prevAlpha) root.style.setProperty('--lg-alpha', prevAlpha);
      else root.style.removeProperty('--lg-alpha');
      if (prevBlur) root.style.setProperty('--lg-blur', prevBlur);
      else root.style.removeProperty('--lg-blur');
      sheen.style.transition = prevTransition;
      if (createdSheen) sheen.remove();
    },
  };
}
```

## 出处

- 本站原创实现（参考苹果光学玻璃层次与指针高光设计语言）
