Q06-导航按钮高级拟态玻璃效果构建完全指南

06-导航按钮高级拟态玻璃效果构建完全指南 🪟

本文档详细讲解如何在没有背景图片的导航页面上,通过纯 CSS 技术为每个导航按钮构建高级拟态玻璃(Glassmorphism)效果。涵盖从基础原理到 Apple Liquid Glass、Honor MagicOS 风格的完整实现方案,以及交互状态增强和完整可运行示例 🚀

This document provides a comprehensive guide to building advanced glassmorphism effects for navigation buttons on a page without background images, covering everything from fundamental principles to Apple Liquid Glass and Honor MagicOS style implementations, interactive state enhancements, and complete runnable examples 🚀


术语表 / Terminology

术语 / Term 说明 / Description
Glassmorphism 拟态玻璃设计风格,通过透明度和模糊营造毛玻璃效果
backdrop-filter CSS 属性,对元素后方区域应用模糊等图形效果
Liquid Glass Apple 在 WWDC 2025 推出的动态玻璃设计语言,具有表面高光和动态模糊特性
RGBA 红绿蓝 + Alpha 通道的颜色模型,用于控制透明度
Pseudo-element CSS 伪元素(::before / ::after),用于在不额外添加 HTML 的情况下创造装饰层
Mask Image CSS 遮罩技术,控制元素某部分的可见性
Blur Radius 模糊半径,决定模糊效果的强度
Refraction 折射效果,光线穿过玻璃时发生的弯曲现象
Specular Highlight 镜面高光,光线在玻璃表面的反射亮点
FeTurbulence SVG 滤镜,用于生成噪点纹理模拟玻璃表面粗糙度

章节阅读路线图 🗺️ / Chapter Reading Roadmap

  1. 核心原理 🧠 / Core Principle → 理解无背景图场景下玻璃效果的技术原理
  2. 基础玻璃效果实现 🪟 / Basic Glass Effect → 三种核心技术路线
  3. Apple Liquid Glass 风格实现 🍎 / Apple Liquid Glass Style → 复现 Apple 的动态玻璃效果
  4. Honor MagicOS 风格实现 📱 / Honor MagicOS Style → 实现荣耀的液态玻璃风格
  5. 交互状态增强 ✨ / Interactive State Enhancement → Hover、Active、选中态动效
  6. 完整可运行示例 🎯 / Complete Runnable Example → 整合所有技术的导航页面
  7. 总结 📝 / Summary → 回顾核心要点与最佳实践

1. 核心原理 🧠 / Core Principle

🧠 Note: 本章解释没有背景图片时如何实现玻璃效果的技术原理 / This chapter explains the technical principles behind achieving glass effects without background images.

1.1 传统 Glassmorphism 的依赖条件 / Traditional Glassmorphism Dependencies

标准的 Glassmorphism 效果依赖两个核心条件:

css 复制代码
/* 标准玻璃效果核心代码 */
.glass {
  background: rgba(255, 255, 255, 0.15);   /* 半透明背景 */
  backdrop-filter: blur(12px);              /* 背景模糊 */
}

传统实现要求元素后方有视觉内容 (图片、渐变、其他 UI 元素等),backdrop-filter: blur() 通过模糊这些内容来营造毛玻璃的透明质感。如果没有背景图片,backdrop-filter 没有内容可模糊,玻璃效果就无法显现。

1.2 无背景图场景的破局思路 / Breaking Through Without Background Images

当导航页面没有背景图片时,需要通过人工构建深度层次来取代自然背景:

  1. 自建背景层 🏗️:在玻璃层后方用 CSS Gradient 创建色彩丰富、有深度感的背景
  2. 多层玻璃叠加 📚:利用多个半透明层互相堆叠,产生光学上的深度幻觉
  3. 光影模拟 💡:通过 box-shadow(inset + outset)、border 和伪元素模拟玻璃的折射和高光
  4. 噪点纹理 🌫️:使用 SVG <feTurbulence> 滤波器或 CSS 噪点图案模拟玻璃表面的细微粗糙度

核心公式 / Core Formula:

scss 复制代码
视觉效果 = 背景色彩层 + 基础玻璃层(blur + opacity) + 边缘光效层(border + shadow) + 高光反射层(pseudo-element)

1.3 视觉层次架构 / Visual Layer Architecture

css 复制代码
用户视角 → 导航按钮文字
               ↓
         玻璃表面层(高光、反光伪元素)
               ↓
         玻璃主体层(backdrop-filter: blur + rgba背景)
               ↓
         背景色彩层(CSS渐变、噪点纹理)
               ↓
         页面基础底色

每一层都是独立的 CSS 层,通过 position: absolutez-index 堆叠。正是这种层次堆叠,在没有背景图片的情况下創造了玻璃的视觉深度。


参考资料:


2. 基础玻璃效果实现 🪟 / Basic Glass Effect

🪟 Note: 本章介绍三种核心技术路线,从简单的 box-shadow 到高级的伪元素模糊,再到结合所有技术的完整方案 / This chapter introduces three core technical approaches, from simple box-shadow to advanced pseudo-element blurring, to a complete combined solution.

