Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda