web/_javascript/commons/topbar-switch.js

70 lines
1.5 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() {
2021-01-23 10:07:18 +03:00
const topbarWrapper = $("#topbar-wrapper");
const toc = $("#toc-wrapper");
const access = $(".access");
const searchInput = $("#search-input");
let didScroll;
let lastScrollTop = 0;
const delta = 5;
const topbarHeight = topbarWrapper.outerHeight();
2019-09-30 15:38:41 +03:00
function hasScrolled() {
var st = $(this).scrollTop();
/* 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 && st > topbarHeight) {
/* Scroll Down */
2021-01-23 10:07:18 +03:00
topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
2019-09-30 15:38:41 +03:00
2021-01-23 10:07:18 +03:00
if (toc.length > 0) {
toc.removeClass("topbar-down");
2019-09-30 15:38:41 +03:00
}
2021-01-23 10:07:18 +03:00
if (access.length > 0) {
access.removeClass("topbar-down");
2019-09-30 15:38:41 +03:00
}
2021-01-23 10:07:18 +03:00
if (searchInput.is(":focus")) {
searchInput.blur(); /* remove focus */
2019-09-30 15:38:41 +03:00
}
2021-01-23 10:07:18 +03:00
} else if (st + $(window).height() < $(document).height()) {
/* Scroll Up */
2021-01-23 10:07:18 +03:00
topbarWrapper.removeClass("topbar-up").addClass("topbar-down");
if (toc.length > 0) {
toc.addClass("topbar-down");
}
if (access.length > 0) {
access.addClass("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")) { /* Not in small screens */
didScroll = true;
}
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
2021-01-23 10:07:18 +03:00
});