2.1 方法一:box-shadow + RGBA 背景层 / Method 1: box-shadow + RGBA Background

这是最直接的实现方式,适合快速原型或性能敏感场景:

css 复制代码
/* 方法一:基础玻璃效果 🪟 */
.nav-btn-glass {
  background: rgba(255, 255, 255, 0.08);              /* 半透明背景,数值越小越透 */
  border: 1px solid rgba(255, 255, 255, 0.1);          /* 玻璃边缘线条 */
  border-radius: 14px;                                  /* 圆角,模拟玻璃边缘 */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),            /* 顶部内发光:模拟上方光源 */
    inset 0 -1px 0 rgba(0, 0, 0, 0.05),                /* 底部内阴影:模拟底部暗面 */
    0 4px 16px rgba(0, 0, 0, 0.1);                      /* 外部投影:玻璃悬浮感 */
}

原理分析 / How It Works:

  • rgba(255, 255, 255, 0.08) --- 8% 不透明度的白色背景,让底层颜色透出
  • border: 1px solid rgba(255, 255, 255, 0.1) --- 10% 白边,模拟玻璃切面的边缘亮度
  • inset box-shadow 模拟玻璃的厚度感:顶部亮(光线从上方来),底部暗
  • outset box-shadow 让玻璃元素在页面上有"悬浮"的物理感

适合场景:导航容器背景、次要按钮、静态装饰元素

2.2 方法二:伪元素模糊层 / Method 2: Pseudo-element Blur Layer

backdrop-filter 没有内容可以模糊时,可以用伪元素创建一个自包含的模糊层

css 复制代码
/* 方法二:伪元素模糊玻璃 🪟 */
.nav-btn-pseudo {
  position: relative;                                   /* 为伪元素定位提供锚点 */
  background: transparent;                               /* 完全透明,露出伪元素层 */
  border: 1px solid rgba(255, 255, 255, 0.15);          /* 玻璃边缘 */
  border-radius: 14px;                                  /* 圆角 */
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);           /* 外部投影 */
  z-index: 1;                                            /* 确保文字在伪元素之上 */
}

.nav-btn-pseudo::before {
  content: '';                                          /* 必须设置 */
  position: absolute;                                   /* 绝对定位 */
  inset: 0;                                             /* 覆盖整个父元素 */
  background: linear-gradient(                          /* 渐变背景模拟玻璃后的色彩 */
    135deg,
    rgba(255, 255, 255, 0.2) 0%,
    rgba(255, 255, 255, 0.05) 50%,
    rgba(200, 220, 255, 0.1) 100%
  );
  border-radius: inherit;                               /* 继承父元素圆角 */
  filter: blur(8px);                                    /* 模糊自身,模拟毛玻璃 */
  z-index: -1;                                          /* 放在父元素内容之后 */
  pointer-events: none;                                 /* 不阻挡交互 */
}

关键技巧 🔑:filter: blur(8px) 作用于伪元素自身的渐变背景,自产自销模糊内容------不需要外部背景图片。伪元素的渐变在模糊后产生柔和的光晕层,模拟玻璃的折射效果。

适合场景:在暗色或纯色背景上的导航按钮、需要立体感的单个按钮

2.3 方法三:完整玻璃层叠方案 / Method 3: Complete Glass Stack

结合 backdrop-filter 与自建背景层,实现最接近真实玻璃的效果:

css 复制代码
/* 方法三:完整玻璃层叠方案 🪟 */
.nav-container {
  /* 🏗️ 构建背景色彩层:模拟玻璃后会有的彩色环境 */
  background:
    radial-gradient(ellipse at 20% 50%, rgba(120, 180, 255, 0.15) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 50%, rgba(255, 180, 240, 0.1) 0%, transparent 60%),
    linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  border-radius: 16px;
  padding: 8px;
}

/* 单个导航按钮 */
.nav-btn-stack {
  position: relative;
  background: rgba(255, 255, 255, 0.08);               /* 半透明背景 */
  backdrop-filter: blur(12px);                          /* 模糊后方的渐变背景层 */
  -webkit-backdrop-filter: blur(12px);                  /* Safari 兼容 */
  border: 1px solid rgba(255, 255, 255, 0.12);          /* 玻璃边缘 */
  border-radius: 12px;                                  /* 圆角 */
  color: rgba(255, 255, 255, 0.9);                      /* 文字颜色 */
  padding: 12px 24px;                                   /* 内边距 */
  cursor: pointer;                                      /* 交互指示 */
  transition: all 0.3s ease;                            /* 状态过渡 */
}

/* 边缘高光层:模拟玻璃切面的光泽 ✨ */
.nav-btn-stack::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.25) 0%,                       /* 左上角光源方向高光 */
    transparent 40%,
    rgba(255, 255, 255, 0.05) 80%,
    rgba(255, 255, 255, 0.1) 100%
  );
  pointer-events: none;
  z-index: 1;                                           /* 覆盖在玻璃之上 */
}

方案对比 / Approach Comparison:

