Certainly! Below is an example of HTML, CSS, and JavaScript code to create a sticky header on a website:
HTML:
CSS (styles.css):
cssbody { font-family: Arial, sans-serif; margin: 0;}.sticky-header { background-color: #333; color: #fff; padding: 10px 20px; position: fixed; top: 0; left: 0; width: 100%; z-index: 1000;}.content { padding: 50px 20px; margin-top: 80px; /* Make space for the fixed header */}h1 { margin: 0;}
JavaScript (script.js):
javascriptdocument.addEventListener("DOMContentLoaded", function() { window.addEventListener("scroll", function() { const header = document.querySelector(".sticky-header"); const scrollY = window.scrollY || window.pageYOffset; if (scrollY > 0) { header.classList.add("scrolled"); } else { header.classList.remove("scrolled"); } });});
In this example, we have an HTML file with a header and content section. The header has a class of sticky-header
, and the content is wrapped in a div
with a class of content
.
The CSS file (styles.css
) styles the header and content sections. The header is initially styled with a background color of #333
and white text. It's set to be fixed at the top of the page with position: fixed;
, ensuring it remains visible even when scrolling. The content section has a top margin to create space for the fixed header.
The JavaScript file (script.js
) listens for the scroll
event. When the user scrolls, it checks the scroll position (scrollY
), and if it's greater than zero, it adds the class scrolled
to the header, which can be used to apply additional styles if desired.
Finally, you can customize the styles and add more content to suit your specific website needs.
Извините за недоразумение, но я не имею способности просматривать внешние ресурсы или статьи напрямую. Если вы предоставите текст статьи или краткое описание её содержания, я с удовольствием помогу сделать выводы на основе этой информации.