Skip to content
Advertisement

Change the background color based on what the fixed div is currently over

I have a NavBar with fixed position and there will be divs with different background color. when scrolling i want my nav to have the same color of the div that’s it is over.

my goal for this is when scrolling the content of the other div does not overlap with the navbar content like this

enter image description here

Advertisement

Answer

You can change the navbar background using javascript by checking which element is active on scroll event, I going to post here a simple solution using jquery, but you could use some React dependence that gives you the active element based on scroll to change the color, building a custom hook is also a good choice to solve your use case:

$(window).scroll(function() {
    const scrollDistance = $(window).scrollTop();
    $('section').each(function(i) {
        if ($(this).position().top <= scrollDistance) {
            const bgColor = `linear-gradient(#ccc, ${$(this).css('background-color')})`;
            $('nav').css('background', bgColor);
        }
    });
});
*{
   box-sizing: border-box;
}
.container{
  width: 400px;
  position: relative;
}
section, nav{
  padding: 1rem;
}
section{
    padding-top: 40px;
}
nav{
  position: fixed;
  top: 0;
  display: block;
  background: transparent;
  font-weight: 600;
  width: 400px;
}
.first-section{
  background: yellow;
} 
.second-section{
  background: #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<nav>Navigation</nav>
<section class="first-section">
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</section>

<section class="second-section">
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?
</section>
  
  <section class="first-section">
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</section>
</div>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement