1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-18 05:41:31 +00:00

Avoid preview-image delaying the switching of the top bar title (mobile views)

Use Intersection Observer API to optimize the performance of title switching
This commit is contained in:
Cotes Chung
2022-01-06 17:42:08 +08:00
parent e7b377cf63
commit 837d0778b6
7 changed files with 46 additions and 30 deletions

View File

@@ -4,6 +4,7 @@
$(function() {
const $topbarWrapper = $("#topbar-wrapper");
const $topbarTitle = $("#topbar-title");
const $panel = $("#panel-wrapper");
const $searchInput = $("#search-input");
@@ -54,7 +55,7 @@ $(function() {
}
$(window).scroll(function(event) {
if ($("#topbar-title").is(":hidden")) {
if ($topbarTitle.is(":hidden")) {
didScroll = true;
}
});

View File

@@ -1,46 +1,61 @@
/*
* Top bar title auto change while scrolling in mobile screens.
* Top bar title auto change while scrolling up/down in mobile screens.
*/
$(function() {
const titleSelector = "div.post>h1:first-of-type";
const $pageTitle = $(titleSelector);
const $topbarTitle = $("#topbar-title");
const topbarTitle = $("#topbar-title");
const postTitle = $("div.post>h1");
if ($pageTitle.length === 0 /* on Home page */
|| $pageTitle.hasClass("dynamic-title")
|| $topbarTitle.is(":hidden")) {/* not in mobile views */
return;
}
const DEFAULT = topbarTitle.text().trim();
let title = (postTitle.length > 0) ?
postTitle.text().trim() : $("h1").text().trim();
const defaultTitleText = $topbarTitle.text().trim();
let titleText = $pageTitle.text().trim();
let hasScrolled = false;
let lastScrollTop = 0;
if ($("#page-category").length || $("#page-tag").length) {
/* The title in Category or Tag page will be "<title> <count_of_posts>" */
if (/\s/.test(title)) {
title = title.replace(/[0-9]/g, "").trim();
if (/\s/.test(titleText)) {
titleText = titleText.replace(/[0-9]/g, "").trim();
}
}
/* Replace topbar title while scroll screens. */
$(window).scroll(function () {
if ($("#post-list").length /* in Home page */
|| postTitle.is(":hidden") /* is tab pages */
|| topbarTitle.is(":hidden") /* not mobile screens */
|| $("#sidebar.sidebar-expand").length) { /* when the sidebar trigger is clicked */
return false;
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;
}
if ($(this).scrollTop() >= 95) {
if (topbarTitle.text() !== title) {
topbarTitle.text(title);
let curScrollTop = $(window).scrollTop();
let isScrollDown = lastScrollTop < curScrollTop;
lastScrollTop = curScrollTop;
let heading = entries[0];
if (isScrollDown) {
if (heading.intersectionRatio === 0) {
$topbarTitle.text(titleText);
}
} else {
if (topbarTitle.text() !== DEFAULT) {
topbarTitle.text(DEFAULT);
if (heading.intersectionRatio === 1) {
$topbarTitle.text(defaultTitleText);
}
}
});
}, options);
/* Click title remove hover effect. */
topbarTitle.click(function() {
observer.observe(document.querySelector(titleSelector));
/* Click title will scroll to top */
$topbarTitle.click(function() {
$("body,html").animate({scrollTop: 0}, 800);
});