特性 方法一 box-shadow 方法二 伪元素模糊 方法三 完整层叠
视觉效果 基础平面感 有模糊立体感 最接近真实玻璃
性能 最优 🚀 中等 ⚡ 较重 ⚠️
代码量 中等 较多
浏览器兼容 最优 良好 良好
适合场景 快速原型、性能优先 暗色主题按钮 高品质 UI 需求

💡 建议 🌟:移动端或性能敏感场景使用方法一;桌面端高品质体验使用方法三。


参考资料:


3. Apple Liquid Glass 风格实现 🍎 / Apple Liquid Glass Style

🍎 Note: 本章基于 WWDC 2025 发布的 Liquid Glass 设计语言,复现其在导航按钮上的核心效果 / This chapter recreates the core effects of the Liquid Glass design language announced at WWDC 2025 on navigation buttons.

3.1 Apple Liquid Glass 设计特征 / Design Characteristics

Apple 在 iOS 26 和 macOS Tahoe 中引入的 Liquid Glass 设计语言,其核心特征包括:

  1. 动态折射 🌀:玻璃背后的内容会随视角产生类似真实玻璃的光线扭曲
  2. 镜面高光 ✨:玻璃表面有流动感的高光反射,随交互动态变化
  3. 通透层次 🔍:多层玻璃叠加产生真实的景深感
  4. 边缘辉光 🌈:玻璃切面呈现微弱的彩色边缘,模拟物理玻璃的色散

⚠️ 注意:纯 CSS 无法完全复现 Apple 的实时折射效果(需要 WebGL/Shader)。本文聚焦于可用 CSS 实现的高保真近似方案。

3.2 基础 Liquid Glass 按钮 / Basic Liquid Glass Button

css 复制代码
/* 🍎 Apple Liquid Glass 风格导航按钮 */
.nav-btn-liquid {
  position: relative;
  /* 半透明白色基底:极低的透明度让背景层透出 */
  background: rgba(255, 255, 255, 0.08);
  /* 核心模糊:模拟玻璃的毛玻璃质感 */
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);  /* Safari 兼容 */
  /* 玻璃边缘:亮白色细边,模拟玻璃切面的高光 */
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 16px;                                  /* 大圆角,符合 Liquid Glass 风格 */
  /* 多层阴影营造深度 🏗️ */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),             /* 顶部内发光,模拟上方光源 */
    inset 0 -1px 0 rgba(255, 255, 255, 0.05),           /* 底部微弱光 */
    0 4px 24px rgba(0, 0, 0, 0.15),                     /* 中等投影 */
    0 1px 4px rgba(0, 0, 0, 0.1);                       /* 贴近投影 */
  color: rgba(255, 255, 255, 0.9);                      /* 浅色文字 */
  padding: 12px 28px;                                   /* 内边距 */
  cursor: pointer;                                      /* 交互指示 */
  font-size: 15px;                                      /* 字体大小 */
  font-weight: 500;                                     /* 字重 */
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);   /* 平滑过渡 */
  overflow: hidden;                                     /* 防止高光溢出 */
}

3.3 Liquid Glass 的高光模拟 / Highlights Simulation

css 复制代码
/* 🍎 Liquid Glass 高光伪元素:模拟玻璃表面的光反射 */
.nav-btn-liquid::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  /* 线性渐变模拟统一方向的光源反射 */
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.35) 0%,                       /* 左上方向强高光 */
    rgba(255, 255, 255, 0.08) 30%,                      /* 快速衰减 */
    transparent 60%,                                     /* 中间无高光区 */
    rgba(255, 255, 255, 0.03) 85%,                      /* 右侧微弱反光 */
    rgba(255, 255, 255, 0.08) 100%                      /* 右下角边缘光 */
  );
  pointer-events: none;                                 /* 不阻挡点击 */
  z-index: 1;                                           /* 显示在文字之上产生光泽感 */
}

3.4 实现多层玻璃叠加背景 / Multi-layer Glass Background

对于没有背景图片的导航,构建一个多层玻璃背景来提供 backdrop-filter 的模糊素材:

css 复制代码
/* 🍎 导航容器:构建 Liquid Glass 所需的多层背景 */
.nav-bar-liquid {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 20px;
  position: relative;
  /* ⚡ 自建多层渐变背景,模拟玻璃后会有的彩色环境 */
  background:
    /* 第一层:大范围柔光 */
    radial-gradient(ellipse at 15% 40%, rgba(100, 180, 255, 0.2) 0%, transparent 50%),
    /* 第二层:互补色柔光 */
    radial-gradient(ellipse at 85% 60%, rgba(200, 100, 255, 0.15) 0%, transparent 50%),
    /* 第三层:底部环境光 */
    radial-gradient(ellipse at 50% 100%, rgba(255, 160, 100, 0.08) 0%, transparent 40%),
    /* 基础深色背景 */
    linear-gradient(145deg, #1c1c2e 0%, #2a2a3e 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),             /* 顶部内发光 */
    0 8px 32px rgba(0, 0, 0, 0.3);                       /* 外部投影 */
}

3.5 Apple 风格 - 暗色与亮色主题适配 / Dark & Light Theme Adaptation

css 复制代码
/* 🌙 暗色主题(默认) */
.nav-btn-liquid {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.9);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    0 4px 20px rgba(0, 0, 0, 0.2);
}

