I'm focused on grasping the user's need for a responsive navbar built with HTML, CSS, and JavaScript, specifically the desired "scroll behavior." My central task is to understand how the navigation bar should dynamically hide on downward scrolls and reappear on upward scrolls. This is the core functionality I must address.
Refining the Approach
I'm now zeroing in on the practical steps. I'm focusing on the HTML structure: <nav>, logo, links, and the mobile toggle. CSS will handle the styling, especially the "hidden" state with transform: translateY(-100%) and transitions. I'll need a way to detect scroll direction in JavaScript.
Outlining the Structure
I'm solidifying the HTML skeleton: a <nav> with a logo, links within a <ul>, and a mobile toggle. My CSS will be based on flexbox for layout and fixed positioning, with media queries for different screen sizes. I'm prioritizing the mobile-first approach. The .scroll-down class will be key, and I will hide the nav bar using transform: translateY(-100%).
Developing the Implementation Plan
I'm now structuring my approach. I've broken the task down into HTML, CSS, and JavaScript components. Step-by-step implementation includes the HTML structure (nav, logo, links, mobile toggle). CSS will style the navbar and the hide effect via transform. JavaScript will handle scroll direction detection, and adding/removing the scroll-down class.
Structuring the Code
I'm now consolidating the project into distinct HTML, CSS, and JavaScript files. The HTML will be a semantic <nav> with a logo, links, and a mobile toggle button. The CSS will manage the layout with flexbox and the hide/show transition using transform. JavaScript will detect scroll direction, adding or removing the scroll-down class for the hiding behavior.
Here is a complete step-by-step solution. I have separated the HTML, CSS, and JavaScript to make it easy to understand.
How it works
CSS fixed position: Keeps the navbar at the top.
CSS transition: Makes the movement smooth.
JS Logic: We compare the the current scroll position with the previous scroll position.
If Current > Previous = Scrolling Down (Add class to hide).
If Current < Previous = Scrolling Up (Remove class to show).
1. The HTML
We need a semantic structure: a container, the logo, the navigation links, and a "hamburger" button for mobile devices.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Navbar</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Navigation -->
<nav id="navbar">
<div class="logo">Brand</div>
<ul class="nav-links" id="nav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
<!-- Mobile Menu Icon -->
<div class="burger" id="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<!-- Dummy content to allow scrolling -->
<section style="height: 200vh; padding: 100px 20px;">
<h1>Scroll down to hide me, scroll up to show me!</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
</section>
<script src="script.js"></script>
</body>
</html>
2. The CSS
We use Flexbox for layout. The key magic happens in the #navbar class (for the sticky positioning) and the nav-hidden class (which moves the navbar up out of sight).
css
/* General Resets */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
/* Navbar Styling */
#navbar {
position: fixed; /* Sticks to the top */
top: 0;
width: 100%;
height: 70px;
background-color: #333;
color: white;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 2rem;
z-index: 1000;
/* Animation Transition */
transition: top 0.3s;
}
/* Class to hide navbar (will be added by JS) */
#navbar.nav-hidden {
top: -70px; /* Moves it exactly out of view */
}
.logo {
font-size: 1.5rem;
font-weight: bold;
letter-spacing: 2px;
}
.nav-links {
display: flex;
list-style: none;
}
.nav-links li a {
color: white;
text-decoration: none;
margin-left: 2rem;
font-weight: 500;
}
/* Burger Menu (Hidden on Desktop) */
.burger {
display: none;
cursor: pointer;
}
.burger div {
width: 25px;
height: 3px;
background-color: white;
margin: 5px;
}
/* --- RESPONSIVENESS (Mobile) --- */
@media screen and (max-width: 768px) {
.nav-links {
position: absolute;
right: 0px;
height: 92vh;
top: 70px;
background-color: #333;
display: flex;
flex-direction: column;
align-items: center;
width: 50%;
transform: translateX(100%); /* Hide off-screen */
transition: transform 0.5s ease-in;
}
/* This class is added when burger is clicked */
.nav-links.nav-active {
transform: translateX(0%);
}
.nav-links li {
opacity: 1;
margin-top: 30px;
}
.burger {
display: block; /* Show burger on mobile */
}
}
3. The JavaScript
This script handles two things:
The scroll detection logic.
The click event for the mobile hamburger menu.
javascript
// 1. Select elements
const navbar = document.getElementById("navbar");
const burger = document.getElementById("burger");
const navLinks = document.getElementById("nav-links");
// 2. Variable to store previous scroll position
let lastScrollTop = 0;
// 3. Scroll Event Listener
window.addEventListener("scroll", function() {
// Get current scroll position
let scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Logic: Is current scroll greater than last scroll?
if (scrollTop > lastScrollTop) {
// SCROLLING DOWN -> Hide Navbar
// We typically don't hide if we are at the very top (top 0)
navbar.classList.add("nav-hidden"); // CSS handles the movement
// Optional: Close mobile menu if user scrolls down
navLinks.classList.remove('nav-active');
} else {
// SCROLLING UP -> Show Navbar
navbar.classList.remove("nav-hidden");
}
// Update lastScrollTop to the current position for the next event
lastScrollTop = scrollTop;
});
// 4. Mobile Menu Toggle
burger.addEventListener('click', () => {
navLinks.classList.toggle('nav-active');
});
Summary of the Logic
The "Hidden" Class: In CSS, #navbar.nav-hidden sets top: -70px. Since the navbar is 70px tall, this effectively slides it completely out of view.
Direction Detection: We store lastScrollTop. Every time the user scrolls, we check if the new number is bigger or smaller.
New > Old = Going Down.
New < Old = Going Up.
Transition: The CSS property transition: top 0.3s ensures the navbar glides in and out rather than snapping instantly.