2020-12-27 09:24:43 +03:00
|
|
|
/*
|
|
|
|
Safari doesn't support CSS `scroll-behavior: smooth`,
|
2021-01-23 10:07:18 +03:00
|
|
|
so here is a compatible solution for all browser to smooth scrolling
|
2020-12-27 09:24:43 +03:00
|
|
|
|
|
|
|
See: <https://css-tricks.com/snippets/jquery/smooth-scrolling/>
|
|
|
|
|
|
|
|
Warning: It must be called after all `<a>` tags (e.g., the dynamic TOC) are ready.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
$("a[href*='#']")
|
|
|
|
.not("[href='#']")
|
|
|
|
.not("[href='#0']")
|
|
|
|
.click(function(event) {
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
if (this.pathname.replace(/^\//, "") === location.pathname.replace(/^\//, "")) {
|
|
|
|
if (location.hostname === this.hostname) {
|
2020-12-27 09:24:43 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const REM = 16; /* 16px */
|
2020-12-27 09:24:43 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const hash = decodeURI(this.hash);
|
|
|
|
let isFnRef = RegExp(/^#fnref:/).test(hash);
|
2021-10-31 17:56:22 +03:00
|
|
|
let isFn = isFnRef? false : RegExp(/^#fn:/).test(hash);
|
|
|
|
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
|
2021-01-23 10:07:18 +03:00
|
|
|
let target = $(selector);
|
2020-12-27 09:24:43 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
if (target.length) {
|
|
|
|
event.preventDefault();
|
2020-12-27 09:24:43 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
if (history.pushState) { /* add hash to URL */
|
|
|
|
history.pushState(null, null, hash);
|
2020-12-31 16:37:17 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
let curOffset = $(this).offset().top;
|
|
|
|
let destOffset = target.offset().top;
|
|
|
|
const scrollUp = (destOffset < curOffset);
|
|
|
|
const topbarHeight = $("#topbar-wrapper").outerHeight();
|
2020-12-31 16:37:17 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
if (scrollUp && isFnRef) {
|
|
|
|
/* Avoid the top-bar covering `fnref` when scrolling up
|
|
|
|
because `fnref` has no `%anchor`(see: module.scss) style. */
|
|
|
|
destOffset -= (topbarHeight + REM / 2);
|
2020-12-31 16:37:17 +03:00
|
|
|
}
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
$("html,body").animate({
|
|
|
|
scrollTop: destOffset
|
|
|
|
}, 800, () => {
|
|
|
|
|
|
|
|
const $target = $(target);
|
|
|
|
$target.focus();
|
|
|
|
|
|
|
|
const SCROLL_MARK = "scroll-focus";
|
|
|
|
|
|
|
|
/* clean up old scroll mark */
|
|
|
|
if ($(`[${SCROLL_MARK}=true]`).length) {
|
|
|
|
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean :target links */
|
|
|
|
if ($(":target").length) { /* element that visited by the URL with hash */
|
|
|
|
$(":target").attr(SCROLL_MARK, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set scroll mark to footnotes */
|
|
|
|
if (isFn || isFnRef) {
|
|
|
|
$target.attr(SCROLL_MARK, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($target.is(":focus")) { /* Checking if the target was focused */
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
|
|
|
|
$target.focus(); /* Set focus again */
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-31 16:37:17 +03:00
|
|
|
}
|
2020-12-27 09:24:43 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 16:37:17 +03:00
|
|
|
}); /* click() */
|
2020-12-27 09:24:43 +03:00
|
|
|
});
|