/* ☀️ 亮色主题 */
@media (prefers-color-scheme: light) {
  .nav-bar-liquid {
    /* 亮色多层渐变背景:强化对比度让毛玻璃效果在浅色模式下依然可见 💪 */
    background:
      radial-gradient(ellipse at 20% 30%, rgba(180, 210, 255, 0.30) 0%, transparent 50%),
      radial-gradient(ellipse at 80% 70%, rgba(220, 180, 255, 0.22) 0%, transparent 50%),
      radial-gradient(ellipse at 50% 100%, rgba(255, 200, 120, 0.10) 0%, transparent 40%),
      linear-gradient(145deg, #dce2ec 0%, #d0d6e4 100%);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.6),
      0 8px 32px rgba(0, 0, 0, 0.10);
  }

  .nav-btn-liquid {
    background: rgba(255, 255, 255, 0.55);               /* 亮色下更不透明,增强玻璃质感 */
    border-color: rgba(255, 255, 255, 0.85);
    color: rgba(0, 0, 0, 0.85);                          /* 亮色用深色文字 */
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.9),
      0 4px 12px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(20px) saturate(180%);          /* Apple 官网在两种模式下均使用 180% */
  }
}

> 💡 **亮色模式下毛玻璃可见性技巧**:浅色背景会使 glassmorphism 效果戏剧性地减弱,原因是 `backdrop-filter` 缺少可模糊的视觉内容。Apple 官方指南也承认「玻璃、浅色模式、可读性」三者难以兼得。解决方案包括:(1)增加背景层渐变对比度,为模糊提供素材;(2)提高玻璃层不透明度至 0.55~0.80;(3)统一使用 `saturate(180%)` 增强色彩透出;(4)在 page 层面添加微妙的渐变背景,营造深度层次。

💡 Key Takeaways / 核心要点

  • backdrop-filter: blur() + saturate() 是核心 --- Apple 用 saturate(180%) 增强玻璃后的色彩饱和度 / The core is backdrop-filter: blur() + saturate() --- Apple uses saturate(180%) to enhance color saturation behind the glass
  • 多层渐变构建伪背景 --- 无背景图时用 radial/linear gradient 模拟玻璃后的彩色环境 / Multi-layer gradients create a pseudo-background when no image is available
  • 单伪元素表面高光 --- ::before 负责模拟玻璃表面的方向性高光反射 / A single pseudo-element handles directional highlight simulation on the glass surface

参考资料:


4. Honor MagicOS 风格实现 📱 / Honor MagicOS Style

📱 Note: 本章实现 Honor MagicOS 11 引入的液态玻璃导航效果,其风格相比 Apple 更强调发光感和彩色光晕 / This chapter implements the liquid glass navigation effect introduced in Honor MagicOS 11, which emphasizes glow effects and colored halos more than Apple's approach.

4.1 Honor 液态玻璃设计特征 / Honor Liquid Glass Characteristics

Honor 在 MagicOS 11 中的 Liquid Glass 设计具有独特的视觉语言:

  1. 强发光感 🌟:边缘和背景有明显的彩色发光晕染,比 Apple 更鲜艳
  2. 浮动感更强 🎈:投影更深、更扩散,元素仿佛悬浮在界面之上
  3. 动态光效 💫:按钮周围有实时渲染的光晕效果,随交互产生变化
  4. 色彩更丰富 🎨:使用紫、蓝、粉等彩色光晕,不像 Apple 那样克制
css 复制代码
/* 📱 Honor MagicOS 风格导航按钮 */
.nav-btn-honor {
  position: relative;
  /* 比 Apple 更低的透明度,让背后的彩色光晕更明显 */
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(16px) saturate(200%);
  -webkit-backdrop-filter: blur(16px) saturate(200%);
  /* 双层边框:模拟玻璃边缘 + 发光晕染 */
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 14px;
  /* 多层阴影:强调悬浮感和发光 💫 */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),             /* 顶部内发光 */
    0 8px 32px rgba(120, 80, 255, 0.15),                /* 彩色扩散投影 */
    0 4px 12px rgba(0, 0, 0, 0.2);                      /* 基础投影 */
  color: rgba(255, 255, 255, 0.95);
  padding: 12px 28px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

4.3 彩色光晕层实现 / Colored Halo Layer

Honor 风格最显著的特征是按钮周围的彩色光晕:

