Ways to adjust text hue in areas with backgrounds of white or black

Is it possible to dynamically change the color of a menu based on the background color? For example, if the menu is scrolling over a black background, the menu text should be white; and 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; }

Answer №1

Learn how to implement Jquery with this practical example.

  • Utilize variables to save the scroll position, and the position of elements offset from the top where you want the color change, such as element1 and element2 in this instance.
  • Compare the scroll's top offset against the elements' top position while scrolling through the document using $(document).scroll(function()).
  • Apply the correct CSS color or styles to the elements based on the 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>

Updated:

$(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>

Answer №2

Include the following CSS in your HTML file:

.navbar li:hover {
  background-color: #ffffff;
}

.navbar li {
  background-color: #000000;
}

.navbar li > a {
  color: #ffffff;
}

.navbar li:hover > a {
  color: #000000;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some ways to utilize symbol fonts such as marvosym within a web browser?

Are you looking to use special LaTeX fonts like \Pluto from marvosym in your web development projects? Wondering how to display this symbol with HTML / JavaScript / CSS without relying on an image? You can download the font from This symbol corresp ...

Tips for migrating an AngularJS application to Angular

My current project involves implementing a basic search function using AngularJS (link). I want to integrate this feature into an existing Angular application. To do this, I created a new Angular app and transferred the view to app.component.html. <hea ...

Having trouble with a basic jQuery selector not functioning as expected

Having trouble parsing this HTML code: <tr id="a"> <td class="classA"> <span class="classB">Toronto</span> </td> <td class="classC"> <span class="classD">Winnipeg</span> </ ...

Utilizing optional parameters with React Router

Imagine I have a page at http://www.example.com/page/#/search set up with the following routing: <Router history={hashHistory}> <Route path='/search/' component={SearchPage} /> </Router> When a user performs a search using t ...

Vertically Center Image in a Div

I have been struggling to center an image within a div, but all the usual methods like using margin: auto; do not seem to work. I have tried various popular techniques with no success. <style> /* Add a black background color to the top navigation ...

`Can controllers be included in route or template definitions?`

When it comes to defining a route with a template, there are two main ways to set the controller for the view: In the route: $routeProvider .when('/phone/:phoneId', { controller: 'PhoneDetailController', templateUrl: &ap ...

When using the each() function, the array shows a length of zero and does not

Although I am well-versed in PHP, my understanding of JQuery is lacking and I'm experiencing some difficulty with arrays. Despite researching numerous forum posts on the topic, I still can't seem to get it right. I believe I have an array struct ...

Additional padding was included for the jQuery logo

I've been working on fine-tuning my jQuery submenu, but I'm running into some CSS challenges. Here are the issues I'm facing: How can I prevent the navigation from adding extra padding at the bottom each time it is reopened? .sidebar-n ...

Warning: The function is missing a dependency in the React Hook useEffect

Currently, I have a React component that utilizes the useEffect() hook: const [stateItem, setStateItem] = useState(0); useEffect(() => { if (condition) { myFunction(); } }, [stateItem]); const myFunction = () => { return 'hello&apos ...

The List<type> parameter within an ASP MVC controller action is coming back as null instead of an empty list

When using ASP MVC 3 with jQuery.ajax to post array values with traditional:true, everything works perfectly fine as long as the array contains values. However, I encounter an issue when the array is empty in the JavaScript – the value passed into the re ...

Looking to disable the back button in the browser using next.js?

How can I prevent the browser's back button from working in next.js? // not blocked... Router.onRouteChangeStart = (url) => { return false; }; Does anyone know of a way to disable the browser's back button in next.js? ...

When transferring files to the src/pages directory in Next.js, the custom _app and _document components are not recognized

The documentation for Next.js mentions that the src/pages directory can be used as an alternative to /pages. However, I encountered a problem when moving my custom _app.tsx and _document.tsx files into the src folder, as they were being ignored. If you cr ...

Ways to display a different div when clicking on a div?

Good afternoon, I've noticed that this question has been asked numerous times before, but none of the solutions provided seem to work for my specific issue. My problem involves a div with the class .title3. I want another div with the class .Content ...

What is the standard root directory in the "require" function's context?

After spending hours struggling to set up a simple "require" command, I've come to the conclusion that var example = require("example") only works if there's an example.js file in the node_modules directory of the project. I'm encountering ...

Angular input range slider that automatically rounds decimal values from the data bindings

I've implemented a range slider within an Angular form to capture and display recorded values. <input [formControlName]="object.id" [id]="object.id" type="range" [(ngModel)]="object.answer" [step]="objec ...

Issues arising when using December in a JavaScript function to determine weekends

My function is used to determine if a day falls on the weekend or not, and it works perfectly for most months except December. What could be causing this issue? weekEnd: function(date) { // Determine if it's a weekend var date1 = new Dat ...

How can we include identical item names within a single JavaScript object?

My attempt at achieving the desired result involves creating an object like this: var obj = {id: id, items: "asdf", items: "sdff", test: varTest}; However, I face a challenge where I need to dynamically add two elements with the same name 'items&apo ...

What could be the reason for Angular to merge the element at index 0 of an array into a subarray instead of doing

After setting up the Array in my oninit function, I encountered an issue where one part of the array was functioning as intended while the other returned an error. this.tests = [{ status: 0, testresults: [{ name: 'test ...

Leveraging the MEAN Stack to Build Modern Web Applications

My web application development journey has primarily involved working with JQuery on HTML5. As the code has grown to over 1300 lines and become increasingly complex, I've realized it's all on the front end. The code consists of two main component ...

Utilizing previous ajax call responses for jQuery event handling

In this task, the goal is to retrieve data from the Pokemon API and add it to a list. The API request results contain links to previous and next pages. Here is the HTML structure: <ul class="poke-list"></ul> <div class="pagination"> ...