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-08 12:07:18 +03:00
|
|
|
const $topbarWrapper = $("#topbar-wrapper");
|
|
|
|
const topbarHeight = $topbarWrapper.outerHeight();
|
2021-12-06 01:54:52 +03:00
|
|
|
const $topbarTitle = $("#topbar-title");
|
2021-12-08 12:07:18 +03:00
|
|
|
|
|
|
|
const ATTR_TOC_SCROLLING = "toc-scrolling-up";
|
2021-12-06 01:54:52 +03:00
|
|
|
const SCROLL_MARK = "scroll-focus";
|
2021-12-08 12:07:18 +03:00
|
|
|
const REM = 16; // in pixels
|
|
|
|
let tocScrollUpCount = 0;
|
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) {
|
2021-01-23 10:07:18 +03:00
|
|
|
if (this.pathname.replace(/^\//, "") === location.pathname.replace(/^\//, "")) {
|
|
|
|
if (location.hostname === this.hostname) {
|
|
|
|
const hash = decodeURI(this.hash);
|
2021-12-06 01:54:52 +03:00
|
|
|
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
|
|
|
|
let toFootnote = toFootnoteRef? false : RegExp(/^#fn:/).test(hash);
|
2021-10-31 17:56:22 +03:00
|
|
|
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
|
2021-12-06 01:54:52 +03:00
|
|
|
let $target = $(selector);
|
2020-12-27 09:24:43 +03:00
|
|
|
|
2021-12-06 01:54:52 +03:00
|
|
|
let parent = $(this).parent().prop("tagName");
|
|
|
|
let isAnchor = RegExp(/^H\d/).test(parent);
|
2021-12-08 12:07:18 +03:00
|
|
|
let isMobileViews = !$topbarTitle.is(":hidden");
|
2021-12-06 01:54:52 +03:00
|
|
|
|
|
|
|
if (typeof $target !== "undefined") {
|
2021-01-23 10:07:18 +03:00
|
|
|
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-12-06 01:54:52 +03:00
|
|
|
let curOffset = isAnchor? $(this).offset().top : $(window).scrollTop();
|
2021-12-08 12:07:18 +03:00
|
|
|
let destOffset = $target.offset().top -= REM / 2;
|
2021-12-06 01:54:52 +03:00
|
|
|
|
|
|
|
if (destOffset < curOffset) { // scroll up
|
2021-12-08 12:07:18 +03:00
|
|
|
if (!isAnchor && !toFootnote) { // trigger by ToC item
|
|
|
|
if (!isMobileViews) { // on desktop/tablet screens
|
|
|
|
$topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
|
|
|
|
// Send message to `${JS_ROOT}/commons/topbar-switch.js`
|
|
|
|
$topbarWrapper.attr(ATTR_TOC_SCROLLING, true);
|
|
|
|
tocScrollUpCount += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((isAnchor || toFootnoteRef) && isMobileViews) {
|
2021-12-08 11:53:48 +03:00
|
|
|
destOffset -= topbarHeight;
|
2021-12-08 12:07:18 +03:00
|
|
|
console.log(`[smooth] mobile -= topbar height`);
|
2021-12-06 01:54:52 +03:00
|
|
|
}
|
2020-12-31 16:37:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 12:07:18 +03:00
|
|
|
$("html").animate({
|
2021-01-23 10:07:18 +03:00
|
|
|
scrollTop: destOffset
|
2021-12-08 12:07:18 +03:00
|
|
|
}, 500, () => {
|
2021-01-23 10:07:18 +03:00
|
|
|
$target.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 */
|
2021-12-06 01:54:52 +03:00
|
|
|
if (toFootnote || toFootnoteRef) {
|
2021-01-23 10:07:18 +03:00
|
|
|
$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 */
|
|
|
|
}
|
2021-12-08 12:07:18 +03:00
|
|
|
|
|
|
|
if (typeof $topbarWrapper.attr(ATTR_TOC_SCROLLING) !== "undefined") {
|
|
|
|
tocScrollUpCount -= 1;
|
|
|
|
|
|
|
|
if (tocScrollUpCount <= 0) {
|
|
|
|
$topbarWrapper.attr(ATTR_TOC_SCROLLING, "false");
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 10:07:18 +03:00
|
|
|
});
|
|
|
|
}
|
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
|
|
|
});
|