css 复制代码
/* 📱 紫色光晕伪元素:模拟 Honor 的彩色氛围光 */
.nav-btn-honor::before {
  content: '';
  position: absolute;
  /* 光晕略微超出按钮边界,形成发光轮廓 🌟 */
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border-radius: inherit;
  /* 彩色渐变光晕:紫→蓝→粉 🎨 */
  background: linear-gradient(
    135deg,
    rgba(160, 80, 255, 0.2) 0%,                         /* 紫色光晕 */
    rgba(80, 160, 255, 0.1) 40%,                        /* 蓝色光晕 */
    rgba(255, 100, 180, 0.15) 70%,                      /* 粉色光晕 */
    rgba(160, 80, 255, 0.05) 100%                        /* 回到紫色 */
  );
  filter: blur(12px);                                    /* 模糊扩散,形成光晕效果 */
  z-index: -1;                                           /* 放在按钮后方 */
  pointer-events: none;
  opacity: 0.8;                                          /* 默认可见 */
  transition: opacity 0.3s ease, filter 0.3s ease;
}

/* Hover 时增强光晕 */
.nav-btn-honor:hover::before {
  opacity: 1;
  filter: blur(16px);                                    /* 模糊更大 → 光晕更扩散 */
}

/* 选中/活跃态的彩色光晕增强 */
.nav-btn-honor.active::before {
  background: linear-gradient(
    135deg,
    rgba(160, 80, 255, 0.35) 0%,                         /* 更高强度的紫 */
    rgba(80, 160, 255, 0.2) 40%,
    rgba(255, 100, 180, 0.25) 70%,
    rgba(160, 80, 255, 0.1) 100%
  );
  filter: blur(20px);
  opacity: 1;
}

4.4 构建 Honor 风格的背景层 / Honor-Style Background Layer

css 复制代码
/* 📱 MagicOS 风格导航容器 */
.nav-bar-honor {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-radius: 20px;
  position: relative;
  /* 🎨 更丰富的彩色背景层:紫色调为主,配合蓝粉点缀 */
  background:
    /* 主光晕:紫色大范围柔光 */
    radial-gradient(ellipse at 30% 50%, rgba(160, 80, 255, 0.12) 0%, transparent 50%),
    /* 辅助光晕:蓝色 */
    radial-gradient(ellipse at 70% 30%, rgba(80, 160, 255, 0.1) 0%, transparent 40%),
    /* 底部环境光:粉色 */
    radial-gradient(ellipse at 50% 100%, rgba(255, 100, 180, 0.08) 0%, transparent 50%),
    /* 深色基础 */
    linear-gradient(160deg, #1a1a2e 0%, #16213e 50%, #1a1a3e 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 8px 40px rgba(0, 0, 0, 0.3);
}

4.5 Apple vs Honor 风格对比 / Style Comparison

特征 Apple Liquid Glass 🍎 Honor MagicOS 📱
透明度 中等 (rgba 0.08~0.15) 更低 (rgba 0.06)
模糊强度 blur(20px) + saturate(180%) blur(16px) + saturate(200%)
边缘高光 白色高亮边框,克制 彩色光晕晕染,张扬
阴影风格 柔和、贴近、真实 扩散、彩色、梦幻
整体感觉 真实的物理玻璃 梦幻的发光琉璃
适用场景 讲究真实感和质感的界面 追求视觉冲击力的年轻化界面

参考资料:


5. 交互状态增强 ✨ / Interactive State Enhancement

Note: 本章为导航按钮添加 Hover、Active、Focus 和选中态的动效增强 / This chapter adds hover, active, focus, and selected state motion enhancements to navigation buttons.

5.1 悬浮态(Hover)增强 / Hover State Enhancement

css 复制代码
/* 🖱️ Hover 时玻璃增厚、高光增强 */
.nav-btn-liquid:hover {
  /* 玻璃主体变化 */
  background: rgba(255, 255, 255, 0.12);               /* 背景略微不透明,玻璃"变厚" */
  border-color: rgba(255, 255, 255, 0.3);              /* 边缘更亮 */
  transform: translateY(-1px);                           /* 微上浮 */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 8px 32px rgba(0, 0, 0, 0.2),
    0 12px 40px rgba(100, 140, 255, 0.1);              /* 增加彩色投影 */
}

/* 🖱️ Hover 时高光增强 */
.nav-btn-liquid:hover::before {
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.5) 0%,                        /* 高光更强 */
    rgba(255, 255, 255, 0.1) 30%,
    transparent 60%,
    rgba(255, 255, 255, 0.05) 85%,
    rgba(255, 255, 255, 0.12) 100%
  );
}

5.2 点击态(Active)增强 / Active State Enhancement

css 复制代码
/* 👆 Active(点击)时玻璃"受压"效果 */
.nav-btn-liquid:active {
  transform: translateY(0) scale(0.97);                  /* 下沉并微缩小,模拟按压 */
  background: rgba(255, 255, 255, 0.15);               /* 更不透明 */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 2px 8px rgba(0, 0, 0, 0.15);                      /* 投影减小 */
  transition: all 0.1s ease;                             /* 点击反馈要快 */
}

/* 👆 Active 时高光变暗 */
.nav-btn-liquid:active::before {
  opacity: 0.5;
}

5.3 选中/活跃态(Active Tab) / Selected/Active Tab State

