<html><head><base href="https://sticky-navbar.example.com" /></head>
<body>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
line-height: 1.6;
}
.header {
background-color: #4CAF50;
color: white;
text-align: center;
padding: 2rem;
}
.navbar {
background-color: #333;
overflow: hidden;
transition: all 0.3s ease;
}
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: background-color 0.3s;
}
.navbar a:hover {
background-color: #ddd;
color: black;
}
.content {
padding: 20px;
margin-bottom: 2000px; /* To create scrollable content */
}
.fix-navbar {
position: fixed;
top: 0;
width: 100%;
z-index: 1000;
}
.section {
margin-bottom: 2rem;
padding: 1rem;
background-color: white;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
h2 {
color: #4CAF50;
margin-bottom: 1rem;
}
/* Animation for navbar items */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
.navbar a {
animation: fadeIn 0.5s ease forwards;
opacity: 0;
}
.navbar a:nth-child(1) { animation-delay: 0.1s; }
.navbar a:nth-child(2) { animation-delay: 0.2s; }
.navbar a:nth-child(3) { animation-delay: 0.3s; }
.navbar a:nth-child(4) { animation-delay: 0.4s; }
.navbar a:nth-child(5) { animation-delay: 0.5s; }
</style>
<div class="header">
<h1>Sticky Navbar Example</h1>
</div>
<div class="navbar" id="navbar">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#portfolio">Portfolio</a>
<a href="#contact">Contact</a>
</div>
<div class="content">
<div id="home" class="section">
<h2>Home</h2>
<p>Welcome to our website! Explore our services and portfolio to learn more about what we offer.</p>
</div>
<div id="about" class="section">
<h2>About Us</h2>
<p>We are a team of passionate professionals dedicated to delivering high-quality solutions to our clients.</p>
</div>
<div id="services" class="section">
<h2>Our Services</h2>
<p>We offer a wide range of services including web design, development, and digital marketing strategies.</p>
</div>
<div id="portfolio" class="section">
<h2>Portfolio</h2>
<p>Check out some of our recent projects and see how we've helped businesses grow and succeed online.</p>
</div>
<div id="contact" class="section">
<h2>Contact Us</h2>
<p>Get in touch with us to discuss your project or to learn more about how we can help your business.</p>
</div>
</div>
<script>
const navbar = document.getElementById("navbar");
const sticky = navbar.offsetTop;
function handleStickyNavbar() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("fix-navbar");
} else {
navbar.classList.remove("fix-navbar");
}
}
window.onscroll = function() {
handleStickyNavbar();
};
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
</script>
</body></html>