Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

86
Visualizações
Get the top part of the Window using JavaScript

I'm making a Navbar. Well, it is working correctly (I scroll down and the style changes), but then, when I scroll to the top, the styles stay the same. Is there a way to change styles when I scroll to the top part?

Here's the HTML

<div class="navbar">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About us</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
  <a href="#"><span>Visit San Francisco</span></a>
</div>

Here's the JavaScript:

var body = document.querySelector("body");
var navbar = document.querySelector(".navbar");
var cover = document.querySelector(".cover");

function changeNavbarStyle() {
    navbar.classList.add("postConv");
}

function removeNavbarStyle() {
    navbar.classList.remove("postConv");
}

body.onscroll = changeNavbarStyle;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Most of modern browsers such as "Firefox", "Opera", "Edge", ... use document.documentElement.scrollTop to detect the position of scroll from top of the page. So maybe if you change your script as follows, it works:

var body = document.querySelector("body");
var navbar = document.querySelector(".navbar");
var cover = document.querySelector(".cover");

function changeNavbarStyle() {
    navbar.classList.add("postConv");
    if(document.documentElement.scrollTop === 0) {
        removeNavbarStyle();
    }
}

function removeNavbarStyle() {
    navbar.classList.remove("postConv");
}

body.onscroll = changeNavbarStyle;
body {
    min-height: 850px;
}

.postConv {
    background-color: #c5bdd1;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>nav-style</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="navbar">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
        <a href="#"><span>Visit San Francisco</span></a>
    </div>
      
    <script src="script.js"></script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use HTML DOM onScroll event. So your HTML code would look like:

<body id="body" onscroll="scroll()">
    <div class="navbar">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About us</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
        <a href="#"><span>Visit San Francisco</span></a>
    </div>
</body>

And in your JavaScript code you will define your scroll() method like:

function scroll() {
    var elmnt = document.getElementById("body");
    var y = elmnt.scrollTop;
    if(y == 0) {
        navbar.classList.remove("postConv");
    } else {
        navbar.classList.add("postConv");
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda