Skip to content

Commit

Permalink
fix: use 2 indents and remove unnecessary named variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kavania2002 committed Jul 23, 2024
1 parent 7527e01 commit 0921344
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions sphinx/themes/basic/static/navigate_top.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,45 @@ var lastScrollTop = document.documentElement.scrollTop;
const GO_TO_TOP_OFFSET = 64;

const _scrollToTop = (positionY) => {
const navigateToTopButtom = document.getElementById("navigate-to-top");
// position not yet crossed of offset
if (positionY < GO_TO_TOP_OFFSET) {
navigateToTopButtom.classList.add("hide-navigate-to-top");
navigateToTopButtom.classList.remove("show-navigate-to-top");
} else {
if (positionY > lastScrollTop) {
// scrolling down
navigateToTopButtom.classList.add("hide-navigate-to-top");
navigateToTopButtom.classList.remove("show-navigate-to-top");
} else if (positionY < lastScrollTop) {
// scrolling up
navigateToTopButtom.classList.add("show-navigate-to-top");
navigateToTopButtom.classList.remove("hide-navigate-to-top");
}
const navigateToTopButtom = document.getElementById("navigate-to-top");
// position not yet crossed of offset
if (positionY < GO_TO_TOP_OFFSET) {
navigateToTopButtom.classList.add("hide-navigate-to-top");
navigateToTopButtom.classList.remove("show-navigate-to-top");
} else {
if (positionY > lastScrollTop) {
// scrolling down
navigateToTopButtom.classList.add("hide-navigate-to-top");
navigateToTopButtom.classList.remove("show-navigate-to-top");
} else if (positionY < lastScrollTop) {
// scrolling up
navigateToTopButtom.classList.add("show-navigate-to-top");
navigateToTopButtom.classList.remove("hide-navigate-to-top");
}
// update the position for next scroll event
lastScrollTop = positionY;
}
// update the position for next scroll event
lastScrollTop = positionY;
};

const _setupScrollHandler = () => {
let lastKnownScrollPosition = 0;
// help to keep track if requestAnimationFrame is scheduled
let ticking = false;

document.addEventListener("scroll", (event) => {
lastKnownScrollPosition = window.scrollY;

if (!ticking) {
window.requestAnimationFrame(() => {
_scrollToTop(lastKnownScrollPosition)
// animation is complete (so now be called again)
ticking = false;
});

// it's scheduled so don't call back the requestAnimationFrame
ticking = true;
}
});
};

const _main = () => {
_setupScrollHandler();
let lastKnownScrollPosition = 0;
// help to keep track if requestAnimationFrame is scheduled
let ticking = false;

document.addEventListener("scroll", (event) => {
lastKnownScrollPosition = window.scrollY;

if (!ticking) {
window.requestAnimationFrame(() => {
_scrollToTop(lastKnownScrollPosition)
// animation is complete (so now be called again)
ticking = false;
});

// it's scheduled so don't call back the requestAnimationFrame
ticking = true;
}
});
};

document.addEventListener("DOMContentLoaded", _main);
document.addEventListener("DOMContentLoaded", _setupScrollHandler);

0 comments on commit 0921344

Please sign in to comment.