web/assets/js/_commons/topbar-title.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
/*
2021-01-23 10:07:18 +03:00
* Top bar title auto change while scrolling in mobile screens.
2020-01-02 16:17:49 +03:00
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2018-2019 Cotes Chung
* MIT License
2019-09-30 15:38:41 +03:00
*/
$(function() {
2019-09-30 15:38:41 +03:00
2021-01-23 10:07:18 +03:00
const topbarTitle = $("#topbar-title");
const postTitle = $("div.post>h1");
const DEFAULT = topbarTitle.text().trim();
let title = (postTitle.length > 0) ?
postTitle.text().trim() : $("h1").text().trim();
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>" */
2019-09-30 15:38:41 +03:00
if (/\s/.test(title)) {
2020-08-19 07:26:45 +03:00
title = title.replace(/[0-9]/g, "").trim();
2019-09-30 15:38:41 +03:00
}
}
/* Replace topbar title while scroll screens. */
2019-09-30 15:38:41 +03:00
$(window).scroll(function () {
if ($("#post-list").length /* in Home page */
2021-01-23 10:07:18 +03:00
|| postTitle.is(":hidden") /* is tab pages */
|| topbarTitle.is(":hidden") /* not mobile screens */
|| $("#sidebar.sidebar-expand").length) { /* when the sidebar trigger is clicked */
2019-09-30 15:38:41 +03:00
return false;
}
if ($(this).scrollTop() >= 95) {
2021-01-23 10:07:18 +03:00
if (topbarTitle.text() !== title) {
topbarTitle.text(title);
2019-09-30 15:38:41 +03:00
}
} else {
2021-01-23 10:07:18 +03:00
if (topbarTitle.text() !== DEFAULT) {
topbarTitle.text(DEFAULT);
2019-09-30 15:38:41 +03:00
}
}
});
2019-09-30 15:38:41 +03:00
/* Click title remove hover effect. */
2021-01-23 10:07:18 +03:00
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
});