惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
V
V2EX
The GitHub Blog
The GitHub Blog
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
A
Arctic Wolf
Google DeepMind News
Google DeepMind News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
W
WeLiveSecurity
Cloudbric
Cloudbric
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
U
Unit 42
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Spread Privacy
Spread Privacy
Hugging Face - Blog
Hugging Face - Blog
O
OpenAI News
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
博客园 - 聂微东
V
Visual Studio Blog
美团技术团队
Security Archives - TechRepublic
Security Archives - TechRepublic
G
GRAHAM CLULEY
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
Know Your Adversary
Know Your Adversary
小众软件
小众软件
Engineering at Meta
Engineering at Meta
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
T
Troy Hunt's Blog

mephisto.cc

修复rime-ice无法弹出候选框的问题 开机滚动字体显示优化 个人网站安全防护 树莓派上部署Vaultwarden 使用goaccess实时分析Caddy日志 个人网站监控 Hugo全站AVIF记 Linux下尝试使用Godot开发小游戏 Arch linux dae 透明代理 Airflow接管galler-dl下载任务 如何使用gallery-dl批量下载图像 Arch核显下如何愉快玩Dota2和CS2 Arch/labwc 环境网络相关设置 Rime添加dota2词库 Arch 重装记录 Supertuxkart iOS版终于发布了 打造自己的流媒体音乐服务 剪切板管理工具clipcat推荐 MangoHud性能监控 微信小程序开发记 Rofi试用 解决Arch下VSCode无法输入中文的问题 独立窗口管理器下无法录屏问题处理 上海市二手房成交数据监控 OpenLDAP监控 Arch linux如何顺畅连接蓝牙设备 使用Git和Ansible管理配置文件 Arch Linux SSL VPN 客户端配置 简单获取celery任务实时结果 Arch linux下iNode客户端的安装和使用方法 券商费率调整记 迷你主机组装记 Labwc更换定制皮肤 树莓派跑分流代理 实用自动代理配置示例 九年老鼠标复活记 Openvpn示例 Mac下如何旋转webp图片 如何隐藏Vscode中Markdown PDF插件的头部内容 解决Linux下网络配置无法变更保存的问题 labwc环境启用wlogout Ubuntu下新官方微信尝鲜 简单检测用户是否开启广告屏蔽 Atuin ZFS下卡顿问题解决 Wine安装64位微信 Fish 和 Atuin使用记 Fastapi简单实现临时下载链接 Fastapi微信公众号开发简要 Hugo根据语种展示不同内容 如何修复Waybar微信图标错误 信封加密简要 Linux环境下维护公众号记录 快速隐藏和呼出终端 Wayland环境自动切换壁纸 SuperTuxKart 试玩 简单抓取雪球股票快讯 为什么你的kill命令不能列出信号表 Wine安装微信保姆教程 Hysteria科学上网简要 Ubuntu 23.10 钉钉无法启动解决方法 网站导航栏防止插入Adsense自动广告 Linux环境按键检测 Hugo文末添加最后修改时间 Wayland环境ksnip无法复制问题解决 我眼中的股票市场 Caddy简单图片防盗链 greetd和regreet使用教程 Wayland环境下截图加后期修改 Wayland 环境下gif录屏 Firefox的一些有趣功能 Ubuntu 切换系统语言 Firefox 标题栏高度调整 Hugo自定义字体 Labwc 便捷配置 Wofi使用教程 窗口管理器labwc使用记 文件共享软件Dufs推荐 Hugo静态站点接入Adsense广告 Fcitx5配置详解 Ubuntu安装chrome的方法总结 一种Xterm.js的全屏方案 Google网址收录api Python示例 手机听离线音乐 Snipe it资产导入 应用启动器yofi使用配置 Snipe it资产管理系统安装使用 Ubuntu开机启动加速 Vue3 vite TypeScript跨域等相关设置 Ubuntu手动升级Libreoffice Ubuntu设置alacritty为默认终端 简体文章批量转换为繁体 使用Inkscape调整svg图片大小 实用命令 使用mitmproxy给手机app抓包 Python中rstrip方法细解 Github同主机多仓库部署deploy key问题处理 Linux安装最新版本Python KeePassXC使用教程 Ubuntu 22.10连接蓝牙耳机报错br-connection-profile-unavailable解决方法 Caddy日志配置轮转和格式化
网站添加回到顶部功能
2023-06-26 · via mephisto.cc

