web/_javascript/commons/topbar-switch.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
/*
* Hide Header on scroll down
*/
2019-09-30 15:38:41 +03:00
$(function() {
const $topbarWrapper = $("#topbar-wrapper");
const $panel = $("#panel-wrapper");
const $searchInput = $("#search-input");
2019-09-30 15:38:41 +03:00
const CLASS_TOPBAR_UP = "topbar-up";
const CLASS_TOPBAR_DOWN = "topbar-down";
const ATTR_TOC_SCROLLING_UP = "toc-scrolling-up"; // topbar locked
2021-01-23 10:07:18 +03:00
let didScroll;
let lastScrollTop = 0;
const delta = $topbarWrapper.outerHeight();
const topbarHeight = $topbarWrapper.outerHeight();
2019-09-30 15:38:41 +03:00
function hasScrolled() {
let st = $(this).scrollTop();
2019-09-30 15:38:41 +03:00
/* Make sure they scroll more than delta */
2020-08-19 07:26:45 +03:00
if (Math.abs(lastScrollTop - st) <= delta) {
2019-09-30 15:38:41 +03:00
return;
2020-08-19 07:26:45 +03:00
}
2019-09-30 15:38:41 +03:00
if (st > lastScrollTop ) { // Scroll Down
if (st > topbarHeight) {
$topbarWrapper.removeClass(CLASS_TOPBAR_DOWN).addClass(CLASS_TOPBAR_UP);
$panel.removeClass(CLASS_TOPBAR_DOWN);
2019-09-30 15:38:41 +03:00
if ($searchInput.is(":focus")) {
$searchInput.blur(); /* remove focus */
}
2019-09-30 15:38:41 +03:00
}
} else {// Scroll up
// did not reach the bottom of the document, i.e., still have space to scroll up
if (st + $(window).height() < $(document).height()) {
let tocScrollingUp = $topbarWrapper.attr(ATTR_TOC_SCROLLING_UP);
if (typeof tocScrollingUp !== "undefined") {
if (tocScrollingUp === "false") {
$topbarWrapper.removeAttr(ATTR_TOC_SCROLLING_UP);
}
2019-09-30 15:38:41 +03:00
} else {
$topbarWrapper.removeClass(CLASS_TOPBAR_UP).addClass(CLASS_TOPBAR_DOWN);
$panel.addClass(CLASS_TOPBAR_DOWN);
}
2019-09-30 15:38:41 +03:00
}
}
lastScrollTop = st;
}
2020-08-19 07:26:45 +03:00
$(window).scroll(function(event) {
if ($("#topbar-title").is(":hidden")) {
2020-08-19 07:26:45 +03:00
didScroll = true;
}
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
2021-01-23 10:07:18 +03:00
});