一个小小的液态玻璃插件

· · 休闲·娱乐

洛谷液态玻璃 UI v7 —— 让洛谷变成一块晶莹剔透的玻璃

一个基于 Stylus 的洛谷全局美化样式,采用「液态玻璃」(Liquid Glass / Glassmorphism)设计语言,适配洛谷新版 Columba 前端与旧版前端,并内置多级兼容降级机制。

一、这是什么?

这是一段可以配合 Stylus 浏览器扩展使用的用户样式表(UserStyle)。安装后,洛谷全站的界面会发生这样的变化:

  • 🪟 全站卡片玻璃化:题库、题目页、个人主页、专栏文章、犇犇流……所有卡片变成半透明毛玻璃;
  • 🌄 自定义背景图:背景使用三层图片链 + 渐变兜底,图床挂了也不会白屏;
  • 🧊 顶栏 / 侧栏悬浮玻璃:导航栏带 backdrop-filter 模糊与高光描边;
  • ✨ 细节动效:卡片悬浮上移、扫光动画、hover 阴影加深;
  • 📱 移动端自动降级:小屏幕关闭模糊效果,省电防卡顿;
  • ♿ 无障碍适配:尊重系统「减少动态效果」设置。

    二、核心技术原理

    1. 玻璃效果的本质:backdrop-filter

    真正的「毛玻璃」靠的是这个属性:

    background: rgba(255, 255, 255, 0.001);   /* 几乎全透明 */
    backdrop-filter: blur(20px) saturate(130%);
  • blur(20px):把元素背后的内容模糊化;
  • saturate(130%):提升透出色彩的饱和度,让玻璃更有「通透感」;
  • 背景色设为 0.001 透明度(而不是 transparent),是为了绕过洛谷某些内联样式的覆盖判断。 再配合两个「灵魂细节」:
    /* 玻璃顶边高光,模拟光打在玻璃边缘 */
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
    /* 白色渐变高光层,模拟玻璃折角反光 */
    background-image: linear-gradient(145deg,
    rgba(255,255,255,0.14) 0%,
    rgba(255,255,255,0.02) 45%,
    rgba(255,255,255,0.001) 100%);

    2. 核心修复算法:CSS 变量的「级联抢权」

    v7 修复的最关键 Bug:为什么题目页正常、主页却全白? CSS 自定义属性的继承规则是:谁离使用点最近,谁的声明生效。

    :root          ← 我在这里声明了 --theme-card-background
    └── body     ← ❌ 洛谷也在这里声明了同名变量!
    └── 主页内容   ← 实际继承到的是洛谷的值,我的设置失效

    解决方案:多级下沉声明。在 html、body、#app 等多个层级把变量再声明一遍,利用相同优先级下「后声明 / 更近祖先覆盖」的规则抢回控制权:

    html, body, #app, #app-old,
    .theme-fg, .theme-page, .full-container {
    --theme-card-background: rgba(255, 255, 255, 0.001) !important;
    --theme-card-backdrop-filter: blur(20px) saturate(130%) !important;
    /* ... */
    }

    这样无论洛谷在哪一层定义同名变量,都会被我们的值覆盖——这是一次典型的 CSS 级联层对抗。

    3. 容器剥白算法::not() 白名单递归

    洛谷页面里套了无数层白色背景的 <div>。逐个类名选择器太脆弱(前端一改版就失效),所以采用结构性排除法:

    main > div:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame) {
    background: transparent !important;
    }

    思路是:「除了玻璃卡片以外的所有容器,一律剥掉背景」。也就是维护一个「玻璃元素白名单」,其余全部强制透明。v7 还加了两道兜底:

    /* 按类名关键词匹配(index/home/wrapper/container) */
    div[class*="wrapper"]:not(.card):not(.l-card):not(pre) { ... }
    /* 内联样式带 background 的 div 一律打掉 */
    #app div[style*="background"]:not(.progress):not(.hljs):not(pre) {
    background-color: transparent !important;
    }

    4. 背景图三层容错链

    --page-bg: url("图床地址1"),
    url("图床地址2"),
    linear-gradient(160deg, #2c3e50, #4a6b8a, #6b8cae);

    CSS 多重背景的规则是:前面的图层遮住后面的。这里利用它的天然容错性——

  1. 图床图 1 加载成功 → 显示图 1;
  2. 图 1 挂了 → 透出图 2;
  3. 两张都挂了 → 显示最后的渐变色兜底,配合 --page-fallback 纯色,任何情况下都不会白屏。

    5. 渐进降级的三道防线

    /* 第一道:浏览器不支持 backdrop-filter → 改用高不透明度静态色 */
    @supports not ((backdrop-filter: blur(1px)) or
    (-webkit-backdrop-filter: blur(1px))) {
    .card { background: rgba(248, 250, 253, 0.88) !important; }
    }
    /* 第二道:移动端 → 关闭模糊省电防卡 */
    @media (max-width: 768px) {
    .card { backdrop-filter: none !important;
    background: rgba(248, 250, 253, 0.78) !important; }
    }
    /* 第三道:用户开启「减少动态效果」→ 禁用过渡动画 */
    @media (prefers-reduced-motion: reduce) {
    .card { transition: none !important; }
    }

    三道防线层层把关:功能不支持 → 视觉降级;设备受限 → 性能优先;用户偏好 → 完全尊重。

    6. 扫光动画:transform + ::after 伪元素

    卡片 hover 时会有一道高光扫过,实现方式很取巧:

    .card::after {
    content: "";
    position: absolute; inset: 0;
    background: linear-gradient(115deg,
    transparent 30%, rgba(255,255,255,0.18) 50%, transparent 70%);
    transform: translateX(-120%);        /* 初始藏在左侧外面 */
    transition: transform 0.6s ease;
    pointer-events: none;                /* 不拦截鼠标 */
    }
    .card:hover::after { transform: translateX(120%); }  /* 扫到右侧 */

    用 transform 而不是改 left,是因为 transform 走 GPU 合成层,不触发重排(reflow),性能好得多。

    三、各页面针对性的玻璃参数

    不同页面内容密度不同,玻璃的「厚度」也不同——这是调参的核心思路: | 区域 | 透明度 | 模糊强度 | 原因 | |---|---|---|---| | 主卡片 | 0.001(85% 透) | blur(20px) | 主视觉,最大化玻璃感 | | 犇犇流 | 0.40 | blur(20px) | 文字密集,需要保证可读性 | | 专栏文章 | 0.45 | blur(18px) | 长文阅读,适当加底色 | | 文章目录 | 0.60 | blur(14px) | 小字导航,透明度最高 | | 咕值图表卡 | 0.56 | 关闭 | 图表上方有 tooltip,模糊会导致渲染错位,所以直接禁用 | 💡 一个实战经验:图表和 backdrop-filter 是天敌。#rating-chart 的悬浮提示会被模糊层裁切,所以代码里单独对它 backdrop-filter: none,并把 tooltip 的 z-index 拉满到 2147483647。

    四、代码里的「防误伤」设计

    激进的透明化规则很容易误伤按钮、代码块、输入框,所以处处埋着排除项:

    .card > :not(canvas):not(img):not(pre):not(input)
    :not(textarea):not(select):not(button):not(.progress):not(.hljs) {
    background: transparent !important;
    }
  • 代码块 pre / .hljs 单独给 0.92 的高不透明底色——代码必须清晰可读;
  • 表格 采用「深色表头 + 半透明行 + hover 增亮」的分层方案;
  • 文末还留了一段注释掉的「备用核弹」:如果某次洛谷改版主页又白了,取消注释即可暴力透明化所有 div,属于最后的保险丝。

    五、总结

    这个样式的技术含量不在「写了多少条规则」,而在于四个工程思想:

  1. 级联对抗:变量多级下沉,夺取被页面脚本覆盖的控制权;
  2. 白名单剥白:用 :not() 结构排除代替逐类名枚举,抗改版;
  3. 多层容错:背景三层链、特性查询、媒体查询三道降级防线;
  4. 性能意识:transform 动画走 GPU、移动端关模糊、pointer-events: none 不拦截交互。 如果你也想给自己的网站做一套液态玻璃主题,记住核心公式: 半透明底色 + backdrop-filter 模糊 + 顶部高光内阴影 + 白色渐变折光 = 玻璃感

再配上一套降级方案,就是完整的工程实现了。

样式基于 Stylus 扩展使用,欢迎在评论区反馈 Bug 或提出适配需求(把 Console 里输出的类名发给我即可精准修复)。 代码奉上:

::::info[javascript code UIv7]

/* ============================================================

/ ========== 0. 玻璃 Token(:root 层) ========== / :root { / 洛谷 Columba 新前端主题变量 / --theme-body-back: rgba(20, 35, 56, 0.00) !important; --theme-body-image: none !important; --theme-navi-back: rgba(20, 35, 56, 0.28) !important; --theme-card-background: rgba(255, 255, 255, 0.001) !important; / 85% 透明 / --theme-card-backdrop-filter: blur(20px) saturate(130%) !important; --theme-card-background-hover: rgba(255, 255, 255, 0.22) !important; --theme-fg: #1a2433 !important;

/ 背景图三层链:图1 → 图2 → 深色渐变兜底(图床挂了也不白屏) / --page-bg: url("https://cdn.luogu.com.cn/upload/image_hosting/ciebmdnk.png"), url("https://cdn.luogu.com.cn/upload/image_hosting/8s2kqbdr.png"), linear-gradient(160deg, #2c3e50 0%, #4a6b8a 60%, #6b8cae 100%); --page-fallback: #2c3e50;

--nav-glass: rgba(20, 35, 56, 0.28); --panel-glass: rgba(255, 255, 255, 0.001); / 85% 透明 / --section-glass: rgba(236, 242, 248, 0.001); --card-glass: rgba(238, 243, 248, 0.16); --feed-glass: rgba(240, 245, 250, 0.40); --tab-panel-glass: rgba(236, 242, 248, 0.16); --profile-card-glass: rgba(238, 243, 248, 0.30); --profile-rating-glass: rgba(238, 243, 248, 0.56); --article-title-glass: rgba(238, 244, 250, 0.32); --article-glass: rgba(242, 247, 252, 0.45); --article-toc-glass: rgba(241, 246, 251, 0.60);

--blur-nav: blur(12px); --blur-card: blur(20px); --blur-soft: blur(14px); --blur-inner: blur(10px);

--radius-card: 10px; --radius-panel: 12px;

--glass-highlight: inset 0 1px 0 rgba(255, 255, 255, 0.45); --glass-shadow: 0 8px 26px rgba(0, 0, 0, 0.12); --glass-shadow-hover: 0 12px 32px rgba(0, 0, 0, 0.14); --glass-shadow-soft: 0 4px 14px rgba(0, 0, 0, 0.06); }

/* ========== 0b.【v7 核心修复】变量下沉到 body 层 ==========

/ ========== 1. 背景与内容层透明 ========== / html, body { background-image: var(--page-bg) !important; background-position: center center !important; background-size: cover !important; background-repeat: no-repeat !important; background-attachment: fixed !important; background-color: var(--page-fallback) !important; }

app, #app-old, #app > .lg-bg-content, .lg-bg-content, .lg-bg-cover,

.theme-fg, .theme-page, .wrapper.wrapped, .columba-content-wrap, .full-container, main.lfe-body, main.lcolor-bg-background.main { background: transparent !important; background-image: none !important; box-shadow: none !important; border: none !important; }

/ 主页/全站内嵌白底容器补刀(:not() 保护卡片) / main > div:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame), .lg-main, .main-wrapper, .index-wrapper, .lg-index-content, .lg-content-wrap, .lcolor-bg-background:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame),

app > div:not(.lside):not(.top-bar),

.theme-fg > div:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame), .theme-page > div:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame), .columba-content-wrap > div:not(.card):not(.l-card):not(.lg-article):not(.am-panel):not(.frame) { background: transparent !important; background-color: transparent !important; background-image: none !important; box-shadow: none !important; }

.lg-progress, .lg-index-stat, .lg-index-content > *:not(.card):not(.l-card) { background: transparent !important; }

/ ========== 2. 顶栏与侧边栏 ========== / .lside.bar, .top-bar, #app > nav.lfe-body, header.top-bar, .header-layout.tiny { background: var(--nav-glass) !important; -webkit-backdrop-filter: var(--blur-nav) saturate(125%) !important; backdrop-filter: var(--blur-nav) saturate(125%) !important; border: none !important; box-shadow: var(--glass-highlight), var(--glass-shadow) !important; }

.user-nav { background: transparent !important; }

/ ========== 3. 通用卡片玻璃 ========== / .card, .l-card:not(.user-header-top), .lg-article, .lg-summary, .am-panel, .frame, .ui.segment, .full-container > .card, .side > .card { background: var(--panel-glass) !important; -webkit-backdrop-filter: var(--blur-card) saturate(130%) brightness(1.01) !important; backdrop-filter: var(--blur-card) saturate(130%) brightness(1.01) !important; border: none !important; border-radius: var(--radius-panel) !important; box-shadow: var(--glass-highlight), var(--glass-shadow) !important; transition: box-shadow 0.22s ease !important; }

.card, .l-card, .lg-article, .am-panel, .frame { position: relative; }

.card > :not(canvas):not(img):not(pre):not(input):not(textarea):not(select):not(button):not(.progress):not(.hljs) { background: transparent !important; box-shadow: none !important; }

.card:hover, .l-card:not(.user-header-top):hover, .lg-article:hover, .am-panel:hover, .frame:hover { box-shadow: var(--glass-highlight), var(--glass-shadow-hover) !important; }

.am-panel-hd, .card-header { background: rgba(255, 255, 255, 0.12) !important; border-bottom: 1px solid rgba(255, 255, 255, 0.18) !important; }

/ ========== 4. 题库 / 题目详情页 ========== / .lg-article-sub, .problem-card, .limitation-box { background: rgba(255, 255, 255, 0.001) !important; -webkit-backdrop-filter: blur(18px) saturate(130%) !important; backdrop-filter: blur(18px) saturate(130%) !important; border-radius: var(--radius-card) !important; box-shadow: var(--glass-highlight) !important; }

.markdown-body pre, .lg-content pre, .hljs, pre.code-block, .code-block { background: rgba(243, 246, 250, 0.92) !important; border-radius: 10px !important; border: none !important; box-shadow: var(--glass-highlight) !important; } .markdown-body blockquote { background: rgba(243, 246, 250, 0.75) !important; border-left: 4px solid rgba(104, 153, 215, 0.78) !important; border-radius: 0 8px 8px 0 !important; } .markdown-body code { background: rgba(240, 244, 248, 0.92) !important; border-radius: 4px !important; }

/ ========== 5. 评测记录 / 记录详情 ========== / .am-table, .table { background: transparent !important; } .am-table thead, .table thead { background: rgba(20, 35, 56, 0.35) !important; color: #fff !important; } .am-table tbody tr, .table tbody tr { background: rgba(255, 255, 255, 0.18) !important; } .am-table tbody tr:hover, .table tbody tr:hover { background: rgba(255, 255, 255, 0.32) !important; } .am-table td, .table td { border-top: 1px solid rgba(255, 255, 255, 0.12) !important; }

/ ========== 6. 旧版首页(犇犇 / 比赛卡) ========== /

app-old .lg-index-contest {

background: var(--card-glass) !important; -webkit-backdrop-filter: var(--blur-soft) saturate(120%) !important; backdrop-filter: var(--blur-soft) saturate(120%) !important; box-shadow: var(--glass-highlight), 0 6px 20px rgba(0, 0, 0, 0.001) !important; }

app-old #feed-content {

background: rgba(255, 255, 255, 0.45) !important; -webkit-backdrop-filter: var(--blur-inner) saturate(120%) !important; backdrop-filter: var(--blur-inner) saturate(120%) !important; box-shadow: var(--glass-highlight), var(--glass-shadow-soft) !important; }

app-old #feed .am-comment-main {

background: var(--feed-glass) !important; -webkit-backdrop-filter: var(--blur-card) saturate(125%) !important; backdrop-filter: var(--blur-card) saturate(125%) !important; }

app-old #home-center-nav > li.am-active > a {

background: rgba(104, 153, 215, 0.78) !important; color: #fff !important; }

/ ========== 7. 个人主页 ========== / .l-card:has(.heatmap), .l-card:has(#rating-chart) { background: var(--profile-card-glass) !important; -webkit-backdrop-filter: var(--blur-card) saturate(125%) !important; backdrop-filter: var(--blur-card) saturate(125%) !important; box-shadow: var(--glass-highlight), 0 10px 32px rgba(0, 0, 0, 0.16) !important; }

.l-card:has(#rating-chart) { background: var(--profile-rating-glass) !important; -webkit-backdrop-filter: none !important; backdrop-filter: none !important; }

rating-chart + .tooltip { z-index: 2147483647 !important; }

/ ========== 8. 专栏与文章页 ========== / .article-banner.columba-content-wrap.wrapper { background: var(--article-title-glass) !important; -webkit-backdrop-filter: var(--blur-soft) saturate(120%) !important; backdrop-filter: var(--blur-soft) saturate(120%) !important; box-shadow: var(--glass-highlight), 0 8px 26px rgba(0, 0, 0, 0.001) !important; } .article-content > .lfe-marked-wrap, .article-content > .update-info { background: var(--article-glass) !important; -webkit-backdrop-filter: blur(18px) saturate(115%) !important; backdrop-filter: blur(18px) saturate(115%) !important; box-shadow: var(--glass-highlight), 0 8px 28px rgba(0, 0, 0, 0.09) !important; } .article-content > .actions.left-mode { background: rgba(248, 250, 253, 0.72) !important; -webkit-backdrop-filter: blur(12px) saturate(115%) !important; backdrop-filter: blur(12px) saturate(115%) !important; } .article-content > .toc-wrapper > .toc.with-top { background: var(--article-toc-glass) !important; -webkit-backdrop-filter: var(--blur-soft) saturate(112%) !important; backdrop-filter: var(--blur-soft) saturate(112%) !important; }

/ ========== 9. 页脚 ========== / footer, .l-footer, .l-footer-bg-background { background: transparent !important; -webkit-backdrop-filter: none !important; backdrop-filter: none !important; border: none !important; box-shadow: none !important; } footer::before, footer::after { content: none !important; }

/ ========== 10. 能力降级:不支持 backdrop-filter ========== / @supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) { .card, .l-card, .lg-article, .am-panel, .frame, .lside.bar, .top-bar, #app > nav.lfe-body { background: rgba(248, 250, 253, 0.88) !important; -webkit-backdrop-filter: none !important; backdrop-filter: none !important; } }

/ ========== 11. 移动端:关模糊省电 ========== / @media (max-width: 768px) { .card, .l-card, .lg-article, .am-panel, .frame, .lside.bar, .top-bar, #app > nav.lfe-body, .article-content > .lfe-marked-wrap, .article-content > .toc-wrapper > .toc.with-top { -webkit-backdrop-filter: none !important; backdrop-filter: none !important; background: rgba(248, 250, 253, 0.78) !important; } }

/ ========== 12. 动画偏好 ========== / @media (prefers-reduced-motion: reduce) { .card, .l-card, .lg-article, .am-panel, .frame { transition: none !important; } }

/ ========== 13. 玻璃质感细节 ========== / .card, .l-card, .lg-article { border: 1px solid rgba(255, 255, 255, 0.22) !important; background-image: linear-gradient(145deg, rgba(255,255,255,0.14) 0%, rgba(255,255,255,0.02) 45%, rgba(255,255,255,0.001) 100%) !important; }

.card::after { content: ""; position: absolute; inset: 0; background: linear-gradient(115deg, transparent 30%, rgba(255,255,255,0.18) 50%, transparent 70%); transform: translateX(-120%); transition: transform 0.6s ease; pointer-events: none; border-radius: inherit; } .card:hover::after { transform: translateX(120%); }

.top-bar::after { content: ""; position: absolute; left: 0; right: 0; bottom: -20px; height: 20px; background: linear-gradient(to bottom, rgba(104, 153, 215, 0.001), transparent); pointer-events: none; }

/ ========== 14.【v7 新增】主页强制玻璃化(激进版) ========== / / 主页所有直系内容容器 —— 无论类名叫什么,一律剥白 / body > div > div:not(.top-bar):not(.lside):not(nav):not(footer),

app > div > div:not(.top-bar):not(.lside):not(nav):not(footer),

app main, #app main > div, #app main > div > div,

.lfe-body, .container, .inner-wrapper, .content-wrapper, div[class="index"]:not(.card):not(.l-card):not(.lg-article), div[class="home"]:not(.card):not(.l-card):not(.lg-article), div[class="wrapper"]:not(.card):not(.l-card):not(.lg-article), div[class="container"]:not(.card):not(.l-card):not(.lg-article):not(.am-container) { background: transparent !important; background-color: transparent !important; background-image: none !important; box-shadow: none !important; }

/ 主页核心卡片(问题跳转 / 本站公告 / 近期比赛 / 运势) /

app .columba-card, #app .lg-punch, #app .feed-card,

app div[class="card"]:not([class="card-"]):not(.l-card) {

background: rgba(255, 255, 255, 0.001) !important; -webkit-backdrop-filter: blur(20px) saturate(130%) !important; backdrop-filter: blur(20px) saturate(130%) !important; border-radius: 12px !important; border: 1px solid rgba(255, 255, 255, 0.22) !important; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45), 0 8px 26px rgba(0, 0, 0, 0.12) !important; }

/ 兜底:任何内联样式带白底的 div 一律打掉 /

app div[style*="background"]:not(.progress):not(.hljs):not(pre) {

background-color: transparent !important; }

/ 图表容器底色压低 / .apexcharts-canvas, #app canvas { background: rgba(255, 255, 255, 0.001) !important; } / 液态玻璃 hover 悬浮动画 / .lg-article, .card, .am-comment-main, .l-card:not(:has(#rating-chart)) { transition: transform 0.22s ease, box-shadow 0.22s ease !important; } .lg-article:hover, .card:hover, .am-comment-main:hover, .l-card:not(:has(#rating-chart)):hover { transform: translateY(-3px) !important; box-shadow: var(--glass-highlight), 0 12px 32px rgba(0, 0, 0, 0.14) !important; }

/* ========== 15.【备用核弹】仅当主页仍是白底时取消注释 ==========

app div:not([class="card"]):not([class="Card"]):not(.lg-article):not(.am-panel)

:not(pre):not(.hljs):not(input):not(textarea):not(button):not(.progress)
:not(canvas):not(img):not(svg):not(code) {

background-color: transparent !important; }

============================================================ */

::::