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

281
Visualizações
How can I change the logo color by scrolling DIVs with specific classes?

I'm currently working on a project where the logo color should change depending on the background color. I would prefer to do this with CSS classes.

Unfortunately, the problem is: As soon as the first DIV of the class "bg02" is scrolled, the class changes for the logo too, but as soon as the following "bg02" divs are scrolled, nothing happens anymore. What am I doing wrong? Can you help me?

Here's my code:

JavaScript

$(document).on("scroll", function() {
    var scrollPos = $(document).scrollTop();
    $('#logo').each(function() {
        var currDiv = $(this);
        var refElement = $('.bg02');
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
        $('#logo').removeClass("inverted");
        currDiv.addClass("inverted");
        } else {
        currDiv.removeClass("inverted");
        }
    });
});

CSS

#logo {
    position: fixed;
    top: 20px;
    left: 5%;
    z-index: 100;
    font-size: 26px;
    font-weight: 700;
    color: #000;
}

#logo.inverted {
    color: #fff;
}

.bg01, .bg02 {
    position: relative;
    width: 100%;
    height: 600px;
}

.bg01 {
    background: #fff;
}

.bg02 {
    background: #000;
}

HTML

<div id="logo">Logo</div>


<div class="bg01"></div>

<div class="bg02"></div>

<div class="bg01"></div>

<div class="bg02"></div>

<div class="bg01"></div>

<div class="bg02"></div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your issue is that $('.bg02').position() can only return a single position, so it returns the position for the first one.

To use your method of checking scrollTop(), you need to loop .bg02 not #logo

Couple of small changes to your code:

Loop .bg02:

$('.bg02').each(function() {
  var refElement = $(this);

and a "break" inside the if - return false to stop the loop continuing and removing the inverted class for the later .bg02 that doesn't match

if (positioncheck) {
  $("#logo").addClass("inverted");
  return false;
}

You can also "tweak" when the logo gets inverted by considering its position, eg:

  var scrollPos = $(document).scrollTop() 
                  + $("#logo").position().top 
                  + ($("#logo").height() / 2)

as it was, it would only invert when bg02 got to the top.

Updated snippet:

$(document).on("scroll", function() {
  var scrollPos = $(document).scrollTop() 
                  + $("#logo").position().top 
                  + ($("#logo").height() / 2)
  $('.bg02').each(function() {
    var refElement = $(this);
    if (refElement.position().top <= scrollPos 
        && refElement.position().top + refElement.height() > scrollPos) {
      $('#logo').addClass("inverted");
      // found one, so exit .each
      return false;
    } else {
      $('#logo').removeClass("inverted");
    }
  });
});
#logo {
    position: fixed;
    top: 20px;
    left: 5%;
    z-index: 100;
    font-size: 26px;
    font-weight: 700;
    color: #000;
}

#logo.inverted {
    color: #fff;
}

.bg01, .bg02 {
    position: relative;
    width: 100%;
    height: 200px;
}

.bg01 {
    background: #fff;
}

.bg02 {
    background: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="logo">Logo</div>
<div class="bg01"></div>
<div class="bg02"></div>
<div class="bg01"></div>
<div class="bg02"></div>
<div class="bg01"></div>
<div class="bg02"></div>

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