How to change the text color where part of the background is white or black


Rex Hope

How can I change the color of the menu if the menu scrolls past the background: black, the menu text should be white, or if the background is white, the menu text should be black.

HTML

<div class="fixed-side-navbar">
    <ul class="nav">
        <li><a href="#example-one"><span>Example One</span></a></li>
        <li><a href="#example-two"><span>Example Two</span></a></li>
        <li><a href="#example-three"><span>Example Three</span></a></li>
        <li><a href="#example-four"><span>Example Four</span></a></li>
        <li><a href="#example-five"><span>Example Five</span></a></li>
        <li><a href="#example-six"><span>Example Six</span></a></li>
    </ul>
</div>
<div id="example-one"></div>
<div id="example-two"></div>
<div id="example-three"></div>
<div id="example-four"></div>
<div id="example-five"></div>
<div id="example-six"></div>

CSS:

.example-one {
  background: black;
}
.example-two { 
  background: black;
}
.example-three { background: white }
.example-four { background: black }
.example-five { background: white; }
.example-six { background: black; }
User 7207170

Here is a demo using Jquery.

  • Use variables to store the scroll position , and the offset of the element from the top where you want the color to change, element1 and element2 in this example.
  • When using scrolling documents, check the offset from the top of the scroll to the offset of the top position of the element$(document).scroll(function()) .
  • Apply appropriate CSS colors or styles to elements based on conditional statements .

$(document).ready(function() {
  var scroll_pos = 0;
  $(document).scroll(function() {
    scroll_pos = $(this).scrollTop();
    var element1 = $('#backgrund2').offset().top;
    var element2 = $('#backgrund3').offset().top;
    if (scroll_pos < element2 && scroll_pos > element1) {
      $(".fixed-side-navbar a").css('color', 'black');
    } else {
      $(".fixed-side-navbar a").css('color', 'white');
    }
  });
});
body {
  height: 2000px;
  width: 100%;
}

.fixed-side-navbar {
  position: fixed;
  top: 0;
  left: 0;
}

.fixed-side-navbar a {
  text-decoration: none;
  color: white;
}

#backgrund1,
#backgrund2,
#backgrund3 {
  width: 100%;
  background: black;
  height: 700px;
}

#backgrund2 {
  background: white;
}

#backgrund3 {
  background: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fixed-side-navbar">
  <ul class="nav">
    <li><a href="#background1"><span>Intro</span></a></li>
    <li><a href="#background2"><span>MSQ</span></a></li>
    <li><a href="#background3"><span>ICM</span></a></li>
    <li><a href="#sec2"><span>Section two</span></a></li>
    <li><a href="#sec3"><span>Section three</span></a></li>
    <li><a href="#sec4"><span>Section four</span></a></li>
  </ul>
</div>
<div id="backgrund1"></div>
<div id="backgrund2"></div>
<div id="backgrund3"></div>

renew:

$(document).ready(function() {
  var scroll_pos = 0;
  var $window  = 7*$(window).height()/10; //Since, top=70%
  $(document).scroll(function() {
    scroll_pos = $(this).scrollTop()+$window;
    var element1 = $('#example-one').offset().top;
    var element2 = $('#example-two').offset().top;
    var element3 = $('#example-three').offset().top;
    var element4 = $('#example-four').offset().top;
    var element5 = $('#example-five').offset().top;
    var element6 = $('#example-six').offset().top;


    if ((scroll_pos >= element3 && scroll_pos < element4) || (scroll_pos >= element5 && scroll_pos < element6)) {
      $(".fixed-side-navbar a").css('color', 'black');
    } else {
      $(".fixed-side-navbar a").css('color', 'white');
    }
  });
});
.fixed-side-navbar {
  position: fixed;
  top: 70%;
  left: 0;
}

.fixed-side-navbar a {
  text-decoration: none;
  color: white;
}

#example-one {
  background: black;
  height: 700px;
  width: 100%;
}

#example-two {
  background: black;
  height: 700px;
  width: 100%;
}

#example-three {
  background: white;
  height: 700px;
  width: 100%;
}

#example-four {
  background: black;
  height: 700px;
  width: 100%;
}

#example-five {
  background: white;
  height: 700px;
  width: 100%;
}

#example-six {
  background: black;
  height: 700px;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="fixed-side-navbar">
  <ul class="nav">
    <li><a href="#example-one"><span>Example One</span></a></li>
    <li><a href="#example-two"><span>Example Two</span></a></li>
    <li><a href="#example-three"><span>Example Three</span></a></li>
    <li><a href="#example-four"><span>Example Four</span></a></li>
    <li><a href="#example-five"><span>Example Five</span></a></li>
    <li><a href="#example-six"><span>Example Six</span></a></li>
  </ul>
</div>
<div id="example-one"></div>
<div id="example-two"></div>
<div id="example-three"></div>
<div id="example-four"></div>
<div id="example-five"></div>
<div id="example-six"></div>

Related


How to change background or text color of part of QTreeWidgetItem

Yam Dedeman I am developing a Qt application in C++. The application revolves around a tree widget that has a subtree for each entity in the system. Entities have a "highlight" color that can be specified by the user and highlights events related to a particul

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Change element color to black or white based on background color

AntunCrnja I'm building a new website and I want to change the logo color (ie ) position: fixedbased on the background color or image . I found an example on this site . You can see how the logo and certain elements change color based on the background. body {

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

Black text - white on black background

Brunon Blok I'm creating a game that has a world that the player can move around. And there are some GUIs (eq, stats..) with elements without background (the background is the game world). These elements have black text color, my question is: Can this text be

How to display white text with black background?

Glow Nuts I'm trying to figure out why my text isn't showing up here. I'm assuming it's not white and won't show up against a black background. So I'm trying to figure out why the background is painted black and the text is not showing. I am using JavaFX 12. H

How to display white text with black background?

Glow Nuts I'm trying to figure out why my text isn't showing up here. I'm assuming it's not white and won't show up against a black background. So I'm trying to figure out why the background is painted black and the text is not showing. I am using JavaFX 12. H

How to display white text with black background?

Glow Nuts I'm trying to figure out why my text isn't showing up here. I'm assuming it's not white and won't show up against a black background. So I'm trying to figure out why the background is painted black and the text is not showing. I am using JavaFX 12. H

How to change rxvt color to white on black?

Nickolai Leschov By default, on Ubuntu, the rxvt terminal is black on white. How can I change it to white on black? I am running Lubuntu 16.04 LTS Terence Note: Updated answer to include desktops that are not loaded .Xresourcesor archived by default , like GNO

How to change rxvt color to white on black?

Nickolai Leschov By default, on Ubuntu, the rxvt terminal is black on white. How can I change it to white on black? I am running Lubuntu 16.04 LTS Terence Note: Updated answer to include desktops that are not loaded .Xresourcesor archived by default , like GNO

Change the text color if the background image is not white?

安德拉斯 · 卡尔帕蒂 (AndrásKárpáti) I've read a lot of articles about this but haven't found any solution to my problem. Is it possible to somehow change the text color based on the background image via Jquery or/and CSS? I have to create a responsive website as per r

Change the text color if the background image is not white?

安德拉斯 · 卡尔帕蒂 (AndrásKárpáti) I've read a lot of articles about this but haven't found any solution to my problem. Is it possible to somehow change the text color based on the background image via Jquery or/and CSS? I have to create a responsive website as per r