机构官网的技术栈越简单越好——客户可能三年后才想起改一次,那时框架早就变了。所以整套前端是:一个 610 行的 CSS + 一个 120 行的 JS,零依赖。
这一篇讲这 120 行 JS 里做了什么,以及几个「不用框架也能做得漂亮」的做法。
一、暗色模式:关键在防闪烁
存储用 localStorage,状态写在 <html data-theme> 上,CSS 用 :root[data-theme="dark"] 覆盖变量。
重点是这段必须放在 <head> 里同步执行:
1 2 3 4
| <script> try { t = localStorage.getItem('xongyi-theme') || 'light'; } catch (e) {} document.documentElement.setAttribute('data-theme', t); </script>
|
1 2 3 4 5
| function applyTheme(t) { root.setAttribute('data-theme', t); try { localStorage.setItem('xongyi-theme', t); } catch (e) {} }
|
为什么不能等
放在 body 末尾或等 DOM ready,浏览器会先按亮色渲染一帧再切到暗色——用户看到一次白闪。
凡是影响首屏外观的状态(主题、语言、设备档位),都要在 <head> 里同步定档。这条规则我在 xfm 主题里也用(那个是 data-tier 档位)。
try/catch 包住 localStorage 是必须的:隐私模式下访问它会抛异常,不包住整个脚本就挂了。
二、图标:48 个内联 SVG 的 helper
不用图标字体、不用 CDN,图标是一张 JS 里的字符串表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const ICONS = { 'arrow-right': '<line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/>', 'check': '<polyline points="20 6 9 17 4 12"/>', 'phone': '<path d="M22 16.92v3a2 2 0 0 1-2.18 2 ..."/>', };
hexo.extend.helper.register('icon', function (name, cls) { const body = ICONS[name] || ICONS['link']; return '<svg class="icon' + (cls ? ' ' + cls : '') + '" viewBox="0 0 24 24" fill="none" ' + 'stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" ' + 'width="1em" height="1em" aria-hidden="true">' + body + '</svg>'; });
|
模板里就这么用:
1
| <a href="/contact/"><%= __('contact_us') %><%- icon('arrow-right') %></a>
|
为什么这里用内联、xfm
两个主题选了不同方案,原因是使用量不同:
|
xongyi |
xfm |
| 图标数量 |
48 个,页面用量少 |
更多,且同一页面重复出现 |
| 方案 |
helper 内联输出 |
<use xlink:href> 引用雪碧图 |
| 优势 |
实现简单、无额外请求 |
重复图标不重复输出 SVG 节点 |
选型的依据是「同一页面里同一个图标出现几次」,不是哪个方案更「高级」。
stroke="currentColor" 让图标自动跟随文字颜色,预设切换时图标颜色不用管——这是零成本适配三套预设的关键。
三、联系表单:无后端也能收留言
静态站没有后端,表单的 action 做成配置项:
1 2 3 4 5 6 7 8 9
| <%# 留言表单(配置 contact.form_action 后生效) %> <form class="contact-form reveal" action="<%- theme.contact.form_action %>" method="POST"> <div class="form-row"> <label for="cf-name"><%= __('your_name') %></label> <input id="cf-name" type="text" name="name" required> </div> ... <button class="btn btn--primary" type="submit"><%= __('send') %><%- icon('send') %></button> </form>
|
1 2
| contact: form_action: "https://formspree.io/f/xxxxxxx"
|
三种无后端方案
- 第三方表单服务(Formspree / Getform):最省事,配一个 URL;
- 邮件链接:
mailto: 兜底,零配置但体验差;
- 企业微信/飞书机器人 webhook:需要少量 JS 配合,适合国内客户。
主题只负责渲染表单,不绑定任何服务商——把 action 做成配置,客户换服务商不用改代码。
四、动效:两个 IntersectionObserver
滚动淡入
1 2 3 4 5 6 7 8
| var io = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { entry.target.classList.add('in'); io.unobserve(entry.target); } }); }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
|
数字滚动(数据统计板块)
1 2 3 4 5 6 7 8 9 10 11 12
| function animateCounter(el) { var target = parseFloat(el.getAttribute('data-count')) || 0; var duration = 1600; function step(ts) { var p = Math.min((ts - start) / duration, 1); var eased = 1 - Math.pow(1 - p, 3); el.textContent = (target * eased).toFixed(decimals); if (p < 1) requestAnimationFrame(step); else el.textContent = target.toFixed(decimals); } requestAnimationFrame(step); }
|
两个必须做的兜底
不支持 IntersectionObserver 时直接全部显示:
1
| } else { reveals.forEach(function (el) { el.classList.add('in'); }); }
|
否则老浏览器上页面是一片空白——这是致命的,因为元素初始状态是 opacity: 0。
prefers-reduced-motion 要在 CSS 里响应:
1 2 3
| @media (prefers-reduced-motion: reduce) { .reveal { opacity: 1; transform: none; transition: none; } }
|
有前庭功能障碍的用户会被动画影响,这是无障碍的硬要求。
五、其余交互:都很朴素
1 2 3 4 5 6 7 8 9 10 11 12 13
| header.classList.toggle('is-scrolled', window.scrollY > 8);
backTop.classList.toggle('is-visible', window.scrollY > 480);
siteNav.addEventListener('click', function (e) { if (e.target.closest('a')) { siteNav.classList.remove('is-open'); navToggle.setAttribute('aria-expanded', 'false'); } });
|
滚动监听统一加 { passive: true },否则移动端滚动会被阻塞。
无障碍的三件小事,成本极低但常被忽略:
1 2 3
| <a class="skip-link" href="#main"><%= __('skip_to_content') %></a> <!-- 跳转链接 --> <button id="back-to-top" aria-label="<%= __('back_to_top') %>" title="..."> <svg aria-hidden="true"> <!-- 装饰性图标对读屏隐藏 -->
|
一个真实的踩坑组合
.reveal 的初始状态是透明的,导致全页截图时下半页全空白。截图脚本必须先给所有 .reveal 加 .in、把 .stat__number 填成终值,才能截到完整页面。
这类「初始状态不可见」的设计,会同时影响:SEO 快照、截图工具、打印、以及禁用 JS 的用户。
系列导航
评论