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() {
|
2021-12-06 01:54:52 +03:00
|
|
|
const $topbarTitle = $("#topbar-title");
|
2021-12-08 12:07:18 +03:00
|
|
|
const REM = 16; // in pixels
|
2022-01-09 14:09:29 +03:00
|
|
|
const ATTR_SCROLL_FOCUS = "scroll-focus";
|
2021-12-06 01:54:52 +03:00
|
|
|
|
2020-12-27 09:24:43 +03:00
|
|
|
$("a[href*='#']")
|
|
|
|
.not("[href='#']")
|
|
|
|
.not("[href='#0']")
|
|
|
|
.click(function(event) {
|
2022-01-09 14:09:29 +03:00
|
|
|
if (this.pathname.replace(/^\//, "") !==
|
|
|
|
location.pathname.replace(/^\//, "")) {
|
2022-01-05 17:56:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.hostname !== this.hostname) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hash = decodeURI(this.hash);
|
|
|
|
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
|
|
|
|
let toFootnote = toFootnoteRef ? false : RegExp(/^#fn:/).test(hash);
|
|
|
|
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
|
|
|
|
let $target = $(selector);
|
|
|
|
|
2022-01-09 14:09:29 +03:00
|
|
|
let isMobileViews = $topbarTitle.is(":visible");
|
|
|
|
let isPortrait = $(window).width() < $(window).height();
|
2022-01-05 17:56:33 +03:00
|
|
|
|
|
|
|
if (typeof $target === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (history.pushState) { /* add hash to URL */
|
|
|
|
history.pushState(null, null, hash);
|
|
|
|
}
|
|
|
|
|
2022-01-09 14:09:29 +03:00
|
|
|
let curOffset = $(window).scrollTop();
|
2022-01-05 17:56:33 +03:00
|
|
|
let destOffset = $target.offset().top -= REM / 2;
|
|
|
|
|
|
|
|
if (destOffset < curOffset) { // scroll up
|
2022-01-09 14:09:29 +03:00
|
|
|
ScrollHelper.hideTopbar();
|
|
|
|
ScrollHelper.addScrollUpTask();
|
2022-01-05 17:56:33 +03:00
|
|
|
|
2022-01-09 14:09:29 +03:00
|
|
|
if (isMobileViews && isPortrait) {
|
|
|
|
destOffset -= ScrollHelper.getTopbarHeight();
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
|
|
|
|
2022-01-09 14:09:29 +03:00
|
|
|
} else { // scroll down
|
|
|
|
if (isMobileViews && isPortrait) {
|
|
|
|
destOffset -= ScrollHelper.getTopbarHeight();
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
2020-12-27 09:24:43 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 17:56:33 +03:00
|
|
|
$("html").animate({
|
|
|
|
scrollTop: destOffset
|
|
|
|
}, 500, () => {
|
|
|
|
$target.focus();
|
|
|
|
|
|
|
|
/* clean up old scroll mark */
|
2022-01-09 14:09:29 +03:00
|
|
|
if ($(`[${ATTR_SCROLL_FOCUS}=true]`).length) {
|
|
|
|
$(`[${ATTR_SCROLL_FOCUS}=true]`).attr(ATTR_SCROLL_FOCUS, false);
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean :target links */
|
|
|
|
if ($(":target").length) { /* element that visited by the URL with hash */
|
2022-01-09 14:09:29 +03:00
|
|
|
$(":target").attr(ATTR_SCROLL_FOCUS, false);
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set scroll mark to footnotes */
|
|
|
|
if (toFootnote || toFootnoteRef) {
|
2022-01-09 14:09:29 +03:00
|
|
|
$target.attr(ATTR_SCROLL_FOCUS, true);
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2022-01-09 14:09:29 +03:00
|
|
|
if (ScrollHelper.hasScrollUpTask()) {
|
|
|
|
ScrollHelper.popScrollUpTask();
|
2022-01-05 17:56:33 +03:00
|
|
|
}
|
|
|
|
});
|
2020-12-31 16:37:17 +03:00
|
|
|
}); /* click() */
|
2020-12-27 09:24:43 +03:00
|
|
|
});
|