本站用的 hugo clarity 皮肤自带一个回到顶部功能,图标不满意,我给改成了现在的样子,易于识别。‘回到首页’图标一直展示,有点碍事。所以需要优化下,使其默认不显示,只有当滚动条向下滚动一定距离后,超过这个阈值,显示“回到顶部”图标,不超过隐藏。

实现逻辑简单易懂,整个改动如下图所示:

hugo back to top

下面解释下变动

1. 给图标所在元素添加个 id

添加 id 的目的是便于后续操作,控制图标是否显示。变化如下:

 1diff --git a/layouts/partials/top.html b/layouts/partials/top.html
 2index bdaa538..33aade0 100644
 3--- a/layouts/partials/top.html
 4+++ b/layouts/partials/top.html
 5@@ -1,3 +1,3 @@
 6-<a class="to_top" id="toTop" href="#documentTop">
 7-  <img src="/icons/to-top.svg" width="44" height="44" alt="to-top" />
 8-</a>
 9+<a class="to_top" href="#documentTop">
10+  <img src="/icons/to-top.svg" width="44" height="44" alt="to-top">
11+</a>
12\ No newline at end of file

差别是 <a> 标签添加了个 id="toTop"

2. 设置默认隐藏图标

 1diff --git a/assets/sass/_components.sass b/assets/sass/_components.sass
 2index 8a2c398..301844a 100644
 3--- a/assets/sass/_components.sass
 4+++ b/assets/sass/_components.sass
 5@@ -546,7 +546,6 @@ figcaption
 6
 7 .to
 8   &_top
 9-    visibility: hidden
10     background-color: transparent
11     width: 2.75rem
12     height: 2.75rem
13(END)

to_top 属性添加了 visibility: hidden,意思就是默认隐藏,其实元素还是在的,性能好(来回滚动时,不用频繁创建/删除),仍然会影响布局。

3. 添加控制逻辑

 1diff --git a/assets/js/custom.js b/assets/js/custom.js
 2index 54f7fe3..11b1d82 100644
 3--- a/assets/js/custom.js
 4+++ b/assets/js/custom.js
 5@@ -1,11 +1 @@
 6-// add custom js in this file
 7-
 8-window.onscroll = function () {
 9-  var pageOffset =
10-    document.documentElement.scrollTop || document.body.scrollTop;
11-  if (pageOffset > 1000) {
12-    document.getElementById("toTop").style.visibility = "visible";
13-  } else {
14-    document.getElementById("toTop").style.visibility = "hidden";
15-  }
16-};
17+// add custom js in this file
18\ No newline at end of file
19(END)

这个皮肤的自定义 js 代码放在 custom.js 中,原本是空的。上面代码的意思,滚动时执行函数里面的代码,如果垂直方向偏离顶部的偏移量(上卷隐藏了多少)大于 1000 像素(pixels),设置 id 为 toTop 的元素可见,否则隐藏。效果就是往下滚动一段距离,图标显示,反之不显示。

scrollTop 的英文解释:

An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0.

还不理解的话,看这个 stackoverflow 回答

scrollTop cat

最后,由于页面锚点“documentTop” 在 body 元素上面,所以点击后就回到了顶部,读者可以滚动本站的页面试试效果。当然,每个网站代码结构不一样,理解原理后,自己可以自行添加此功能,本人非专业前端,这个实现不一定是最佳实践,但对于我来说够用了,分享的目的也是给非专业人员参考。