页面右下角挂一个会轻轻摇摆的小挂件——小狐狸抱着写有站名的木牌,往下滚一段距离它才出现,点一下平滑回到顶部。比常见的方块箭头有意思得多,也不占版面。
整套是纯 CSS + 原生 JS + 一个内联 SVG,不依赖任何库。下面每块都能点「预览效果」直接看,最后几节讲怎么把小狐狸换成别的形象。
原版 · 实际效果
<style>
.wxz-demo{max-width:520px;margin:0 auto;padding:14px 16px;border-radius:10px;
background:#eef2ff;color:#3d4a66;font-size:13px;line-height:1.7}
.wxz-demo-fill{height:700px}
</style>
<div class="wxz-demo">往下滚一点,右下角会出现挂件</div>
<div class="wxz-demo-fill"></div>
<div class="wxz-demo">往下滚一点,右下角会出现挂件</div>
<style>
html{scroll-behavior:smooth}
.wx-backtop{
position:fixed; right:22px; bottom:36px; width:82px; z-index:999999;
display:block; text-decoration:none; cursor:pointer;
opacity:0; visibility:hidden; transform:translateY(14px);
transition:opacity .35s ease, transform .35s ease, visibility .35s ease;
-webkit-tap-highlight-color:transparent;
}
.wx-backtop.is-show{opacity:1; visibility:visible; transform:translateY(0)}
.wx-backtop svg{
width:82px; height:auto; display:block; transform-origin:50% 0;
animation:wxSway 3.2s ease-in-out infinite;
filter:drop-shadow(0 4px 8px rgba(0,0,0,.18));
}
@keyframes wxSway{
0%,100%{transform:rotate(-3.5deg)}
50% {transform:rotate(3.5deg)}
}
.wx-backtop:hover svg{animation-play-state:paused; transform:rotate(0)}
@media (prefers-reduced-motion:reduce){
.wx-backtop svg{animation:none}
html{scroll-behavior:auto}
}
@media (max-width:768px){
.wx-backtop{right:10px; bottom:18px; width:62px}
.wx-backtop svg{width:62px}
}
</style>
<noscript>
<style>
.wx-backtop{opacity:1 !important; visibility:visible !important; transform:none !important}
</style>
</noscript>
<a class="wx-backtop" href="#" title="回到顶部" aria-label="回到顶部">
<svg viewBox="0 0 100 190" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="回到顶部">
<!-- 挂绳 -->
<path d="M50 0 L50 14" stroke="#c0703c" stroke-width="3" stroke-linecap="round"/>
<circle cx="50" cy="17" r="3.2" fill="#c0703c"/>
<!-- 【小狐狸|耳朵 + 头 + 身体 + 小手】替换这一段即可换形象 -->
<path d="M34 28 L35 11 L47 21 Z" fill="#f2a25c"/>
<path d="M66 28 L65 11 L53 21 Z" fill="#f2a25c"/>
<path d="M37.5 25 L38.5 16 L45 22 Z" fill="#ffe0c2"/>
<path d="M62.5 25 L61.5 16 L55 22 Z" fill="#ffe0c2"/>
<ellipse cx="50" cy="40" rx="21" ry="18" fill="#f2a25c"/>
<ellipse cx="50" cy="46" rx="13" ry="10" fill="#fff6ec"/>
<circle cx="42" cy="38" r="2.4" fill="#3a2b25"/>
<circle cx="58" cy="38" r="2.4" fill="#3a2b25"/>
<circle cx="50" cy="45" r="2" fill="#3a2b25"/>
<circle cx="35" cy="44" r="3.2" fill="#ff8f8f" opacity=".5"/>
<circle cx="65" cy="44" r="3.2" fill="#ff8f8f" opacity=".5"/>
<rect x="37" y="55" width="26" height="30" rx="11" fill="#f2a25c"/>
<circle cx="43" cy="86" r="5.5" fill="#f2a25c"/>
<circle cx="57" cy="86" r="5.5" fill="#f2a25c"/>
<path d="M39 66 Q31 80 29 102" stroke="#f2a25c" stroke-width="6" fill="none" stroke-linecap="round"/>
<path d="M61 66 Q69 80 71 102" stroke="#f2a25c" stroke-width="6" fill="none" stroke-linecap="round"/>
<!-- 木牌 + 站名 -->
<path d="M50 96 L74 105 L74 176 Q74 180 70 180 L30 180 Q26 180 26 176 L26 105 Z"
fill="#f0d3a8" stroke="#b07d4f" stroke-width="2.5" stroke-linejoin="round"/>
<path d="M32 111 L68 111" stroke="#dcb68c" stroke-width="1.5"/>
<g font-family="PingFang SC,Microsoft YaHei,Hiragino Sans GB,Heiti SC,sans-serif"
font-size="14" font-weight="700" fill="#7a4a1e" text-anchor="middle">
<text x="50" y="126">维</text>
<text x="50" y="141">形</text>
<text x="50" y="156">筑</text>
<text x="50" y="171">网</text>
</g>
<!-- 流苏 -->
<path d="M40 180 L40 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="40" cy="187" r="2.4" fill="#c0703c"/>
<path d="M60 180 L60 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="60" cy="187" r="2.4" fill="#c0703c"/>
</svg>
</a>
<script>
(function () {
var btn = document.querySelector('.wx-backtop');
if (!btn) return;
function toggle() {
var y = window.pageYOffset || document.documentElement.scrollTop || 0;
btn.classList[y > 200 ? 'add' : 'remove']('is-show');
}
toggle();
window.addEventListener('scroll', toggle, { passive: true });
btn.addEventListener('click', function (e) {
e.preventDefault();
try { window.scrollTo({ top: 0, behavior: 'smooth' }); }
catch (err) { window.scrollTo(0, 0); }
});
})();
</script>原版代码的问题:创意和完成度都挺好——摇摆、挂绳、流苏、木牌都画了,还做了 prefers-reduced-motion。但有几个会影响实际使用的问题。
① 滚到底会压住页脚
| 检查项 | 实测(手机 390) |
|---|---|
| 挂件距底部 | 18px |
| 挂件高 | 118px |
| 与页脚重叠 | 45px |
回到顶部按钮恰好在页面最底部最常被用到,而那里正是页脚。实测挂件和页脚重叠 45px——版权信息、备案号会被盖住一半。
② z-index:999999 会浮在主题弹窗之上
| 元素 | 层级 |
|---|---|
| 主题弹窗 / 登录框 | 常见 9999 |
| 本挂件 | 999999 |
| 实测右下角点击命中 | 挂件(不是遮罩) |
登录框、搜索层、公告弹窗……只要层级比它低,挂件都会浮在它们上面。我模拟了一个 z-index:9999 的全屏遮罩,右下角点下去命中的仍是挂件。
③ 系统开了「减弱动效」,JS 还是平滑滚动
CSS 里写了 @media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}},但 JS 里的 window.scrollTo({behavior:'smooth'}) 是硬编码的——CSS 管不到 JS 参数。
实测开启减弱动效后,点击回顶仍有约 0.49 秒的滚动动画。对前庭敏感的用户来说,"页面自己飞"正是他们想关掉的东西。
④ 手机上木牌文字只有约 8.7px
SVG 的 viewBox 宽 100,木牌字号 font-size="14"。手机上挂件缩到 62px 宽,14 × 0.62 ≈ 8.7px——实测文字包围盒 10px(含字形外框),实际笔画只有 8~9px,小到要凑近看。
⑤ html{scroll-behavior:smooth} 是整站生效的
它会改变站点上所有锚点跳转的行为,不只是这个按钮。主题自带的目录跳转、评论区锚点都会被拖成平滑滚动。
⑥ 无 JS 时的降级有缺口
原版用 <noscript> 兜底:禁用 JS 时挂件常显。思路对,但 noscript 只在浏览器完全禁用 JS 时生效——脚本被 CSP 拦下、被压缩工具改坏、或者运行时报错,浏览器仍认为 JS 是开的,noscript 不生效,而 is-show 也永远加不上,挂件就彻底消失了。
修复版 · 不压页脚 + 尊重系统设置
<style>
.wxz-demo{max-width:520px;margin:0 auto;padding:14px 16px;border-radius:10px;
background:#eef2ff;color:#3d4a66;font-size:13px;line-height:1.7}
.wxz-demo-fill{height:700px}
</style>
<div class="wxz-demo">往下滚一点,右下角会出现挂件;滚到最底,它会自动上移让开页脚</div>
<div class="wxz-demo-fill"></div>
<div class="wxz-demo">往下滚一点,右下角会出现挂件;滚到最底,它会自动上移让开页脚</div>
<style>
.wxz-demo{max-width:520px;margin:0 auto;padding:14px 16px;border-radius:10px;
background:#eef2ff;color:#3d4a66;font-size:13px;line-height:1.7}
.wxz-top{
--wxz-bt-bottom:36px; /* 离底部距离 */
--wxz-bt-right:22px; /* 离右侧距离 */
--wxz-bt-w:82px; /* 挂件宽度 */
--wxz-bt-z:999; /* 层级,别超过主题弹窗 */
position:fixed; right:var(--wxz-bt-right); bottom:var(--wxz-bt-bottom);
width:var(--wxz-bt-w); z-index:var(--wxz-bt-z);
display:block; text-decoration:none; cursor:pointer;
opacity:1; visibility:visible;
transition:opacity .35s ease, transform .35s ease, bottom .25s ease;
-webkit-tap-highlight-color:transparent;
}
/* 只有 JS 接管后才隐藏:脚本挂了挂件也还在 */
.wxz-top.js{opacity:0; visibility:hidden; transform:translateY(14px)}
.wxz-top.js.is-show{opacity:1; visibility:visible; transform:translateY(0)}
.wxz-top svg{
width:100%; height:auto; display:block; transform-origin:50% 0;
animation:wxzSway 3.2s ease-in-out infinite;
filter:drop-shadow(0 4px 8px rgba(0,0,0,.18));
}
@keyframes wxzSway{0%,100%{transform:rotate(-3.5deg)}50%{transform:rotate(3.5deg)}}
.wxz-top:hover svg{animation-play-state:paused; transform:rotate(0)}
.wxz-top:focus-visible{outline:2px solid #ffd43b; outline-offset:4px; border-radius:8px}
@media (prefers-reduced-motion:reduce){
.wxz-top svg{animation:none}
.wxz-top{transition:none}
}
@media (max-width:768px){
.wxz-top{--wxz-bt-right:10px; --wxz-bt-bottom:18px; --wxz-bt-w:66px}
}
</style>
<a class="wxz-top" href="#top" title="回到顶部" aria-label="回到顶部" data-wxz-foot="footer">
<svg viewBox="0 0 100 190" xmlns="http://www.w3.org/2000/svg">
<!-- 挂绳 -->
<path d="M50 0 L50 14" stroke="#c0703c" stroke-width="3" stroke-linecap="round"/>
<circle cx="50" cy="17" r="3.2" fill="#c0703c"/>
<!-- 【小狐狸|耳朵 + 头 + 身体 + 小手】替换这一段即可换形象 -->
<path d="M34 28 L35 11 L47 21 Z" fill="#f2a25c"/>
<path d="M66 28 L65 11 L53 21 Z" fill="#f2a25c"/>
<path d="M37.5 25 L38.5 16 L45 22 Z" fill="#ffe0c2"/>
<path d="M62.5 25 L61.5 16 L55 22 Z" fill="#ffe0c2"/>
<ellipse cx="50" cy="40" rx="21" ry="18" fill="#f2a25c"/>
<ellipse cx="50" cy="46" rx="13" ry="10" fill="#fff6ec"/>
<circle cx="42" cy="38" r="2.4" fill="#3a2b25"/>
<circle cx="58" cy="38" r="2.4" fill="#3a2b25"/>
<circle cx="50" cy="45" r="2" fill="#3a2b25"/>
<circle cx="35" cy="44" r="3.2" fill="#ff8f8f" opacity=".5"/>
<circle cx="65" cy="44" r="3.2" fill="#ff8f8f" opacity=".5"/>
<rect x="37" y="55" width="26" height="30" rx="11" fill="#f2a25c"/>
<circle cx="43" cy="86" r="5.5" fill="#f2a25c"/>
<circle cx="57" cy="86" r="5.5" fill="#f2a25c"/>
<path d="M39 66 Q31 80 29 102" stroke="#f2a25c" stroke-width="6" fill="none" stroke-linecap="round"/>
<path d="M61 66 Q69 80 71 102" stroke="#f2a25c" stroke-width="6" fill="none" stroke-linecap="round"/>
<!-- 木牌 + 站名 -->
<path d="M50 96 L74 105 L74 176 Q74 180 70 180 L30 180 Q26 180 26 176 L26 105 Z"
fill="#f0d3a8" stroke="#b07d4f" stroke-width="2.5" stroke-linejoin="round"/>
<path d="M32 111 L68 111" stroke="#dcb68c" stroke-width="1.5"/>
<g font-family="PingFang SC,Microsoft YaHei,Hiragino Sans GB,Heiti SC,sans-serif"
font-size="16" font-weight="700" fill="#5c3512" text-anchor="middle">
<text x="50" y="127">维</text>
<text x="50" y="143">形</text>
<text x="50" y="159">筑</text>
<text x="50" y="175">网</text>
</g>
<!-- 流苏 -->
<path d="M40 180 L40 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="40" cy="187" r="2.4" fill="#c0703c"/>
<path d="M60 180 L60 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="60" cy="187" r="2.4" fill="#c0703c"/>
</svg>
</a>
<script>
(function () {
var btn = document.querySelector('.wxz-top');
if (!btn) return;
btn.classList.add('js');
var reduce = window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches;
var foot = document.querySelector(btn.getAttribute('data-wxz-foot') || 'footer');
var base = parseFloat(getComputedStyle(btn).getPropertyValue('--wxz-bt-bottom')) || 36;
function place() {
var lift = 0;
if (foot) {
var fr = foot.getBoundingClientRect();
lift = Math.max(0, Math.min(window.innerHeight, window.innerHeight - fr.top));
}
btn.style.bottom = (base + lift) + 'px';
}
function toggle() {
var y = window.pageYOffset || document.documentElement.scrollTop || 0;
btn.classList[y > 200 ? 'add' : 'remove']('is-show');
place();
}
toggle();
window.addEventListener('scroll', toggle, { passive: true });
window.addEventListener('resize', toggle);
btn.addEventListener('click', function (e) {
e.preventDefault();
try { window.scrollTo({ top: 0, behavior: reduce ? 'auto' : 'smooth' }); }
catch (err) { window.scrollTo(0, 0); }
});
})();
</script>改动都在细节上,外观几乎没变:
- 自动让开页脚:用
data-wxz-foot="footer"指定页脚选择器,页脚露出多少,挂件就上移多少。换成你主题的页脚类名即可。 - 默认可见,JS 接管后才隐藏:CSS 里挂件本来就是显示的,脚本第一件事是加
.js类把它藏起来。这样脚本挂了挂件也还在,不再依赖noscript。 - JS 也读
prefers-reduced-motion:matchMedia('(prefers-reduced-motion: reduce)'),命中就用behavior:'auto',瞬间回顶。 - 去掉
html{scroll-behavior:smooth}:平滑滚动只发生在点击时,不污染整站锚点。 - 木牌字号 14 → 16、颜色加深:手机上从约 8.7px 提到约 12.7px,字色
#7a4a1e → #5c3512(对比度 5.16 → 7.02)。 - 层级 999999 → 999:不再压主题的弹窗。
- 四个可调变量:位置、大小、层级都在 CSS 顶部,改一行就生效。
| 检查项 | 原版 | 修复版 |
|---|---|---|
| 与页脚重叠 | 45px | 0 |
| 弹窗能否盖住挂件 | 不能 | 能 |
| 减弱动效下回顶 | 0.49 秒动画 | 瞬间 |
| 手机木牌文字 | 约 8.7px | 约 12.7px |
| 脚本失效时 | 挂件消失 | 常显 |
整站 scroll-behavior |
被改 | 不动 |
换形象 · 只换中间那一段
整个挂件是一张内联 SVG,从上到下分成三段,彼此独立:
| 段 | 内容 | 坐标范围(viewBox 0 0 100 190) |
|---|---|---|
| 挂绳 | 一条竖线 + 一个圆点 | y 0 ~ 20 |
| 形象 | 耳朵 / 头 / 身体 / 小手 | y 8 ~ 100 |
| 木牌 + 流苏 | 站名 + 两条流苏 | y 96 ~ 190 |
换形象只要替换中间那段。代码里已经标好了位置:
<!-- 【小狐狸|耳朵 + 头 + 身体 + 小手】替换这一段即可换形象 --> … <!-- 木牌 + 站名 -->
把两个注释之间的所有行删掉,换成下面任意一段即可。挂绳和木牌都不用动,位置也是对齐的。
小猫款
<svg viewBox="0 0 100 115" xmlns="http://www.w3.org/2000/svg" width="200">
<!-- 复制下面 <g> 到 </g> 之间的所有行,替换小狐狸那一段 -->
<g>
<!-- 耳朵 -->
<path d="M33 31 L34 12 L48 22 Z" fill="#8fa3b8"/>
<path d="M67 31 L66 12 L52 22 Z" fill="#8fa3b8"/>
<path d="M37.5 28 L38.5 17 L45 23 Z" fill="#f0b6c2"/>
<path d="M62.5 28 L61.5 17 L55 23 Z" fill="#f0b6c2"/>
<!-- 头 + 脸 -->
<ellipse cx="50" cy="40" rx="20" ry="17" fill="#8fa3b8"/>
<ellipse cx="50" cy="45" rx="12" ry="9" fill="#fdfdfd"/>
<!-- 眼睛(竖瞳) -->
<ellipse cx="43" cy="38" rx="2.1" ry="2.9" fill="#2b3540"/>
<ellipse cx="57" cy="38" rx="2.1" ry="2.9" fill="#2b3540"/>
<!-- 鼻子 + 嘴 -->
<path d="M50 43.5 l2.2 2.2 -4.4 0 Z" fill="#d9737f"/>
<path d="M50 47 q-3.5 3 -5.5 1" stroke="#2b3540" stroke-width="1" fill="none" stroke-linecap="round"/>
<path d="M50 47 q3.5 3 5.5 1" stroke="#2b3540" stroke-width="1" fill="none" stroke-linecap="round"/>
<!-- 胡须 -->
<path d="M37 43 L27 41" stroke="#6d7d8c" stroke-width="1" stroke-linecap="round"/>
<path d="M37 47 L27 49" stroke="#6d7d8c" stroke-width="1" stroke-linecap="round"/>
<path d="M63 43 L73 41" stroke="#6d7d8c" stroke-width="1" stroke-linecap="round"/>
<path d="M63 47 L73 49" stroke="#6d7d8c" stroke-width="1" stroke-linecap="round"/>
<!-- 身体 + 手 + 脚 -->
<rect x="38" y="55" width="24" height="30" rx="10" fill="#8fa3b8"/>
<path d="M40 66 Q33 78 31 100" stroke="#8fa3b8" stroke-width="6" fill="none" stroke-linecap="round"/>
<path d="M60 66 Q67 78 69 100" stroke="#8fa3b8" stroke-width="6" fill="none" stroke-linecap="round"/>
<circle cx="43" cy="86" r="5.2" fill="#8fa3b8"/>
<circle cx="57" cy="86" r="5.2" fill="#8fa3b8"/>
</g>
</svg>小熊款
<svg viewBox="0 0 100 115" xmlns="http://www.w3.org/2000/svg" width="200">
<!-- 复制下面 <g> 到 </g> 之间的所有行,替换小狐狸那一段 -->
<g>
<!-- 圆耳朵 -->
<circle cx="34" cy="24" r="8.5" fill="#a9784f"/>
<circle cx="66" cy="24" r="8.5" fill="#a9784f"/>
<circle cx="34" cy="24" r="4" fill="#e3b795"/>
<circle cx="66" cy="24" r="4" fill="#e3b795"/>
<!-- 头 + 口鼻 -->
<ellipse cx="50" cy="41" rx="20" ry="17" fill="#a9784f"/>
<ellipse cx="50" cy="46" rx="12" ry="9" fill="#f2ddc6"/>
<!-- 眼睛 + 鼻子 -->
<circle cx="43" cy="37" r="2.2" fill="#3a2b25"/>
<circle cx="57" cy="37" r="2.2" fill="#3a2b25"/>
<ellipse cx="50" cy="43.5" rx="3.4" ry="2.5" fill="#3a2b25"/>
<path d="M50 47 q-3 3 -5 1" stroke="#3a2b25" stroke-width="1" fill="none" stroke-linecap="round"/>
<path d="M50 47 q3 3 5 1" stroke="#3a2b25" stroke-width="1" fill="none" stroke-linecap="round"/>
<!-- 身体 + 手 + 脚 -->
<rect x="38" y="56" width="24" height="29" rx="10" fill="#a9784f"/>
<path d="M40 67 Q32 80 30 101" stroke="#a9784f" stroke-width="6" fill="none" stroke-linecap="round"/>
<path d="M60 67 Q68 80 70 101" stroke="#a9784f" stroke-width="6" fill="none" stroke-linecap="round"/>
<circle cx="43" cy="86" r="5.4" fill="#a9784f"/>
<circle cx="57" cy="86" r="5.4" fill="#a9784f"/>
</g>
</svg>兔子款
<svg viewBox="0 0 100 115" xmlns="http://www.w3.org/2000/svg" width="200">
<!-- 复制下面 <g> 到 </g> 之间的所有行,替换小狐狸那一段 -->
<g>
<!-- 长耳朵 -->
<ellipse cx="41" cy="22" rx="5" ry="12" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
<ellipse cx="59" cy="22" rx="5" ry="12" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
<ellipse cx="41" cy="23" rx="2.4" ry="8" fill="#f6c3c8"/>
<ellipse cx="59" cy="23" rx="2.4" ry="8" fill="#f6c3c8"/>
<!-- 头 + 脸 -->
<ellipse cx="50" cy="43" rx="19" ry="16" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
<ellipse cx="50" cy="47" rx="11" ry="8" fill="#fff"/>
<!-- 眼睛 + 鼻子 + 嘴 -->
<circle cx="43.5" cy="41" r="2.2" fill="#3a2b25"/>
<circle cx="56.5" cy="41" r="2.2" fill="#3a2b25"/>
<path d="M50 45.5 l2 1.8 -4 0 Z" fill="#e08a9a"/>
<path d="M50 49 q-3 2.5 -5 .8" stroke="#3a2b25" stroke-width="1" fill="none" stroke-linecap="round"/>
<path d="M50 49 q3 2.5 5 .8" stroke="#3a2b25" stroke-width="1" fill="none" stroke-linecap="round"/>
<!-- 腮红 -->
<circle cx="36" cy="46" r="3" fill="#ffb3b3" opacity=".55"/>
<circle cx="64" cy="46" r="3" fill="#ffb3b3" opacity=".55"/>
<!-- 身体 + 手 + 脚 -->
<rect x="39" y="57" width="22" height="28" rx="9" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
<path d="M41 67 Q34 79 32 100" stroke="#ded2cb" stroke-width="6" fill="none" stroke-linecap="round"/>
<path d="M59 67 Q66 79 68 100" stroke="#ded2cb" stroke-width="6" fill="none" stroke-linecap="round"/>
<circle cx="44" cy="86" r="5" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
<circle cx="56" cy="86" r="5" fill="#fdfdfd" stroke="#ded2cb" stroke-width="1.4"/>
</g>
</svg>换成自己的图片
不想画 path,也可以直接塞一张图。用 SVG 的 <image> 标签,挂绳和木牌保留,只把形象换成图片:
<svg viewBox="0 0 100 190" xmlns="http://www.w3.org/2000/svg" width="82">
<!-- 挂绳(不动) -->
<path d="M50 0 L50 14" stroke="#c0703c" stroke-width="3" stroke-linecap="round"/>
<circle cx="50" cy="17" r="3.2" fill="#c0703c"/>
<!-- 换成自己的图片:只改 href,其余不用动 -->
<image href="https://wxzcw.com/logo.png" x="28" y="6" width="44" height="92"
preserveAspectRatio="xMidYMid meet"/>
<!-- 木牌 + 站名(不动) -->
<path d="M50 96 L74 105 L74 176 Q74 180 70 180 L30 180 Q26 180 26 176 L26 105 Z"
fill="#f0d3a8" stroke="#b07d4f" stroke-width="2.5" stroke-linejoin="round"/>
<path d="M32 111 L68 111" stroke="#dcb68c" stroke-width="1.5"/>
<g font-family="PingFang SC,Microsoft YaHei,Hiragino Sans GB,Heiti SC,sans-serif"
font-size="16" font-weight="700" fill="#5c3512" text-anchor="middle">
<text x="50" y="127">维</text>
<text x="50" y="143">形</text>
<text x="50" y="159">筑</text>
<text x="50" y="175">网</text>
</g>
<path d="M40 180 L40 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="40" cy="187" r="2.4" fill="#c0703c"/>
<path d="M60 180 L60 186" stroke="#c0703c" stroke-width="2" stroke-linecap="round"/>
<circle cx="60" cy="187" r="2.4" fill="#c0703c"/>
</svg>改 href 就行。几个注意点:
- 用透明底的 PNG 或 SVG,方形白底的图会盖出一个白方块。
preserveAspectRatio="xMidYMid meet"让图片完整放进框里且不变形,比例不同的图会留白而不是被拉扁。- 图片建议 1:2 左右的竖图(44×92 那个框),横图放进去会小得看不清。
- 外链图片要允许跨域引用,部分图床会拦。
为什么类名都带 wxz- 前缀
CSS 是全局生效的,没有作用域概念。只要把 <style> 加进页面,里面的规则就会作用于整个站点,而不只是你粘贴的那一块。
原版用的 .wx-backtop 看着不常见,但 backtop 是极高撞名风险的词——返回顶部是标配模块,很多主题自带同名类。撞名后后加载的覆盖先加载的,站上别的地方就莫名其妙变了。
加上 wxz- 前缀后基本不会撞。所以:
- 照抄时保留
wxz-前缀,别图省事改回短名字。 - 想改类名的话,CSS 和 HTML 里的类名要一起改,只改一处会整个失效。
- 动画名
wxzSway同理——@keyframes也是全局的。
改的时候注意这几点
- 别用
z-index:999999:会浮在主题弹窗之上。用 999 或更低的 900 就够。 - 别在
html上设scroll-behavior:那是整站生效的,只影响点击那一下就够。 - JS 也要读
prefers-reduced-motion:CSS 那条管不到scrollTo的参数。 - 挂件默认状态设为可见:靠 JS 隐藏。脚本挂了至少还能用。
- 页脚选择器要换成你主题的:
data-wxz-foot里填实际类名,找不到页脚就按原位置显示。 - 换形象时别动
viewBox:整张图的坐标系是 0 0 100 190,改了形象会错位。 - 木牌字号别低于 14:手机上会缩到 9px 以下。
- 换形象记得给
<g>加注释:下次想换回来,一眼能找到边界。
怎么用
整段粘进页脚 HTML 代码区即可:
- 博客园:后台 → 设置 → 页脚 HTML 代码。
- WordPress:外观 → 小工具 → 页脚区加一个「自定义 HTML」,或者放进子主题的
footer.php。 - Typecho / Hexo / Hugo:对应的页脚模板文件。
CSS、HTML、JS 三段是连在一起的一整块,粘的时候别拆开,也别重复粘贴——重复会出现两个挂件。
站名在 <text> 那四行,改成你自己的字;四个字的间距是 y=127/143/159/175,两个字的话改成 135 和 160 两行更居中。
兼容性
position:fixed、transform、@keyframes、filter:drop-shadow 全系支持。CSS 变量 var() 需要较新浏览器(IE 不支持,但 IE 已停止支持)。
scrollTo({behavior}) 在老 Safari 上不支持对象参数,代码里已用 try/catch 兜底成 scrollTo(0,0)。matchMedia 不支持时 reduce 会是 undefined,按正常平滑滚动处理,不会报错。
代码仅供学习交流,禁止商用。更多网站美化代码,前往 维形筑网 www.wxzcw.com。
作者:Flechazo
如若转载,请注明出处:维形筑网 https://wxzcw.com