css 复制代码
/* 🎯 选中态的导航按钮:更明显、更亮 */
.nav-btn-liquid.active,
.nav-btn-liquid[aria-current="page"] {
  background: rgba(255, 255, 255, 0.15);               /* 更不透明 */
  border-color: rgba(255, 255, 255, 0.35);              /* 边缘更亮 */
  /* 选中态特有的光效 */
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    0 4px 24px rgba(100, 140, 255, 0.2),                /* 有色投影 */
    0 0 0 1px rgba(100, 140, 255, 0.1);                 /* 微弱的指示边框 */
}

/* 🎯 选中态下方指示条 */
.nav-btn-liquid.active::after {
  content: '';
  position: absolute;
  bottom: 4px;                                            /* 按钮底部 */
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 3px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.7);                 /* 白色指示条 */
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.3);         /* 发光效果 */
}

5.4 入场动画 / Entrance Animation

css 复制代码
/* 🎬 导航按钮入场动画:从透明到玻璃渐变出现 */
@keyframes glassFadeIn {
  0% {
    opacity: 0;
    backdrop-filter: blur(0px);
    transform: translateY(8px);
  }
  100% {
    opacity: 1;
    backdrop-filter: blur(20px);
    transform: translateY(0);
  }
}

.nav-btn-liquid {
  animation: glassFadeIn 0.6s cubic-bezier(0.4, 0, 0.2, 1) both;
}

/* 依次延迟入场的效果 */
.nav-btn-liquid:nth-child(1) { animation-delay: 0ms; }
.nav-btn-liquid:nth-child(2) { animation-delay: 80ms; }
.nav-btn-liquid:nth-child(3) { animation-delay: 160ms; }
.nav-btn-liquid:nth-child(4) { animation-delay: 240ms; }
.nav-btn-liquid:nth-child(5) { animation-delay: 320ms; }

5.5 性能优化建议 / Performance Optimization

css 复制代码
/* ⚡ GPU 硬件加速:确保玻璃渲染流畅 */
.nav-btn-liquid {
  /* 触发 GPU 合成层,避免重绘 */
  will-change: transform, opacity;
  /* 或使用 */
  transform: translateZ(0);                              /* 强制 GPU 加速 */
}

/* 🌐 浏览器兼容降级 */
@supports not (backdrop-filter: blur(1px)) {
  .nav-btn-liquid {
    /* 不支持 backdrop-filter 时的优雅降级 */
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: none;
  }
}

💡 Key Takeaways / 核心要点

  • Hover 增加玻璃厚度感 --- 提高 opacity 和 box-shadow 强度,让玻璃看起来更厚实质感
  • Active 模拟按压反馈 --- 缩放 0.97 + 减小投影,模拟真实玻璃的物理按压
  • GPU 加速保障帧率 --- will-change / translateZ(0) 让玻璃动画保持在 60fps
  • @supports 优雅降级 --- 不支持的浏览器使用半透明实色背景

参考资料:


6. 完整可运行示例 🎯 / Complete Runnable Example

🎯 Note: 本章提供一个完整的导航页面代码,包含 Apple 和 Honor 两种风格的实现 / This chapter provides a complete end-to-end runnable code example with both Apple and Honor style implementations.

📁 完整可运行的 Demo HTML 文件已放置在: resources/Q06/code/glassmorphism-nav-demo.html

该文件在一个页面中并排展示了全部 3 种方法的效果,支持点击切换选中态,方便对比视觉差异。

6.1 HTML 结构 / HTML Structure

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>玻璃效果导航页面 🪟</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>

  <!-- 🍎 Apple Liquid Glass 风格导航 -->
  <nav class="nav-bar-liquid" aria-label="Apple 风格导航">
    <button class="nav-btn-liquid active" aria-current="page">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
      </svg>
      <span>首页</span>
    </button>
    <button class="nav-btn-liquid">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <circle cx="12" cy="12" r="3"></circle>
        <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path>
      </svg>
      <span>探索</span>
    </button>
    <button class="nav-btn-liquid">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path>
        <polyline points="22,6 12,13 2,6"></polyline>
      </svg>
      <span>消息</span>
    </button>
    <button class="nav-btn-liquid">
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
        <circle cx="12" cy="7" r="4"></circle>
      </svg>
      <span>我的</span>
    </button>
  </nav>

  <!-- 📱 Honor MagicOS 风格导航 -->
  <nav class="nav-bar-honor" aria-label="Honor 风格导航">
    <button class="nav-btn-honor active" aria-current="page">
      <span>首页</span>
    </button>
    <button class="nav-btn-honor">
      <span>探索</span>
    </button>
    <button class="nav-btn-honor">
      <span>消息</span>
    </button>
    <button class="nav-btn-honor">
      <span>我的</span>
    </button>
  </nav>

</body>
</html>

6.2 完整 CSS / Complete CSS

css 复制代码
/* ========== 全局重置与基础样式 ========== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 40px;
  padding: 40px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #0d0d1a;                                   /* 深色基础背景 */
}

/* ========== 🍎 Apple Liquid Glass 导航 ========== */

