Skip to content

Commit 3c72298

Browse files
committed
Improve the smooth scrolling
- Add smooth scrolling to the footnotes - Add hash to URL
1 parent ca21947 commit 3c72298

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

assets/css/_addon/main.scss

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -671,27 +671,19 @@ kbd {
671671
margin: 0 0.3rem;
672672
}
673673

674-
sup {
675-
z-index: 1;
676-
&:target {
677-
@extend %anchor;
678-
z-index: 0;
679-
}
680-
}
681-
682674
.footnotes > ol {
683675
padding-left: 2rem;
684676
margin-top: 0.5rem;
685677
> li {
686-
+ li {
687-
margin-top: 0.3rem;
678+
&:not(:last-child) {
679+
margin-bottom: 0.3rem;
688680
}
689681
> p {
690682
margin-left: 0.25em;
691683
margin-top: 0;
692684
margin-bottom: 0;
693685
}
694-
&:target > p {
686+
&:target:not([scroll-focus]), &[scroll-focus=true] > p { // [scroll-focus] added by `smooth-scroll.js`
695687
background-color: var(--footnote-target-bg);
696688
width: fit-content;
697689
-webkit-transition: background-color 1.5s ease-in-out;
@@ -703,14 +695,14 @@ sup {
703695

704696
.footnote {
705697
@at-root a#{&} {
706-
margin: 0 0.2em;
698+
@include ml-mr(1px);
699+
@include pl-pr(2px);
707700
border-bottom-style: none !important;
708701
-webkit-transition: background-color 1.5s ease-in-out; /* Safari prior 6.1 */
709702
transition: background-color 1.5s ease-in-out;
710703
}
711-
@at-root sup:target > a#{&} {
704+
@at-root sup:target:not([scroll-focus]), sup[scroll-focus=true] > a#{&} { // [scroll-focus] added by `smooth-scroll.js`
712705
background-color: var(--footnote-target-bg);
713-
padding: 0 2px;
714706
}
715707
}
716708

@@ -1204,13 +1196,9 @@ img {
12041196
justify-content: center!important;
12051197
}
12061198

1207-
sup:target {
1208-
padding-top: 3.4rem;
1209-
}
1210-
12111199
.footnotes ol > li {
12121200
padding-top: 3.5rem;
1213-
margin-top: -3.2rem !important;
1201+
margin-top: -3.2rem;
12141202
&:first-child {
12151203
margin-top: -3.5rem;
12161204
}

assets/js/_utils/smooth-scroll.js

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,68 @@ $(function() {
1313
.not("[href='#0']")
1414
.click(function(event) {
1515

16-
if (location.pathname.replace(/^\//, "") === this.pathname.replace(/^\//, "")
17-
&& location.hostname === this.hostname) {
16+
if (location.pathname.replace(/^\//, "") === this.pathname.replace(/^\//, "")
17+
&& location.hostname === this.hostname) {
1818

19-
var target = $(decodeURI(this.hash));
19+
const REM = 16; /* 16px */
2020

21-
if (target.length) {
21+
const hash = decodeURI(this.hash);
22+
let isFnRef = RegExp(/^#fnref:/).test(hash);
23+
let isFn = RegExp(/^#fn:/).test(hash);
24+
let selector = hash.includes(":") ? hash.replace(/\:/, "\\:") : hash;
25+
const target = $(selector);
2226

23-
event.preventDefault();
27+
if (target.length) {
28+
event.preventDefault();
2429

25-
$("html, body").animate({
26-
scrollTop: target.offset().top
27-
}, 800, function() {
28-
var $target = $(target);
29-
$target.focus();
30+
if (history.pushState) { /* add hash to URL */
31+
history.pushState(null, null, hash);
32+
}
33+
34+
let curOffset = $(this).offset().top;
35+
let destOffset = target.offset().top;
36+
const scrollUp = (destOffset < curOffset);
37+
const topbarHeight = $("#topbar-wrapper").outerHeight();
3038

31-
if ($target.is(":focus")) { /* Checking if the target was focused */
32-
return false;
33-
} else {
34-
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
35-
$target.focus(); /* Set focus again */
39+
if (scrollUp && isFnRef) {
40+
/* Avoid the top-bar covering `fnref` when scrolling up
41+
because `fnref` has no `%anchor`(see: module.scss) style. */
42+
destOffset -= (topbarHeight + REM / 2);
3643
}
37-
});
44+
45+
$("html,body").animate({
46+
scrollTop: destOffset
47+
}, 800, () => {
48+
49+
var $target = $(target);
50+
$target.focus();
51+
52+
const SCROLL_MARK = "scroll-focus";
53+
54+
/* clean up old scroll mark */
55+
if ($(`[${ SCROLL_MARK }=true]`).length) {
56+
$(`[${ SCROLL_MARK }=true]`).attr(SCROLL_MARK, false);
57+
}
58+
59+
/* Clean :target links */
60+
if ($(":target").length) { /* element that visited by the URL with hash */
61+
$(":target").attr(SCROLL_MARK, false);
62+
}
63+
64+
/* set scroll mark to footnotes */
65+
if (isFn || isFnRef) {
66+
$target.attr(SCROLL_MARK, true);
67+
}
68+
69+
if ($target.is(":focus")) { /* Checking if the target was focused */
70+
return false;
71+
} else {
72+
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
73+
$target.focus(); /* Set focus again */
74+
}
75+
});
76+
}
3877
}
39-
}
4078

41-
}); /* click() */
79+
}); /* click() */
4280
});

0 commit comments

Comments
 (0)