2019-09-30 15:38:41 +03:00
|
|
|
/*
|
2022-01-06 12:42:08 +03:00
|
|
|
* Top bar title auto change while scrolling up/down in mobile screens.
|
2019-09-30 15:38:41 +03:00
|
|
|
*/
|
2020-05-29 19:48:10 +03:00
|
|
|
|
|
|
|
$(function() {
|
2022-01-06 12:42:08 +03:00
|
|
|
const titleSelector = "div.post>h1:first-of-type";
|
|
|
|
const $pageTitle = $(titleSelector);
|
|
|
|
const $topbarTitle = $("#topbar-title");
|
|
|
|
|
|
|
|
if ($pageTitle.length === 0 /* on Home page */
|
|
|
|
|| $pageTitle.hasClass("dynamic-title")
|
|
|
|
|| $topbarTitle.is(":hidden")) {/* not in mobile views */
|
|
|
|
return;
|
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2022-01-06 12:42:08 +03:00
|
|
|
const defaultTitleText = $topbarTitle.text().trim();
|
|
|
|
let titleText = $pageTitle.text().trim();
|
|
|
|
let hasScrolled = false;
|
|
|
|
let lastScrollTop = 0;
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
if ($("#page-category").length || $("#page-tag").length) {
|
2020-08-19 07:26:45 +03:00
|
|
|
/* The title in Category or Tag page will be "<title> <count_of_posts>" */
|
2022-01-06 12:42:08 +03:00
|
|
|
if (/\s/.test(titleText)) {
|
|
|
|
titleText = titleText.replace(/[0-9]/g, "").trim();
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 12:42:08 +03:00
|
|
|
let options = {
|
|
|
|
rootMargin: '-48px 0px 0px 0px', // 48px equals to the topbar height (3rem)
|
|
|
|
threshold: [0, 1]
|
|
|
|
};
|
|
|
|
|
|
|
|
let observer = new IntersectionObserver((entries) => {
|
|
|
|
if (!hasScrolled) {
|
|
|
|
hasScrolled = true;
|
|
|
|
return;
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2022-01-06 12:42:08 +03:00
|
|
|
let curScrollTop = $(window).scrollTop();
|
|
|
|
let isScrollDown = lastScrollTop < curScrollTop;
|
|
|
|
lastScrollTop = curScrollTop;
|
|
|
|
let heading = entries[0];
|
|
|
|
|
|
|
|
if (isScrollDown) {
|
|
|
|
if (heading.intersectionRatio === 0) {
|
|
|
|
$topbarTitle.text(titleText);
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-06 12:42:08 +03:00
|
|
|
if (heading.intersectionRatio === 1) {
|
|
|
|
$topbarTitle.text(defaultTitleText);
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 12:42:08 +03:00
|
|
|
}, options);
|
|
|
|
|
|
|
|
observer.observe(document.querySelector(titleSelector));
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2022-01-06 12:42:08 +03:00
|
|
|
/* Click title will scroll to top */
|
|
|
|
$topbarTitle.click(function() {
|
2020-08-19 07:26:45 +03:00
|
|
|
$("body,html").animate({scrollTop: 0}, 800);
|
2019-09-30 15:38:41 +03:00
|
|
|
});
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
});
|