/* 导航容器:自建多层渐变背景 */
.nav-bar-liquid {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-radius: 20px;
  position: relative;
  /* 多层渐变模拟玻璃后的彩色环境 🎨 */
  background:
    radial-gradient(ellipse at 15% 40%, rgba(100, 180, 255, 0.2) 0%, transparent 50%),
    radial-gradient(ellipse at 85% 60%, rgba(200, 100, 255, 0.15) 0%, transparent 50%),
    radial-gradient(ellipse at 50% 100%, rgba(255, 160, 100, 0.08) 0%, transparent 40%),
    linear-gradient(145deg, #1c1c2e 0%, #2a2a3e 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 8px 32px rgba(0, 0, 0, 0.3);
}

/* Apple 风格导航按钮 */
.nav-btn-liquid {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 14px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.25),
    0 4px 20px rgba(0, 0, 0, 0.2);
  color: rgba(255, 255, 255, 0.85);
  padding: 10px 20px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  will-change: transform, opacity;
}

/* 表面高光伪元素 ✨ */
.nav-btn-liquid::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.35) 0%,
    rgba(255, 255, 255, 0.08) 30%,
    transparent 60%,
    rgba(255, 255, 255, 0.03) 85%,
    rgba(255, 255, 255, 0.08) 100%
  );
  pointer-events: none;
  z-index: 1;
}

/* 按钮悬浮态 🖱️ */
.nav-btn-liquid:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 8px 32px rgba(0, 0, 0, 0.2),
    0 12px 40px rgba(100, 140, 255, 0.1);
}

.nav-btn-liquid:hover::before {
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.5) 0%,
    rgba(255, 255, 255, 0.1) 30%,
    transparent 60%,
    rgba(255, 255, 255, 0.05) 85%,
    rgba(255, 255, 255, 0.12) 100%
  );
}

/* 按钮点击态 👆 */
.nav-btn-liquid:active {
  transform: translateY(0) scale(0.97);
  background: rgba(255, 255, 255, 0.15);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 2px 8px rgba(0, 0, 0, 0.15);
  transition: all 0.1s ease;
}

.nav-btn-liquid:active::before {
  opacity: 0.5;
}

/* 选中态 🎯 */
.nav-btn-liquid.active,
.nav-btn-liquid[aria-current="page"] {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.35);
  color: rgba(255, 255, 255, 1);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    0 4px 24px rgba(100, 140, 255, 0.2),
    0 0 0 1px rgba(100, 140, 255, 0.1);
}

/* 选中态指示器 */
.nav-btn-liquid.active::after {
  content: '';
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 3px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.3);
  opacity: 1;
}

/* ========== 📱 Honor MagicOS 风格导航 ========== */

/* Honor 导航容器 */
.nav-bar-honor {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-radius: 20px;
  position: relative;
  background:
    radial-gradient(ellipse at 30% 50%, rgba(160, 80, 255, 0.12) 0%, transparent 50%),
    radial-gradient(ellipse at 70% 30%, rgba(80, 160, 255, 0.1) 0%, transparent 40%),
    radial-gradient(ellipse at 50% 100%, rgba(255, 100, 180, 0.08) 0%, transparent 50%),
    linear-gradient(160deg, #1a1a2e 0%, #16213e 50%, #1a1a3e 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 8px 40px rgba(0, 0, 0, 0.3);
}

/* Honor 风格导航按钮 */
.nav-btn-honor {
  position: relative;
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(16px) saturate(200%);
  -webkit-backdrop-filter: blur(16px) saturate(200%);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 14px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),
    0 8px 32px rgba(120, 80, 255, 0.15),
    0 4px 12px rgba(0, 0, 0, 0.2);
  color: rgba(255, 255, 255, 0.9);
  padding: 10px 24px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

/* 彩色光晕伪元素 🌟 */
.nav-btn-honor::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border-radius: inherit;
  background: linear-gradient(
    135deg,
    rgba(160, 80, 255, 0.2) 0%,
    rgba(80, 160, 255, 0.1) 40%,
    rgba(255, 100, 180, 0.15) 70%,
    rgba(160, 80, 255, 0.05) 100%
  );
  filter: blur(12px);
  z-index: -1;
  pointer-events: none;
  opacity: 0.8;
  transition: opacity 0.3s ease, filter 0.3s ease;
}

/* Honor 按钮悬浮 */
.nav-btn-honor:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-1px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 12px 40px rgba(120, 80, 255, 0.25),
    0 4px 12px rgba(0, 0, 0, 0.2);
}

.nav-btn-honor:hover::before {
  opacity: 1;
  filter: blur(16px);
}

/* Honor 按钮点击 */
.nav-btn-honor:active {
  transform: translateY(0) scale(0.97);
  transition: all 0.1s ease;
}

/* Honor 选中态 */
.nav-btn-honor.active,
.nav-btn-honor[aria-current="page"] {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 1);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 8px 32px rgba(120, 80, 255, 0.3);
}

.nav-btn-honor.active::before {
  background: linear-gradient(
    135deg,
    rgba(160, 80, 255, 0.35) 0%,
    rgba(80, 160, 255, 0.2) 40%,
    rgba(255, 100, 180, 0.25) 70%,
    rgba(160, 80, 255, 0.1) 100%
  );
  filter: blur(20px);
  opacity: 1;
}

