yCafEe
yCafEe

Reputation: 1

Fixing Header with JavaScript After Scrolling Past 100vh Header:

I am trying to make a header that stays fixed when I scroll down, but it only goes until the end of the header, which is set to 100vh. Does anyone have any idea how I can solve this without stopping using min-height?

I tried using JS to replace the class with fixed so that it only fixes after scrolling past the header, but without success.

document.addEventListener('DOMContentLoaded', function() {
  const nav = document.getElementById('mainNav'); // Certifique-se de que o ID está correto
  const header = document.querySelector('.header');

  function handleScroll() {
    const headerHeight = header.offsetHeight;
    const scrollPosition = window.scrollY;

    if (scrollPosition > headerHeight) {
      nav.classList.add('fixed');
    } else {
      nav.classList.remove('fixed');
    }
  }

  window.addEventListener('scroll', handleScroll);
});
.header {
  min-height: 100vh;
  width: 100%;
  background-image: linear-gradient(rgba(4, 9, 30, 0.7), rgba(4, 9, 30, 0.7)), url('img/background-main.jpg');
  background-position: center;
  background-size: cover;
  text-align: center;
  color: #fff;
  position: relative;
}

nav {
  display: flex;
  padding: 2% 6%;
  justify-content: space-between;
  align-items: center;

  z-index: 1000;
}

nav.fixed {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1001;
}

nav img {
  width: 200px;
}

.nav-links {
  flex: 1;
  text-align: right;
}

.nav-links ul li {
  list-style: none;
  display: inline-block;
  padding: 8px 12px;
  position: relative;

}

.nav-links ul li a {
  color: #fff;
  text-decoration: none;
  font-size: 16px;
  font-weight: bold;
}

.nav-links ul li::after {
  content: '';
  width: 0%;
  height: 2px;
  background: #1b99ca;
  display: block;
  margin: auto;
  transition: 0.5s;
}

.nav-links ul li:hover::after {
  width: 100%;
}

.nav-links ul li:hover>ul.dropdown {
  display: block;
}

ul li ul.dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: rgba(41, 145, 255, 0.425);
  z-index: 999;
  display: none;
  min-width: 130px;
  border-radius: 5px;
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
}

ul li ul.dropdown li {
  display: block;
  padding: 10px;
}

ul li ul.dropdown li a {
  color: #fff;
  text-decoration: none;
  display: block;
  font-size: 14px;
  font-weight: 600;
  transition: 0.3s;
  text-align: left;
}

ul li ul.dropdown li:hover {
  background: #003580;
}

.text-box {
  width: 90%;
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.text-box h1 {
  font-size: 62px;
}

text-box p {
  margin: 10px 0 40px;
  font-size: 14px;
  color: #fff;
}

.hero-btn {
  display: inline-block;
  text-decoration: none;
  color: #fff;
  border: 1px solid #fff;
  padding: 12px 34px;
  font-size: 13px;
  background: transparent;
  position: relative;
  cursor: pointer;
  transition: 0.6s;
  margin-top: 15px;
  border-radius: 5px;
}

.hero-btn:hover {
  background-color: #1b99ca;
  border: 1px solid #1b99ca;
  transition: 1s;
}

nav .fa {
  display: none;
}

nav .fa-bars {
  font-size: 25px;
}
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg==" crossorigin="anonymous" referrerpolicy="no-referrer">

<section class="header">
  <nav id="mainNav" class="mainNav">
    <div class="logo">
      <a href="index.html"> <img src="img/logo-padrao.png" alt="logo da JL Marine"></a>
    </div>
    <div class="nav-links" id="navLinks">
      <i class="fa fa-times" onclick="hideMenu()"></i>
      <ul class="ul">
        <li><a href="index.html">Início</a></li>
        <li>
          <a href="#">Sobre</a>
          <ul class="dropdown">
            <li><a href="sobre.html">A Empresa</a></li>
            <li><a href="equipe.html">Equipe</a></li>
          </ul>
        </li>
        <li>
          <a href="services/services.html">Serviços</a>
          <ul class="dropdown">
            <li><a href="services/marine.html">Marine</a></li>
            <li><a href="services/log.html">Logística</a></li>
            <li><a href="services/desp-adua.html">Despacho Aduaneiro</a></li>
            <li><a href="services/comex.html">Comex</a></li>
            <li><a href="services/outros.html">Outros</a></li>
          </ul>
        </li>
        <li><a href="contacts.html">Contato</a></li>
      </ul>
    </div>
    <i class="fa fa-bars" onclick="showMenu()"></i>
  </nav>
  <div class="text-box">
    <h1>Conheça a JL Marine!</h1>
    <p>Confiabilidade & Excelência</p>
    <a href="sobre.html" class="hero-btn">Saiba Mais</a>
  </div>
</section>

Upvotes: 0

Views: 87

Answers (0)

Related Questions