The user wants me to create a responsive navbar that:
- Hides when scrolling down
- Reappears when scrolling up
- Uses HTML, JS, and CSS
- Is responsive (works on different screen sizes)
Let me create a complete solution with:
- A clean, modern navbar design
- Mobile hamburger menu
- Smooth hide/show animation on scroll
- Responsive design for different screen sizes
Here's my plan:
- HTML structure with navbar, logo, nav links, and hamburger menu
- CSS for styling, animations, and responsive design
- JavaScript for scroll detection and mobile menu toggle
Let me write this out: # Responsive Hide-on-Scroll Navbar
Here's a complete implementation of a responsive navbar that hides on scroll down and reappears when scrolling up.
Full Implementation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hide on Scroll Navbar</title>
<style>
/* CSS Reset and Base Styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
min-height: 300vh; /* Extra height for scrolling demo */
color: #fff;
}
/* Navbar Styles */
.navbar {
position: fixed;
top: 0;
left: 0;
right: 0;
background: rgba(22, 33, 62, 0.95);
backdrop-filter: blur(10px);
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
z-index: 1000;
transition: transform 0.3s ease-in-out, box-shadow 0.3s ease;
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.3);
}
/* Hidden state */
.navbar.hidden {
transform: translateY(-100%);
}
/* Scrolled state - adds extra shadow */
.navbar.scrolled {
background: rgba(22, 33, 62, 0.98);
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
}
/* Logo */
.logo {
font-size: 1.8rem;
font-weight: 700;
color: #fff;
text-decoration: none;
display: flex;
align-items: center;
gap: 0.5rem;
}
.logo-icon {
width: 40px;
height: 40px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
}
/* Navigation Links */
.nav-links {
display: flex;
list-style: none;
gap: 2rem;
align-items: center;
}
.nav-links a {
color: #b8c5d6;
text-decoration: none;
font-weight: 500;
font-size: 1rem;
padding: 0.5rem 0;
position: relative;
transition: color 0.3s ease;
}
.nav-links a::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 2px;
background: linear-gradient(90deg, #667eea, #764ba2);
transition: width 0.3s ease;
}
.nav-links a:hover {
color: #fff;
}
.nav-links a:hover::after {
width: 100%;
}
/* CTA Button */
.cta-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff !important;
padding: 0.75rem 1.5rem !important;
border-radius: 25px;
font-weight: 600 !important;
transition: transform 0.3s ease, box-shadow 0.3s ease !important;
}
.cta-button::after {
display: none !important;
}
.cta-button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
/* Hamburger Menu */
.hamburger {
display: none;
flex-direction: column;
cursor: pointer;
gap: 6px;
padding: 5px;
z-index: 1001;
}
.hamburger span {
width: 28px;
height: 3px;
background: #fff;
border-radius: 3px;
transition: all 0.3s ease;
}
.hamburger.active span:nth-child(1) {
transform: rotate(45deg) translate(6px, 6px);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
transform: translateX(-10px);
}
.hamburger.active span:nth-child(3) {
transform: rotate(-45deg) translate(7px, -7px);
}
/* Main Content */
.main-content {
padding-top: 100px;
text-align: center;
}
.hero {
padding: 4rem 2rem;
max-width: 800px;
margin: 0 auto;
}
.hero h1 {
font-size: 3rem;
margin-bottom: 1rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.hero p {
font-size: 1.2rem;
color: #b8c5d6;
margin-bottom: 2rem;
}
.scroll-indicator {
margin-top: 3rem;
animation: bounce 2s infinite;
}
.scroll-indicator svg {
width: 40px;
height: 40px;
fill: #667eea;
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-15px);
}
60% {
transform: translateY(-7px);
}
}
/* Content Sections */
.section {
padding: 6rem 2rem;
max-width: 1000px;
margin: 0 auto;
}
.section h2 {
font-size: 2rem;
margin-bottom: 1rem;
color: #fff;
}
.section p {
color: #b8c5d6;
font-size: 1.1rem;
line-height: 1.8;
}
/* Scroll Progress Bar */
.scroll-progress {
position: fixed;
top: 0;
left: 0;
height: 3px;
background: linear-gradient(90deg, #667eea, #764ba2);
z-index: 1001;
width: 0%;
transition: width 0.1s ease-out;
}
/* Status Indicator */
.status {
position: fixed;
bottom: 20px;
right: 20px;
background: rgba(22, 33, 62, 0.95);
padding: 1rem 1.5rem;
border-radius: 10px;
font-size: 0.9rem;
color: #b8c5d6;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
z-index: 999;
}
.status span {
color: #667eea;
font-weight: 600;
}
/* Responsive Styles */
@media (max-width: 768px) {
.nav-links {
position: fixed;
top: 0;
right: -100%;
height: 100vh;
width: 70%;
max-width: 300px;
background: rgba(22, 33, 62, 0.98);
flex-direction: column;
justify-content: center;
padding: 2rem;
transition: right 0.4s ease;
box-shadow: -5px 0 30px rgba(0, 0, 0, 0.3);
}
.nav-links.active {
right: 0;
}
.nav-links li {
opacity: 0;
transform: translateX(20px);
transition: opacity 0.3s ease, transform 0.3s ease;
}
.nav-links.active li {
opacity: 1;
transform: translateX(0);
}
.nav-links.active li:nth-child(1) { transition-delay: 0.1s; }
.nav-links.active li:nth-child(2) { transition-delay: 0.2s; }
.nav-links.active li:nth-child(3) { transition-delay: 0.3s; }
.nav-links.active li:nth-child(4) { transition-delay: 0.4s; }
.nav-links.active li:nth-child(5) { transition-delay: 0.5s; }
.hamburger {
display: flex;
}
.hero h1 {
font-size: 2rem;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
z-index: 999;
}
.overlay.active {
opacity: 1;
visibility: visible;
}
}
@media (max-width: 480px) {
.navbar {
padding: 1rem;
}
.logo {
font-size: 1.4rem;
}
.logo-icon {
width: 35px;
height: 35px;
font-size: 1rem;
}
}
</style>
</head>
<body>
<!-- Scroll Progress Bar -->
<div class="scroll-progress" id="scrollProgress"></div>
<!-- Navbar -->
<nav class="navbar" id="navbar">
<a href="#" class="logo">
<div class="logo-icon">🚀</div>
NavBrand
</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="#contact">Contact</a></li>
<li><a href="#" class="cta-button">Get Started</a></li>
</ul>
<div class="hamburger" id="hamburger">
<span></span>
<span></span>
<span></span>
</div>
</nav>
<!-- Mobile Overlay -->
<div class="overlay" id="overlay"></div>
<!-- Main Content -->
<main class="main-content">
<section class="hero" id="home">
<h1>Scroll to See the Magic ✨</h1>
<p>
This navbar automatically hides when you scroll down and
reappears when you scroll up. Try it now!
</p>
<div class="scroll-indicator">
<svg viewBox="0 0 24 24">
<path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"/>
</svg>
</div>
</section>
<section class="section" id="about">
<h2>📱 Fully Responsive</h2>
<p>
This navbar is designed to work seamlessly across all devices.
On mobile, it features a smooth hamburger menu animation with
overlay background. Resize your browser to see it in action!
</p>
</section>
<section class="section" id="services">
<h2>⚡ Smooth Animations</h2>
<p>
CSS transitions provide buttery smooth animations for the
hide/show effect. The navbar slides up when hidden and slides
back down when revealed, creating a polished user experience.
</p>
</section>
<section class="section" id="contact">
<h2>🎯 Smart Detection</h2>
<p>
The JavaScript uses scroll position tracking to determine
scroll direction. It includes a threshold to prevent jittery
behavior during small scrolls, ensuring a stable experience.
</p>
</section>
<section class="section">
<h2>🎨 Modern Design</h2>
<p>
Features glassmorphism effects, gradient accents, and
thoughtful hover states. The progress bar at the top shows
your current scroll position through the page.
</p>
</section>
</main>
<!-- Status Indicator -->
<div class="status" id="status">
Navbar: <span id="navbarStatus">Visible</span>
</div>
<script>
// DOM Elements
const navbar = document.getElementById('navbar');
const hamburger = document.getElementById('hamburger');
const navLinks = document.getElementById('navLinks');
const overlay = document.getElementById('overlay');
const scrollProgress = document.getElementById('scrollProgress');
const navbarStatus = document.getElementById('navbarStatus');
// Scroll Variables
let lastScrollY = window.scrollY;
let ticking = false;
const scrollThreshold = 10; // Minimum scroll amount to trigger hide/show
// ======================
// Scroll Hide/Show Logic
// ======================
function handleScroll() {
const currentScrollY = window.scrollY;
const scrollDifference = currentScrollY - lastScrollY;
// Update scroll progress bar
const scrollHeight = document.documentElement.scrollHeight - window.innerHeight;
const scrollPercentage = (currentScrollY / scrollHeight) * 100;
scrollProgress.style.width = `${scrollPercentage}%`;
// Add/remove scrolled class for extra styling
if (currentScrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
// Only trigger if scroll amount exceeds threshold
if (Math.abs(scrollDifference) > scrollThreshold) {
if (scrollDifference > 0 && currentScrollY > 100) {
// Scrolling DOWN - Hide navbar
navbar.classList.add('hidden');
navbarStatus.textContent = 'Hidden';
} else {
// Scrolling UP - Show navbar
navbar.classList.remove('hidden');
navbarStatus.textContent = 'Visible';
}
lastScrollY = currentScrollY;
}
ticking = false;
}
// Optimized scroll listener with requestAnimationFrame
window.addEventListener('scroll', () => {
if (!ticking) {
window.requestAnimationFrame(handleScroll);
ticking = true;
}
});
// ======================
// Mobile Menu Toggle
// ======================
function toggleMobileMenu() {
hamburger.classList.toggle('active');
navLinks.classList.toggle('active');
overlay.classList.toggle('active');
document.body.style.overflow = navLinks.classList.contains('active') ? 'hidden' : '';
}
hamburger.addEventListener('click', toggleMobileMenu);
overlay.addEventListener('click', toggleMobileMenu);
// Close menu when a link is clicked
navLinks.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
if (navLinks.classList.contains('active')) {
toggleMobileMenu();
}
});
});
// ======================
// Smooth Scroll for Anchor Links
// ======================
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const offsetTop = target.offsetTop - 80;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
});
});
// ======================
// Keyboard Accessibility
// ======================
hamburger.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleMobileMenu();
}
});
// Close menu on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && navLinks.classList.contains('active')) {
toggleMobileMenu();
}
});
// Reset on resize
window.addEventListener('resize', () => {
if (window.innerWidth > 768 && navLinks.classList.contains('active')) {
toggleMobileMenu();
}
});
</script>
</body>
</html>
Key Features
| Feature | Description |
|---|
| 🔄 Hide on Scroll Down | Navbar smoothly slides up when scrolling down |
| ⬆️ Show on Scroll Up | Navbar reappears when scrolling up |
| 📱 Responsive Design | Hamburger menu for mobile devices |
| ⚡ Performance | Uses requestAnimationFrame for optimization |
| 🎯 Scroll Threshold | Prevents jittery behavior with minimum scroll detection |
| 📊 Progress Bar | Visual scroll progress indicator |
| ✨ Smooth Animations | CSS transitions for fluid motion |
How It Works
┌─────────────────────────────────────────────────────┐
│ SCROLL DOWN │
│ ↓ │
│ ┌─────────────────────────────────────────┐ │
│ │ currentScrollY > lastScrollY? │ │
│ │ + scrollDifference > threshold? │ │
│ └─────────────────────────────────────────┘ │
│ ↓ YES │
│ navbar.classList.add('hidden') │
│ transform: translateY(-100%) │
├─────────────────────────────────────────────────────┤
│ SCROLL UP │
│ ↓ │
│ ┌─────────────────────────────────────────┐ │
│ │ currentScrollY < lastScrollY? │ │
│ │ + scrollDifference > threshold? │ │
│ └─────────────────────────────────────────┘ │
│ ↓ YES │
│ navbar.classList.remove('hidden') │
│ transform: translateY(0) │
└─────────────────────────────────────────────────────┘
Core JavaScript Logic
// Simplified version of the scroll detection
let lastScrollY = window.scrollY;
const threshold = 10;
window.addEventListener('scroll', () => {
const currentScrollY = window.scrollY;
const difference = currentScrollY - lastScrollY;
if (Math.abs(difference) > threshold) {
if (difference > 0) {
// Scrolling DOWN → Hide
navbar.classList.add('hidden');
} else {
// Scrolling UP → Show
navbar.classList.remove('hidden');
}
lastScrollY = currentScrollY;
}
});
This creates a polished, modern navbar with smooth animations and excellent mobile support! 🚀