/* ========== 🎬 入场动画 ========== */
@keyframes glassFadeIn {
  0% {
    opacity: 0;
    backdrop-filter: blur(0px);
    transform: translateY(8px);
  }
  100% {
    opacity: 1;
    backdrop-filter: blur(20px);
    transform: translateY(0);
  }
}

.nav-btn-liquid,
.nav-btn-honor {
  animation: glassFadeIn 0.6s cubic-bezier(0.4, 0, 0.2, 1) both;
}

.nav-btn-liquid:nth-child(1),
.nav-btn-honor:nth-child(1) { animation-delay: 0ms; }

.nav-btn-liquid:nth-child(2),
.nav-btn-honor:nth-child(2) { animation-delay: 80ms; }

.nav-btn-liquid:nth-child(3),
.nav-btn-honor:nth-child(3) { animation-delay: 160ms; }

.nav-btn-liquid:nth-child(4),
.nav-btn-honor:nth-child(4) { animation-delay: 240ms; }

/* ========== 🌐 浏览器降级 ========== */
@supports not (backdrop-filter: blur(1px)) {
  .nav-btn-liquid,
  .nav-btn-honor {
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }
}

6.3 运行结果预览 / Preview

运行以上 HTML 和 CSS 后,你将看到两个导航栏:

  1. 🍎 Apple Liquid Glass 风格 --- 左上:四个带 SVG 图标的导航按钮,表面有 135 度方向高光反射,背景由蓝紫粉三层径向渐变构成,选中态带底部指示条
  2. 📱 Honor MagicOS 风格 --- 下方:四个纯文字导航按钮,外围有紫色光晕晕染效果,选中时光晕增强并扩散

两种风格都支持:

  • ✅ Hover 微上浮 + 高光增强
  • ✅ Active 微缩按压反馈
  • ✅ 选中态高亮指示
  • ✅ 入场逐次淡入动画
  • ✅ 不支持 backdrop-filter 的浏览器自动降级

参考资料:


7. 总结 📝 / Summary

本文档详细讲解了在没有背景图片的导航页面上,如何通过纯 CSS 构建高级拟态玻璃效果,核心要点回顾:🎯

核心公式 / Core Formula

scss 复制代码
玻璃效果 = 背景色彩层(radial-gradient) + 玻璃主体层(backdrop-filter + rgba)
           + 表面高光层(::before 线性渐变) + 投影悬浮层(box-shadow)

关键技巧 / Key Techniques

技巧 说明
自建背景层 🏗️ 用多个 radial-gradient 模拟玻璃后方的彩色环境,替代背景图片
多层玻璃叠加 📚 伪元素 + 主元素 + 背景层构成三层玻璃结构,创造深度
高光方向统一 所有高光从同一方向(如 135deg 左上)射入,保持真实感
过渡平滑 使用 cubic-bezier(0.4, 0, 0.2, 1) 让所有状态变化自然

风格决策 / Style Decision

场景 推荐风格
追求真实质感、克制优雅 🍎 Apple Liquid Glass
追求视觉冲击、年轻活力 📱 Honor MagicOS
性能优先、移动端 🪟 方法一:box-shadow + RGBA
需要最逼真效果 🪟 方法三:完整玻璃层叠方案

对比矩阵 / Comparison Matrix

维度 Apple 🍎 Honor 📱
透明度 rgba(255,255,255,0.08) rgba(255,255,255,0.06)
模糊 blur(20px) + saturate(180%) blur(16px) + saturate(200%)
投影 无色柔和投影 紫色彩色投影
光晕 白色高光为主 紫蓝粉色光晕
整体感 真实物理玻璃 梦幻发光琉璃

💡 最终建议 :在生产环境中,建议从方法一 开始,逐步叠加效果。先在浏览器中预览效果,确认可读性和性能后再添加高级层。记住------可读性 > 视觉效果,如果玻璃效果影响了用户的阅读体验,就需要降低透明度或增加对比度。


最后更新时间:2026-06-30

相关推荐
Apifox2 小时前
Apifox 6 月更新|Apifox CLI 全面升级、导入导出优化、OAuth 2.0 支持自动刷新令牌
前端·后端·测试
CodingSpace2 小时前
TypeScript 装饰器
前端
宸翰2 小时前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
systemPro2 小时前
光储充系统数据流全解析:PV / ESS / GRID 数据怎么流转,线损怎么算
前端
古茗前端团队4 小时前
急招!前端|测试|后端|产品(名额多,速来)
前端·后端·架构
Lsx_4 小时前
不只是 Prompt:用 Superpowers Skill 给 AI 编程装上工程化工作流
前端·ai编程·claude
小碗细面5 小时前
前端 Prompt 工程实战:如何搭建场景化 Prompt 库
前端·ai编程
阿瑞IT5 小时前
2026年 AI Agent 生产化落地全景:四大高频故障根因分析与工程解法
前端
木木剑光5 小时前
我开源了一个 React 组件库,沉淀了多个高频组件和实用 Hooks
前端·javascript·react.js