Medium:返回顶部按钮
添加一个浮动按钮,向下滚动后可平滑返回页面顶部。
可复制的代码
// Add a back-to-top button that appears after scrolling down
(function () {
if (document.getElementById('jz-med-top')) { return; }
const btn = document.createElement('button');
btn.id = 'jz-med-top';
btn.textContent = '↑';
btn.style.cssText = 'position:fixed;right:24px;bottom:24px;width:42px;' +
'height:42px;border-radius:50%;border:none;background:#1a8917;' +
'color:#fff;font-size:20px;cursor:pointer;z-index:99999;display:none;';
btn.addEventListener('click', function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
document.body.appendChild(btn);
window.addEventListener('scroll', function () {
btn.style.display = window.scrollY > 600 ? 'block' : 'none';
}, { passive: true });
})();
如何使用此示例
- 用上方按钮复制代码。
- 安装 JustZix(2 分钟),在目标页面打开扩展。
- 添加一条匹配该页面的新规则。
- 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。
为此示例评分
暂无评分 — 成为第一个。