Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
3
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      margin: 0;
      font-family: 'Arial', sans-serif;
    }

    header {
      background-color: #333;
      padding: 10px 0;
      color: white;
      text-align: center;
      position: relative;
      z-index: 1000;
    }

    .sticky {
      position: fixed;
      top: 0;
      width: 100%;
    }

    section {
      height: 1000px; /* Just for demonstration purposes */
    }
  </style>
</head>
<body>
  <header id="main-header">
    <h1>My Sticky Header</h1>
  </header>

  <section>
    <!-- Content goes here -->
  </section>

  <script>
    window.onscroll = function() { stickyHeader() };

    var header = document.getElementById("main-header");
    var sticky = header.offsetTop;

    function stickyHeader() {
      if (window.pageYOffset > sticky) {
        header.classList.add("sticky");
      } else {
        header.classList.remove("sticky");
      }
    }
  </script>
</body>
</html>
Editor is loading...
Leave a Comment