From efeeba4743de5f34445255b7954fd8b852cca646 Mon Sep 17 00:00:00 2001 From: Adrian Rosin Date: Mon, 5 Feb 2024 14:25:54 +0100 Subject: [PATCH] feat: button disappear when scrolling down #16 --- static/script-bon.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/static/script-bon.js b/static/script-bon.js index 069bb06..d4da35f 100644 --- a/static/script-bon.js +++ b/static/script-bon.js @@ -116,3 +116,23 @@ function toggleAnimation() { } } +let lastScrollTop = 0; // Last position of the scroll + +window.addEventListener("scroll", function() { + let currentPosition = window.pageYOffset || document.documentElement.scrollTop; + const toggleButton = document.querySelectorAll('.toggleButton'); + + if (currentPosition > lastScrollTop) { + // Scroll down + toggleButton.forEach(button => { + button.style.display = "none"; + }); + } else { + // Scroll up + toggleButton.forEach(button => { + button.style.display = "block"; + }); + } + lastScrollTop = currentPosition <= 0 ? 0 : currentPosition; // For Mobile or negative scrolling +}, false); +