I have a very simple script for a "disappearing" navbar that's supposed to disappear on scroll down and appear again on scroll up.

The script is not mine, but I don't understand how this works if I'm comparing two variables that apparently have the exact same value since the window.pageYOffset returns the number of pixels of the document that has been scrolled from the top. The only difference is that one variable is in the global scope and the second one isn't.

The variable currentScrollPos is inside the function for the 'onscroll' event, so I'm guessing the previousScrollPos stores the number of pixels on the Y position first, then the other variable because it's in the global scope and that's why inside the function I can check if previous is greater than currentScrollPos...? Is this correct?

And if I end the function saying that previousScrollPos = currentScrollPos (which makes the nav bar appear again at scroll up), how does it still make the condition be possible in the scroll down?

This is probably a dumb question but I'm just not getting it.. Help please

JS

const header = document.querySelector("header");let previousScrollPos = window.pageYOffset; //console.log({previousScrollPos})window.onscroll = function(){let currentScrollPos = window.pageYOffset;//console.log({previousScrollPos})//console.log({currentScrollPos})previousScrollPos > currentScrollPos ?header.classList.remove("scroll") : header.classList.add("scroll") previousScrollPos = currentScrollPos;}

CSS

header{position:fixed;top:0;left:0;width:100%;height:7rem;background-color:var(--main-color);z-index:1;transition:.5s;}header.scroll{position:fixed;top:-7rem;left:0;width:100%;height:7rem;background-color:var(--main-color);z-index:1;transition:.5s;}
2

Best Answer


previousScrollPos contains the value of window.pageYOffset at the last 'scroll' event.

currentScrollPos contains the value of window.pageYOffset at the current 'scroll' event.

These will always be different when the code that shows/hides the menu is evaluated, as currentScrollPos is set on every 'scroll' event.

This little test program shows the scrolling information, which should help you understand what is going on.

const result = document.getElementById('result');let previousScrollPos = window.pageYOffset;window.onscroll = function() {let currentScrollPos = window.pageYOffset;let showStr = 'previousScrollPos: ' + previousScrollPos + '\n' +'currentScrollPos: ' + currentScrollPos + '\n';showStr += previousScrollPos > currentScrollPos ?'scroll up' : 'scroll down';result.innerHTML = showStr;previousScrollPos = currentScrollPos;}
body {height: 60rem;width: 90%;}#result {position: fixed;top: 0px;right: 80px;min-height: 60px;min-width: 200px;background-color: lightgray;padding: 0.3rem;}
<body><h1>Test pageYOffset</h1><p>Scroll the page up and down.</p><pre id="result"></pre></body>

let lastScrollY = window.pageYOffset;let scroll = null;window.onscroll = function() {const scrollY = window.pageYOffset; const direction = scrollY > lastScrollY ? "down" : "up";if (direction !== scroll) {scroll==direction}lastScrollY = scrollY > 0 ? scrollY : 0;}