web/_javascript/commons/scroll-helper.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* A tool for smooth scrolling and topbar switcher
*/
const ScrollHelper = (function () {
2022-10-25 14:26:44 +03:00
const $body = $("body");
const ATTR_TOPBAR_VISIBLE = "data-topbar-visible";
const topbarHeight = $("#topbar-wrapper").outerHeight();
2022-10-25 14:26:44 +03:00
let scrollUpCount = 0; // the number of times the scroll up was triggered by ToC or anchor
let topbarLocked = false;
let orientationLocked = false;
2022-10-25 14:26:44 +03:00
return {
hideTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, false),
showTopbar: () => $body.attr(ATTR_TOPBAR_VISIBLE, true),
2022-10-25 14:26:44 +03:00
// scroll up
2022-10-25 14:26:44 +03:00
addScrollUpTask: () => {
scrollUpCount += 1;
if (!topbarLocked) {
topbarLocked = true;
}
},
popScrollUpTask: () => scrollUpCount -= 1,
hasScrollUpTask: () => scrollUpCount > 0,
topbarLocked: () => topbarLocked === true,
unlockTopbar: () => topbarLocked = false,
getTopbarHeight: () => topbarHeight,
2022-10-25 14:26:44 +03:00
// orientation change
2022-10-25 14:26:44 +03:00
orientationLocked: () => orientationLocked === true,
lockOrientation: () => orientationLocked = true,
unLockOrientation: () => orientationLocked = false
};
}());