File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import { GlobalConfiguration } from "../cfg"
10
10
import { i18n } from "../i18n"
11
11
// @ts -ignore
12
12
import mermaidScript from "./scripts/mermaid.inline"
13
+ // @ts -ignore
14
+ import scrolltotopScript from "./scripts/scrolltotop.inline"
13
15
import mermaidStyle from "./styles/mermaid.inline.scss"
14
16
import { QuartzPluginData } from "../plugins/vfile"
15
17
@@ -238,6 +240,31 @@ export function renderPage(
238
240
</ div >
239
241
)
240
242
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
+
241
268
const lang = componentData . fileData . frontmatter ?. lang ?? cfg . locale ?. split ( "-" ) [ 0 ] ?? "en"
242
269
const doc = (
243
270
< html lang = { lang } saved-theme = "dark" >
@@ -259,6 +286,7 @@ export function renderPage(
259
286
< BodyComponent { ...componentData } />
260
287
) ) }
261
288
</ div >
289
+ { ScrollToTop }
262
290
</ div >
263
291
< Content { ...componentData } />
264
292
< hr />
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -26,3 +26,31 @@ ul.overflow > li:last-of-type {
26
26
display : none ;
27
27
}
28
28
}
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
+ }
You can’t perform that action at this time.
0 commit comments