Skip to content

Commit 992f8ee

Browse files
committed
feat: add back to top progress bar
1 parent dfa3efd commit 992f8ee

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ $ docker cp cover.webp kungfux.github.io:/workspaces/kungfux.github.io/assets/me
5959
`assets/css/jekyll-theme-chirpy.scss`
6060
- Update site title tag from `<h1>` to `<p>`
6161
`_includes/sidebar.html`
62+
- Add progress bar to back to top
63+
`assets/js/progress.js`, `assets/css/jekyll-theme-chirpy.scss`

_includes/metadata-hook.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<meta name="author" content="Alexander Fuks">
2+
3+
<script src="/assets/js/progress.js" defer></script>

assets/css/jekyll-theme-chirpy.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ div.highlighter-rouge {
3737
display: flex;
3838
justify-content: center;
3939
}
40+
41+
// Progress bar
42+
#progress-circle {
43+
margin: -1px -1px;
44+
top: -2.75rem;
45+
position: relative;
46+
transform: rotate(-90deg);
47+
}
48+
49+
#progress-circle circle {
50+
stroke: var(--toc-highlight);
51+
stroke-dasharray: 2 * 3.14 * 20;
52+
stroke-dashoffset: 2 * 3.14 * 20;
53+
transition: stroke-dashoffset 0.2s;
54+
}

assets/js/progress.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const btn = document.getElementById('back-to-top');
3+
4+
if (!btn) {
5+
return;
6+
}
7+
8+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
9+
svg.setAttribute("id", "progress-circle");
10+
svg.setAttribute("width", "44");
11+
svg.setAttribute("height", "44");
12+
13+
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
14+
circle.setAttribute("cx", "22");
15+
circle.setAttribute("cy", "22");
16+
circle.setAttribute("r", "20");
17+
circle.setAttribute("stroke-width", "4");
18+
circle.setAttribute("fill", "none");
19+
20+
svg.appendChild(circle);
21+
btn.appendChild(svg);
22+
23+
window.addEventListener('scroll', () => {
24+
if (window.scrollY > 50) {
25+
const circumference = 2 * 3.14 * 20;
26+
const scrollTop = window.scrollY;
27+
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
28+
const scrollFraction = scrollTop / docHeight;
29+
const drawLength = circumference * scrollFraction;
30+
31+
circle.style.strokeDashoffset = circumference - drawLength;
32+
}
33+
});
34+
});

0 commit comments

Comments
 (0)