The user is asking for a responsive navbar that:
- Is responsive (works on different screen sizes)
- Hides when scrolling down
- Reappears when scrolling up
This is a common UI pattern often called a "smart navbar" or "hiding header". Let me create a comprehensive example with HTML, CSS, and JavaScript.
Key components:
- HTML - navbar structure with logo, menu items, and hamburger menu for mobile
- CSS - styling, responsive design, transitions
- JavaScript - scroll detection logic and mobile menu toggle
Let me create a complete, well-commented example. # Responsive Hide/Show Navbar on Scroll
I'll create a complete navbar that hides on scroll down and reappears on scroll up, with mobile responsiveness.
Complete Solution
<!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>
<style>
/* ============ CSS Reset & Base ============ */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
}
/* ============ Navbar Styles ============ */
.navbar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 5%;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
z-index: 1000;
/* Smooth transition for hide/show */
transition: transform 0.3s ease-in-out;
}
/* Hidden state - slides up */
.navbar.hidden {
transform: translateY(-100%);
}
/* ============ Logo ============ */
.logo {
font-size: 1.8rem;
font-weight: bold;
color: white;
text-decoration: none;
letter-spacing: 2px;
}
.logo:hover {
color: #f0f0f0;
}
/* ============ Navigation Links ============ */
.nav-links {
display: flex;
list-style: none;
gap: 30px;
}
.nav-links a {
color: white;
text-decoration: none;
font-size: 1rem;
font-weight: 500;
padding: 8px 16px;
border-radius: 25px;
transition: all 0.3s ease;
}
.nav-links a:hover {
background: rgba(255, 255, 255, 0.2);
transform: translateY(-2px);
}
/* ============ Hamburger Menu ============ */
.hamburger {
display: none;
flex-direction: column;
cursor: pointer;
gap: 5px;
padding: 10px;
}
.hamburger span {
width: 25px;
height: 3px;
background: white;
border-radius: 3px;
transition: all 0.3s ease;
}
/* Hamburger animation when active */
.hamburger.active span:nth-child(1) {
transform: rotate(45deg) translate(5px, 6px);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
}
.hamburger.active span:nth-child(3) {
transform: rotate(-45deg) translate(5px, -6px);
}
/* ============ Responsive Design ============ */
@media (max-width: 768px) {
.hamburger {
display: flex;
}
.nav-links {
position: fixed;
top: 70px;
left: 0;
right: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
flex-direction: column;
align-items: center;
padding: 20px 0;
gap: 10px;
/* Hidden by default on mobile */
transform: translateY(-150%);
opacity: 0;
transition: all 0.3s ease;
}
.nav-links.active {
transform: translateY(0);
opacity: 1;
}
.nav-links a {
padding: 12px 30px;
width: 80%;
text-align: center;
}
}
/* ============ Demo Content ============ */
.content {
margin-top: 70px;
padding: 40px 5%;
}
.section {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
color: #333;
border-bottom: 1px solid #eee;
}
.section:nth-child(odd) {
background: #f9f9f9;
}
/* ============ Scroll Indicator ============ */
.scroll-indicator {
position: fixed;
bottom: 20px;
right: 20px;
padding: 15px 20px;
background: #333;
color: white;
border-radius: 8px;
font-size: 0.9rem;
opacity: 0.8;
}
</style>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar" id="navbar">
<a href="#" class="logo">BRAND</a>
<ul class="nav-links" id="navLinks">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
<div class="hamburger" id="hamburger">
<span></span>
<span></span>
<span></span>
</div>
</nav>
<!-- Demo Content -->
<div class="content">
<section class="section" id="home">
🏠 Home - Scroll down to see the navbar hide!
</section>
<section class="section" id="about">
📖 About Section
</section>
<section class="section" id="services">
⚙️ Services Section
</section>
<section class="section" id="portfolio">
💼 Portfolio Section
</section>
<section class="section" id="contact">
📧 Contact Section
</section>
</div>
<!-- Scroll Direction Indicator -->
<div class="scroll-indicator" id="indicator">
Scroll direction: --
</div>
<script>
// ============ Navbar Hide/Show on Scroll ============
const navbar = document.getElementById('navbar');
const hamburger = document.getElementById('hamburger');
const navLinks = document.getElementById('navLinks');
const indicator = document.getElementById('indicator');
let lastScrollY = window.scrollY;
let ticking = false;
// Scroll threshold to prevent hiding on tiny scrolls
const SCROLL_THRESHOLD = 10;
function updateNavbar() {
const currentScrollY = window.scrollY;
const scrollDifference = currentScrollY - lastScrollY;
// Only act if scroll difference exceeds threshold
if (Math.abs(scrollDifference) > SCROLL_THRESHOLD) {
if (currentScrollY > lastScrollY && currentScrollY > 100) {
// Scrolling DOWN & past 100px - hide navbar
navbar.classList.add('hidden');
indicator.textContent = 'Scroll direction: ⬇️ DOWN (hidden)';
// Close mobile menu if open
navLinks.classList.remove('active');
hamburger.classList.remove('active');
} else if (currentScrollY < lastScrollY) {
// Scrolling UP - show navbar
navbar.classList.remove('hidden');
indicator.textContent = 'Scroll direction: ⬆️ UP (visible)';
}
lastScrollY = currentScrollY;
}
// Always show navbar when at top
if (currentScrollY <= 0) {
navbar.classList.remove('hidden');
indicator.textContent = 'At top (visible)';
}
ticking = false;
}
// Use requestAnimationFrame for smooth performance
window.addEventListener('scroll', () => {
if (!ticking) {
window.requestAnimationFrame(updateNavbar);
ticking = true;
}
});
// ============ Mobile Menu Toggle ============
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navLinks.classList.toggle('active');
});
// Close mobile menu when a link is clicked
navLinks.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
navLinks.classList.remove('active');
});
});
// ============ Optional: Debounced Version ============
/*
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
const debouncedUpdate = debounce(updateNavbar, 10);
window.addEventListener('scroll', debouncedUpdate);
*/
</script>
</body>
</html>
How It Works
Visual Flow Diagram
┌─────────────────────────────────────────────────┐
│ SCROLL EVENT │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────┐
│ Get Current │
│ Scroll Position │
└────────┬────────┘
│
▼
┌────────────────────────┐
│ Compare with Last │
│ Scroll Position │
└───────────┬────────────┘
│
┌───────────┴───────────┐
│ │
▼ ▼
┌───────────────┐ ┌───────────────┐
│ Scrolling UP │ │ Scrolling DOWN│
│ │ │ & Past 100px │
└───────┬───────┘ └───────┬───────┘
│ │
▼ ▼
┌───────────────┐ ┌───────────────┐
│ Remove │ │ Add │
│ .hidden class │ │ .hidden class │
│ │ │ │
│ Navbar slides │ │ Navbar slides │
│ DOWN (visible)│ │ UP (hidden) │
└───────────────┘ └───────────────┘
Key Features
| Feature | Description |
|---|
| Smooth Animation | CSS transition creates fluid hide/show effect |
| Scroll Threshold | Prevents navbar flickering on small scrolls |
| Performance | Uses requestAnimationFrame for optimization |
| Mobile Menu | Hamburger menu with animated icon |
| Auto-close Menu | Mobile menu closes when scrolling/clicking links |
Alternative: Simpler Vanilla JS Version
// Minimal version without threshold
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll <= 0) {
document.body.classList.remove('scroll-up');
return;
}
if (currentScroll > lastScroll) {
// Scrolling down
document.body.classList.remove('scroll-up');
document.body.classList.add('scroll-down');
} else {
// Scrolling up
document.body.classList.remove('scroll-down');
document.body.classList.add('scroll-up');
}
lastScroll = currentScroll;
});
/* Then use body classes to control navbar */
.scroll-down .navbar {
transform: translateY(-100%);
}
.scroll-up .navbar {
transform: translateY(0);
}
This creates a smooth, professional navbar that enhances user experience by giving more screen space when reading/scrolling down, while remaining easily accessible when scrolling up!