Okay, let's break down how to implement a full-screen header that smoothly collapses to a slim version on scroll using HTML and SCSS. This involves a combination of HTML structure, SCSS styling, and a bit of JavaScript to detect the scroll event and apply classes.
1. HTML Structure (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collapsing Header</title>
<link rel="stylesheet" href="style.scss">
</head>
<body>
<header class="header">
<div class="header-content">
<h1>My Awesome Website</h1>
<p>A captivating tagline.</p>
<nav>
<ul>
<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>
</nav>
</div>
</header>
<main>
<section class="content">
<!-- Your main content goes here -->
<h2>Section 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
<!-- Add more content sections as needed -->
</section>
<section class="content">
<h2>Section 2</h2>
<p>More content...</p>
</section>
<!-- ... more sections ... -->
</main>
<script src="script.js"></script>
</body>
</html>
Explanation:
<header class="header">: This is the main header element. The header class is crucial for styling and JavaScript targeting.
<div class="header-content">: This inner div holds the content of the header (title, tagline, navigation). This allows for better control over the layout within the header.
<nav>: Standard navigation element.
<main>: Contains the main content of your website. This is important for demonstrating the scrolling effect.
<section class="content">: Example content sections to create enough scrollable area.
<script src="script.js"></script>: Links to the JavaScript file that will handle the scroll event and class toggling.
2. SCSS Styling (style.scss)
// Variables
$header-full-height: 100vh;
$header-slim-height: 60px;
$transition-duration: 0.3s;
// Base Styles
body {
margin: 0;
font-family: sans-serif;
}
// Header Styles
.header {
background-color: #333;
color: white;
height: $header-full-height;
width: 100%;
position: fixed; // Important for staying at the top
top: 0;
left: 0;
z-index: 100; // Ensure it's above other content
display: flex;
justify-content: center;
align-items: center;
text-align: center;
transition: height $transition-duration ease-in-out;
.header-content {
padding: 20px;
}
h1 {
font-size: 3em;
margin-bottom: 10px;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
li {
margin: 0 15px;
a {
color: white;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
}
}
// Slim Header Styles (Applied with JavaScript)
.header.slim {
height: $header-slim-height;
.header-content {
display: none; // Hide content in slim mode
}
}
// Main Content Styles
main {
padding-top: $header-full-height; // Initial padding to account for full header
padding-bottom: 20px;
}
.content {
padding: 20px;
margin-bottom: 20px;
background-color: #f0f0f0;
border-radius: 5px;
}
Explanation:
- Variables: We define variables for the header heights and transition duration to make the code more maintainable.
.header:
height: $header-full-height;: Sets the initial height to 100% of the viewport height.
position: fixed;: Keeps the header fixed at the top of the screen during scrolling.
z-index: 100;: Ensures the header stays on top of other content.
transition: height $transition-duration ease-in-out;: This is the key to the smooth collapse effect. It tells the browser to animate the height property over the specified duration with an ease-in-out timing function.
.header.slim:
height: $header-slim-height;: Sets the height to the slim version.
display: none; on .header-content: Hides the header content when it's in the slim state. You might want to adjust this based on what you want to show in the slim header (e.g., just a logo).
main:
padding-top: $header-full-height;: Initially adds padding to the top of the main element to prevent content from being hidden behind the full-height header. This padding will be adjusted by JavaScript when the header collapses.
3. JavaScript (script.js)
document.addEventListener('DOMContentLoaded', function() {
const header = document.querySelector('.header');
const headerFullHeight = window.innerHeight; // Get the full viewport height
const headerSlimHeight = 60; // Match the SCSS variable
const scrollThreshold = 100; // Adjust this value to control when the header collapses
window.addEventListener('scroll', function() {
if (window.scrollY > scrollThreshold) {
header.classList.add('slim');
document.body.style.paddingTop = headerSlimHeight + 'px'; // Adjust body padding
} else {
header.classList.remove('slim');
document.body.style.paddingTop = headerFullHeight + 'px'; // Restore body padding
}
});
});
Explanation:
DOMContentLoaded: Ensures the script runs after the HTML is fully loaded.
querySelector('.header'): Selects the header element.
window.addEventListener('scroll', function() { ... });: Listens for the scroll event on the window.
window.scrollY: Gets the current vertical scroll position.
scrollThreshold: This variable determines how far the user needs to scroll before the header collapses. Adjust this value to your liking.
header.classList.add('slim'): Adds the slim class to the header, triggering the SCSS styles for the slim version.
header.classList.remove('slim'): Removes the slim class, restoring the full-height header.
document.body.style.paddingTop: Adjusts the padding-top of the body to prevent content from being hidden behind the header. This is crucial to maintain the layout.
How it Works:
- Initial State: The header starts with a height of 100vh (full screen) due to the SCSS. The
main content has padding-top equal to 100vh to prevent overlap.
- Scrolling: As the user scrolls down, the JavaScript checks the
window.scrollY value.
- Threshold Reached: When the scroll position exceeds the
scrollThreshold, the JavaScript adds the slim class to the header.
- CSS Transition: The
slim class triggers the CSS transition defined in the SCSS, smoothly animating the header's height from 100vh to 60px. The header content is hidden.
- Body Padding Adjustment: The JavaScript also adjusts the
padding-top of the body element to match the new header height (60px), ensuring the content doesn't jump or get hidden.
- Scrolling Back Up: When the user scrolls back up, and the scroll position falls below the
scrollThreshold, the slim class is removed, and the header smoothly transitions back to its full height. The body padding is restored.
Key Improvements and Considerations:
- Smooth Transition: The
transition property in the SCSS is essential for the smooth animation.
position: fixed;: This is crucial for keeping the header at the top of the screen during scrolling.
z-index: Ensures the header stays on top of other content.
scrollThreshold: Adjust this value to control when the header collapses.
- Body Padding: Adjusting the
padding-top of the body element is vital to prevent content from being hidden behind the header.
- Responsiveness: Consider how this will look on different screen sizes. You might need to adjust the header heights and the
scrollThreshold using media queries in your SCSS.
- Accessibility: Ensure the slim header still provides enough information or navigation options for users. Consider using ARIA attributes to improve accessibility.
- Performance: For very complex websites, consider using requestAnimationFrame for smoother scrolling performance. However, for most cases, the provided code should be sufficient.
- Content in Slim Header: You can customize what's displayed in the slim header by modifying the SCSS. For example, you might want to show a logo or a simplified navigation menu.
This comprehensive example provides a solid foundation for creating a collapsing header effect. Remember to adapt the code to your specific website's design and requirements. Let me know if you have any other questions.