Sticky navigation keeps the menu within reach at all times, but it also occupies screen space and can obscure content. A "smart" navbar balances both: it sits at the top of the page, reappears whenever the user scrolls upward, and hides itself as soon as the user scrolls down. That pattern resembles the URL bar behavior in many mobile browsers, and it can be built without any library using CSS and plain JavaScript.

The markup and the sticky baseline

The example places a <nav> element directly above <main>:

<nav>
  <div class="logo">
    Logo
  </div>
  <div class="links">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
    <a href="#">Link 4</a>
  </div>
</nav>
<main>
  <!--Place the content of your page here-->
</main>

Positioning is sticky only relative to the parent container, so the <nav> must be a direct child of the body rather than nested inside another element. The starting CSS looks like this:

nav {
  position: sticky;
  top: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 1.5rem 2rem;
  background-color: #eaeaea;
}

Determining scroll direction

The detection logic compares two scroll positions. If the previous position is smaller than the current one, the page is moving toward the bottom. This requires a variable holding the last scroll position and another capturing the page's current scroll offset, wrapped in a function named isScrollingDown:

let previousScrollPosition = 0;

const isScrollingDown = () => {
  let currentScrolledPosition = window.scrollY || window.pageYOffset;
  let scrollingDown;

  if (currentScrolledPosition > previousScrollPosition) {
    scrollingDown = true;
  } else {
    scrollingDown = false;
  }
  previousScrollPosition = currentScrolledPosition;
  return scrollingDown;
};

The result drives a class toggle that controls the navbar styling:

const nav = document.querySelector('nav');

const handleNavScroll = () => {
  if (isScrollingDown()) {
    nav.classList.add('scroll-down');
    nav.classList.remove('scroll-up')
  } else {
    nav.classList.add('scroll-up');
    nav.classList.remove('scroll-down')
  }
}

Animating the navbar away

When the downward class is applied, the navigation needs a corresponding rule. The updated CSS:

nav {
  /* default styling */
  transition: top 500ms ease-in-out;
}

nav.scroll-up {
  top: 0;
}

nav.scroll-down {
  top: -100%;
}

Here top is set to -100% of the element's height so the bar slides out of sight. Translation or a fade-out would work equally well if a different animation is preferred.

Limiting scroll event cost

Scroll listeners fire frequently, and pixel-level accuracy is not needed for this effect. Throttling solves it: a throttle is a higher-order function that acts as a timer around the function it receives, so a 250ms throttle calls the handler no more than once every 250ms during scrolling. A basic implementation:

// initialize a throttleWait variable
var throttleWait;

const throttle = (callback, time) => {
  // if the variable is true, don't run the function
  if (throttleWait) return;

  // set the wait variable to true to pause the function
  throttleWait = true;

  // use setTimeout to run the function within the specified time
  setTimeout(() => {
    callback();

    // set throttleWait to false once the timer is up to restart the throttle function
    throttleWait = false;
  }, time);
}

Wrapping handleNavScroll in that throttle:

window.addEventListener("scroll", () => {
  throttle(handleNavScroll, 250)
});

The handler now runs only once per 250ms.

Keeping the navbar reachable

Custom JavaScript features still need to respect accessibility. One concern is that the navbar should stay visible while an element inside it holds focus; browsers generally scroll the focused region into view, but scroll handlers can interfere. Adding focus handling to the CSS:

nav.scroll-up,
nav:focus-within {
  top: 0;
}

Support for the focus-within selector is not universal, so a JavaScript fallback can stand in for it:

const handleNavScroll = () => {
  if (isScrollingDown() && !nav.contains(document.activeElement))) {
    nav.classList.add('scroll-down');
    nav.classList.remove('scroll-up')
  } else {
    nav.classList.add('scroll-up');
    nav.classList.remove('scroll-down')
  }
}

Now the scroll-down class is applied only when the user is scrolling down and the <nav> contains no focused element.

Motion itself also matters. Some users prefer no animation, which is detectable through the prefers-reduced-motion media query. In that case the script can skip the function entirely:

const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");

window.addEventListener("scroll", () => {
  if (mediaQuery && !mediaQuery.matches) {
    throttle(handleNavScroll, 250)
  }
});

The result is a smart navbar built from CSS and vanilla JavaScript: persistent navigation access without permanently consuming viewport space, and without an overbuilt dependency or accessibility compromises.