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

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
/*
2020-01-02 16:17:49 +03:00
* Topbar title auto change while scrolling in mobile screens.
* 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
var DEFAULT = $("#topbar-title").text().trim();
var title = ($("div.post>h1").length > 0) ?
$("div.post>h1").text().trim() : $("h1").text().trim();
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 */
|| $("div.post>h1").is(":hidden") /* is tab pages */
|| $("#topbar-title").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) {
2020-08-19 07:26:45 +03:00
if ($("#topbar-title").text() !== title) {
2019-09-30 15:38:41 +03:00
$("#topbar-title").text(title);
}
} else {
2020-08-19 07:26:45 +03:00
if ($("#topbar-title").text() !== DEFAULT) {
2019-09-30 15:38:41 +03:00
$("#topbar-title").text(DEFAULT);
}
}
});
2019-09-30 15:38:41 +03:00
/* Click title remove hover effect. */
2020-08-19 07:26:45 +03:00
$("#topbar-title").click(function() {
$("body,html").animate({scrollTop: 0}, 800);
2019-09-30 15:38:41 +03:00
});
});