Skip to content

Commit 9bf7938

Browse files
Scroll To Top Button Fixes #7
1 parent f9e49dd commit 9bf7938

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

quartz/components/renderPage.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { GlobalConfiguration } from "../cfg"
1010
import { i18n } from "../i18n"
1111
// @ts-ignore
1212
import mermaidScript from "./scripts/mermaid.inline"
13+
// @ts-ignore
14+
import scrolltotopScript from "./scripts/scrolltotop.inline"
1315
import mermaidStyle from "./styles/mermaid.inline.scss"
1416
import { QuartzPluginData } from "../plugins/vfile"
1517

@@ -238,6 +240,31 @@ export function renderPage(
238240
</div>
239241
)
240242

243+
const ScrollToTop = (
244+
<button id="scrollToTopBtn" title="Go to top">
245+
<svg
246+
xmlns="http://www.w3.org/2000/svg"
247+
width="24"
248+
height="24"
249+
viewBox="0 0 24 24"
250+
fill="none"
251+
stroke="currentColor"
252+
stroke-width="2"
253+
stroke-linecap="round"
254+
stroke-linejoin="round"
255+
>
256+
<polyline points="18 15 12 9 6 15"></polyline>
257+
</svg>
258+
</button>
259+
)
260+
261+
pageResources.js.push({
262+
script: scrolltotopScript,
263+
loadTime: "afterDOMReady",
264+
moduleType: "module",
265+
contentType: "inline",
266+
})
267+
241268
const lang = componentData.fileData.frontmatter?.lang ?? cfg.locale?.split("-")[0] ?? "en"
242269
const doc = (
243270
<html lang={lang} saved-theme="dark">
@@ -259,6 +286,7 @@ export function renderPage(
259286
<BodyComponent {...componentData} />
260287
))}
261288
</div>
289+
{ScrollToTop}
262290
</div>
263291
<Content {...componentData} />
264292
<hr />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Get the button element
2+
const scrollToTopBtn = document.getElementById("scrollToTopBtn")!;
3+
4+
// Show or hide the button based on scroll position
5+
window.addEventListener("scroll", () => {
6+
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
7+
scrollToTopBtn.classList.add("show");
8+
} else {
9+
scrollToTopBtn.classList.remove("show");
10+
}
11+
});
12+
13+
// Scroll to the top of the page on button click
14+
scrollToTopBtn.addEventListener("click", () => {
15+
window.scrollTo({
16+
top: 0,
17+
behavior: "smooth"
18+
});
19+
});

quartz/styles/custom.scss

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,31 @@ ul.overflow > li:last-of-type {
2626
display: none;
2727
}
2828
}
29+
30+
/* Scroll To Top CSS */
31+
#scrollToTopBtn {
32+
position: fixed;
33+
bottom: 40px;
34+
right: 40px;
35+
background-color: var(--lightgray);
36+
color: white;
37+
border: 1px solid var(--darkgray);
38+
padding: 3px 5px;
39+
border-radius: 100px;
40+
cursor: pointer;
41+
font-size: 16px;
42+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
43+
z-index: 100;
44+
opacity: 0;
45+
pointer-events: none;
46+
transition: opacity 0.3s ease-in-out;
47+
}
48+
49+
#scrollToTopBtn.show {
50+
opacity: 1;
51+
pointer-events: auto;
52+
}
53+
54+
#scrollToTopBtn:hover {
55+
background-color: var(--secondary);
56+
}

0 commit comments

Comments
 (0)