Skip to content

Commit cdc6a18

Browse files
committed
Merge branch 'hotfix/5.0.2'
1 parent a41b491 commit cdc6a18

File tree

15 files changed

+146
-118
lines changed

15 files changed

+146
-118
lines changed

_javascript/commons/topbar-switch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
$(function() {
66
const $topbarWrapper = $("#topbar-wrapper");
7+
const $topbarTitle = $("#topbar-title");
78
const $panel = $("#panel-wrapper");
89
const $searchInput = $("#search-input");
910

@@ -54,7 +55,7 @@ $(function() {
5455
}
5556

5657
$(window).scroll(function(event) {
57-
if ($("#topbar-title").is(":hidden")) {
58+
if ($topbarTitle.is(":hidden")) {
5859
didScroll = true;
5960
}
6061
});

_javascript/commons/topbar-title.js

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
11
/*
2-
* Top bar title auto change while scrolling in mobile screens.
2+
* Top bar title auto change while scrolling up/down in mobile screens.
33
*/
44

55
$(function() {
6+
const titleSelector = "div.post>h1:first-of-type";
7+
const $pageTitle = $(titleSelector);
8+
const $topbarTitle = $("#topbar-title");
9+
10+
if ($pageTitle.length === 0 /* on Home page */
11+
|| $pageTitle.hasClass("dynamic-title")
12+
|| $topbarTitle.is(":hidden")) {/* not in mobile views */
13+
return;
14+
}
615

7-
const topbarTitle = $("#topbar-title");
8-
const postTitle = $("div.post>h1");
9-
10-
const DEFAULT = topbarTitle.text().trim();
11-
12-
let title = (postTitle.length > 0) ?
13-
postTitle.text().trim() : $("h1").text().trim();
16+
const defaultTitleText = $topbarTitle.text().trim();
17+
let titleText = $pageTitle.text().trim();
18+
let hasScrolled = false;
19+
let lastScrollTop = 0;
1420

1521
if ($("#page-category").length || $("#page-tag").length) {
1622
/* The title in Category or Tag page will be "<title> <count_of_posts>" */
17-
if (/\s/.test(title)) {
18-
title = title.replace(/[0-9]/g, "").trim();
23+
if (/\s/.test(titleText)) {
24+
titleText = titleText.replace(/[0-9]/g, "").trim();
1925
}
2026
}
2127

22-
/* Replace topbar title while scroll screens. */
23-
$(window).scroll(function () {
24-
if ($("#post-list").length /* in Home page */
25-
|| postTitle.is(":hidden") /* is tab pages */
26-
|| topbarTitle.is(":hidden") /* not mobile screens */
27-
|| $("#sidebar.sidebar-expand").length) { /* when the sidebar trigger is clicked */
28-
return false;
28+
let options = {
29+
rootMargin: '-48px 0px 0px 0px', // 48px equals to the topbar height (3rem)
30+
threshold: [0, 1]
31+
};
32+
33+
let observer = new IntersectionObserver((entries) => {
34+
if (!hasScrolled) {
35+
hasScrolled = true;
36+
return;
2937
}
3038

31-
if ($(this).scrollTop() >= 95) {
32-
if (topbarTitle.text() !== title) {
33-
topbarTitle.text(title);
39+
let curScrollTop = $(window).scrollTop();
40+
let isScrollDown = lastScrollTop < curScrollTop;
41+
lastScrollTop = curScrollTop;
42+
let heading = entries[0];
43+
44+
if (isScrollDown) {
45+
if (heading.intersectionRatio === 0) {
46+
$topbarTitle.text(titleText);
3447
}
3548
} else {
36-
if (topbarTitle.text() !== DEFAULT) {
37-
topbarTitle.text(DEFAULT);
49+
if (heading.intersectionRatio === 1) {
50+
$topbarTitle.text(defaultTitleText);
3851
}
3952
}
40-
});
53+
}, options);
54+
55+
observer.observe(document.querySelector(titleSelector));
4156

42-
/* Click title remove hover effect. */
43-
topbarTitle.click(function() {
57+
/* Click title will scroll to top */
58+
$topbarTitle.click(function() {
4459
$("body,html").animate({scrollTop: 0}, 800);
4560
});
4661

_javascript/copyright

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Chirpy v5.0.1 (https://github.com/cotes2020/jekyll-theme-chirpy/)
2+
* Chirpy v5.0.2 (https://github.com/cotes2020/jekyll-theme-chirpy/)
33
* © 2019 Cotes Chung
44
* MIT Licensed
55
*/

_javascript/utils/smooth-scroll.js

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,81 +21,92 @@ $(function() {
2121
.not("[href='#']")
2222
.not("[href='#0']")
2323
.click(function(event) {
24-
if (this.pathname.replace(/^\//, "") === location.pathname.replace(/^\//, "")) {
25-
if (location.hostname === this.hostname) {
26-
const hash = decodeURI(this.hash);
27-
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
28-
let toFootnote = toFootnoteRef? false : RegExp(/^#fn:/).test(hash);
29-
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
30-
let $target = $(selector);
31-
32-
let parent = $(this).parent().prop("tagName");
33-
let isAnchor = RegExp(/^H\d/).test(parent);
34-
let isMobileViews = !$topbarTitle.is(":hidden");
35-
36-
if (typeof $target !== "undefined") {
37-
event.preventDefault();
38-
39-
if (history.pushState) { /* add hash to URL */
40-
history.pushState(null, null, hash);
41-
}
42-
43-
let curOffset = isAnchor? $(this).offset().top : $(window).scrollTop();
44-
let destOffset = $target.offset().top -= REM / 2;
45-
46-
if (destOffset < curOffset) { // scroll up
47-
if (!isAnchor && !toFootnote) { // trigger by ToC item
48-
if (!isMobileViews) { // on desktop/tablet screens
49-
$topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
50-
// Send message to `${JS_ROOT}/commons/topbar-switch.js`
51-
$topbarWrapper.attr(ATTR_TOC_SCROLLING, true);
52-
tocScrollUpCount += 1;
53-
}
54-
}
55-
56-
if ((isAnchor || toFootnoteRef) && isMobileViews) {
57-
destOffset -= topbarHeight;
58-
}
59-
}
60-
61-
$("html").animate({
62-
scrollTop: destOffset
63-
}, 500, () => {
64-
$target.focus();
65-
66-
/* clean up old scroll mark */
67-
if ($(`[${SCROLL_MARK}=true]`).length) {
68-
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
69-
}
70-
71-
/* Clean :target links */
72-
if ($(":target").length) { /* element that visited by the URL with hash */
73-
$(":target").attr(SCROLL_MARK, false);
74-
}
75-
76-
/* set scroll mark to footnotes */
77-
if (toFootnote || toFootnoteRef) {
78-
$target.attr(SCROLL_MARK, true);
79-
}
80-
81-
if ($target.is(":focus")) { /* Checking if the target was focused */
82-
return false;
83-
} else {
84-
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
85-
$target.focus(); /* Set focus again */
86-
}
87-
88-
if (typeof $topbarWrapper.attr(ATTR_TOC_SCROLLING) !== "undefined") {
89-
tocScrollUpCount -= 1;
90-
91-
if (tocScrollUpCount <= 0) {
92-
$topbarWrapper.attr(ATTR_TOC_SCROLLING, "false");
93-
}
94-
}
95-
});
24+
25+
if (this.pathname.replace(/^\//, "") !== location.pathname.replace(/^\//, "")) {
26+
return;
27+
}
28+
29+
if (location.hostname !== this.hostname) {
30+
return;
31+
}
32+
33+
const hash = decodeURI(this.hash);
34+
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
35+
let toFootnote = toFootnoteRef ? false : RegExp(/^#fn:/).test(hash);
36+
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
37+
let $target = $(selector);
38+
39+
let parent = $(this).parent().prop("tagName");
40+
let isAnchor = RegExp(/^H\d/).test(parent);
41+
let isMobileViews = !$topbarTitle.is(":hidden");
42+
43+
if (typeof $target === "undefined") {
44+
return;
45+
}
46+
47+
event.preventDefault();
48+
49+
if (history.pushState) { /* add hash to URL */
50+
history.pushState(null, null, hash);
51+
}
52+
53+
let curOffset = isAnchor ? $(this).offset().top : $(window).scrollTop();
54+
let destOffset = $target.offset().top -= REM / 2;
55+
56+
if (destOffset < curOffset) { // scroll up
57+
if (!isAnchor && !toFootnote) { // trigger by ToC item
58+
if (!isMobileViews) { // on desktop/tablet screens
59+
$topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
60+
// Send message to `${JS_ROOT}/commons/topbar-switch.js`
61+
$topbarWrapper.attr(ATTR_TOC_SCROLLING, true);
62+
tocScrollUpCount += 1;
9663
}
9764
}
65+
66+
if ((isAnchor || toFootnoteRef) && isMobileViews) {
67+
destOffset -= topbarHeight;
68+
}
69+
70+
} else {
71+
if (isMobileViews) {
72+
destOffset -= topbarHeight;
73+
}
9874
}
9975

76+
$("html").animate({
77+
scrollTop: destOffset
78+
}, 500, () => {
79+
$target.focus();
80+
81+
/* clean up old scroll mark */
82+
if ($(`[${SCROLL_MARK}=true]`).length) {
83+
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
84+
}
85+
86+
/* Clean :target links */
87+
if ($(":target").length) { /* element that visited by the URL with hash */
88+
$(":target").attr(SCROLL_MARK, false);
89+
}
90+
91+
/* set scroll mark to footnotes */
92+
if (toFootnote || toFootnoteRef) {
93+
$target.attr(SCROLL_MARK, true);
94+
}
95+
96+
if ($target.is(":focus")) { /* Checking if the target was focused */
97+
return false;
98+
} else {
99+
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
100+
$target.focus(); /* Set focus again */
101+
}
102+
103+
if (typeof $topbarWrapper.attr(ATTR_TOC_SCROLLING) !== "undefined") {
104+
tocScrollUpCount -= 1;
105+
106+
if (tocScrollUpCount <= 0) {
107+
$topbarWrapper.attr(ATTR_TOC_SCROLLING, "false");
108+
}
109+
}
110+
});
100111
}); /* click() */
101112
});

_sass/addon/commons.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,10 @@ $sidebar-display: "sidebar-display";
13351335
min-width: 150px;
13361336
}
13371337

1338+
#search-hints {
1339+
display: none;
1340+
}
1341+
13381342
#search-result-wrapper {
13391343
margin-top: 3rem;
13401344
}
@@ -1492,10 +1496,6 @@ $sidebar-display: "sidebar-display";
14921496
}
14931497
}
14941498

1495-
#search-hints {
1496-
display: none;
1497-
}
1498-
14991499
.post-content {
15001500
font-size: 1.03rem;
15011501
}

_sass/jekyll-theme-chirpy.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* The styles for Jekyll theme Chirpy
33
*
4-
* Chirpy v5.0.1 (https://github.com/cotes2020/jekyll-theme-chirpy)
4+
* Chirpy v5.0.2 (https://github.com/cotes2020/jekyll-theme-chirpy)
55
* © 2019 Cotes Chung
66
* MIT Licensed
77
*/

assets/js/dist/categories.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)