mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2025-12-18 13:44:15 +00:00
Improve the smooth scrolling
- Add smooth scrolling to the footnotes - Add hash to URL
This commit is contained in:
@@ -13,30 +13,68 @@ $(function() {
|
||||
.not("[href='#0']")
|
||||
.click(function(event) {
|
||||
|
||||
if (location.pathname.replace(/^\//, "") === this.pathname.replace(/^\//, "")
|
||||
&& location.hostname === this.hostname) {
|
||||
if (location.pathname.replace(/^\//, "") === this.pathname.replace(/^\//, "")
|
||||
&& location.hostname === this.hostname) {
|
||||
|
||||
var target = $(decodeURI(this.hash));
|
||||
const REM = 16; /* 16px */
|
||||
|
||||
if (target.length) {
|
||||
const hash = decodeURI(this.hash);
|
||||
let isFnRef = RegExp(/^#fnref:/).test(hash);
|
||||
let isFn = RegExp(/^#fn:/).test(hash);
|
||||
let selector = hash.includes(":") ? hash.replace(/\:/, "\\:") : hash;
|
||||
const target = $(selector);
|
||||
|
||||
event.preventDefault();
|
||||
if (target.length) {
|
||||
event.preventDefault();
|
||||
|
||||
$("html, body").animate({
|
||||
scrollTop: target.offset().top
|
||||
}, 800, function() {
|
||||
var $target = $(target);
|
||||
$target.focus();
|
||||
|
||||
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 */
|
||||
if (history.pushState) { /* add hash to URL */
|
||||
history.pushState(null, null, hash);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}); /* click() */
|
||||
let curOffset = $(this).offset().top;
|
||||
let destOffset = target.offset().top;
|
||||
const scrollUp = (destOffset < curOffset);
|
||||
const topbarHeight = $("#topbar-wrapper").outerHeight();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$("html,body").animate({
|
||||
scrollTop: destOffset
|
||||
}, 800, () => {
|
||||
|
||||
var $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 */
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}); /* click() */
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user