Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!