<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Hide/Show Navbar</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
padding-top: 70px; /* Prevent content jump under fixed navbar */
line-height: 1.6;
}
/* Navbar Styles */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
transition: transform 0.3s ease-in-out;
z-index: 1000;
}
.navbar.hidden {
transform: translateY(-100%);
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
height: 70px;
}
.logo {
color: white;
font-size: 1.5rem;
font-weight: bold;
text-decoration: none;
}
.nav-menu {
display: flex;
list-style: none;
gap: 2rem;
}
.nav-menu a {
color: white;
text-decoration: none;
font-weight: 500;
transition: color 0.3s;
}
.nav-menu a:hover {
color: #ffd700;
}
.hamburger {
display: none;
flex-direction: column;
cursor: pointer;
gap: 4px;
}
.hamburger span {
width: 25px;
height: 3px;
background: white;
transition: 0.3s;
}
/* Mobile Styles */
@media (max-width: 768px) {
.hamburger {
display: flex;
}
.nav-menu {
position: fixed;
top: 70px; /* Below navbar height */
left: -100%;
width: 100%;
height: calc(100vh - 70px);
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
flex-direction: column;
justify-content: flex-start;
align-items: center;
padding-top: 2rem;
gap: 1rem;
transition: left 0.3s ease-in-out;
}
.nav-menu.active {
left: 0;
}
.nav-menu li {
opacity: 0;
transform: translateX(30px);
transition: all 0.3s ease;
}
.nav-menu.active li {
opacity: 1;
transform: translateX(0);
}
.nav-menu li:nth-child(1) { transition-delay: 0.1s; }
.nav-menu li:nth-child(2) { transition-delay: 0.2s; }
.nav-menu li:nth-child(3) { transition-delay: 0.3s; }
.nav-menu li:nth-child(4) { transition-delay: 0.4s; }
}
/* Demo Content */
.content {
min-height: 200vh;
padding: 2rem;
}
.section {
margin-bottom: 4rem;
padding: 2rem;
background: #f4f4f4;
border-radius: 8px;
}
</style>
</head>
<body>
<nav class="navbar" id="navbar">
<div class="nav-container">
<a href="#" class="logo">Sherlock</a>
<ul class="nav-menu" id="nav-menu">
<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>
</ul>
<div class="hamburger" id="hamburger">
<span></span>
<span></span>
<span></span>
</div>
</div>
</nav>
<div class="content">
<div class="section" id="home">
<h1>Scroll down to see the navbar hide, scroll up to reveal it!</h1>
<p>This is a fully responsive navbar built with HTML, CSS, and vanilla JS. It hides smoothly when scrolling down and reappears when scrolling up (or at the top of the page).</p>
</div>
<div class="section" id="about">
<h2>About Section</h2>
<p>Keep scrolling... Features include mobile hamburger menu with slide-in animation.</p>
</div>
<div class="section" id="services">
<h2>Services</h2>
<p>Made with flexbox for layout, CSS transitions for smooth effects, and scroll event listener for hide/show logic.</p>
</div>
<div class="section" id="contact">
<h2>Contact</h2>
<p>Resize the window to test responsiveness!</p>
</div>
</div>
<script>
// Mobile menu toggle
const hamburger = document.getElementById('hamburger');
const navMenu = document.getElementById('nav-menu');
hamburger.addEventListener('click', () => {
navMenu.classList.toggle('active');
});
// Close mobile menu when clicking a link
navMenu.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
navMenu.classList.remove('active');
});
});
// Navbar hide/show on scroll
let prevScrollPos = window.pageYOffset;
const navbar = document.getElementById('navbar');
window.onscroll = () => {
const currentScrollPos = window.pageYOffset;
// Show navbar when scrolling up or at top
if (prevScrollPos > currentScrollPos || currentScrollPos === 0) {
navbar.classList.remove('hidden');
} else {
navbar.classList.add('hidden');
}
prevScrollPos = currentScrollPos;
};
</script>
</body>
</html>
How It Works
- HTML: Standard navbar structure with logo, menu, and hamburger icon.
- CSS:
- Fixed positioning and
transform: translateY(-100%) for smooth hide/show.
- Flexbox for layout.
- Media query for mobile: Hamburger menu slides in from left with staggered animations.
- JS:
- Tracks scroll position to detect direction.
- Adds/removes
hidden class based on scroll up/down (always shows at scrollY === 0).
- Toggles mobile menu independently.
Copy-paste into an HTML file to test. Fully responsive, performant, and no external dependencies! 🚀