Is there a way to match the color of a fixed element with the background elements' color?

In my current website setup, I have multiple full-width content containers stacked on the Y-axis. Every other container is blue and red alternatively. I am looking to incorporate an SVG image or a button fixed in the top-left corner that moves with the user as they scroll. The fill color of this element should dynamically invert based on the background color of the underlying content containers. When the fixed element hovers over two containers simultaneously, its fill color should be split accordingly.

As the user scrolls, the fixed element (resembling a circle) should adapt its fill color to match the background elements below it. If the circle is fully positioned over a red container, it should turn blue; if over a blue container, it should turn red. When spanning two containers, it should blend its fill color.

The initial idea of using position:fixed for each content container did not work as expected due to the attachment of elements' viewport to the window/body. Position:sticky was tried next, which stopped the element at the container's bottom line but lacked a sharp cut-off effect beyond it.

Exploring the use of z-index revealed limitations in achieving the desired effect because of its one-dimensional nature.

CSS code snippet to adjust the blue circle:

.fixed {
  position: fixed;
  top: 50px;
  left: 50px;
  width: 100px;
  height: 100px;
  border-radius: 50px;
  background-color: blue;
}

.bg {
  height: 200px
}

.bg-red {
  background-color: red;
}

.bg-blue {
  background-color: blue
}
<div>
  <div class="fixed"></div>
  <div class="backgrounds">
    <div class="bg bg-red"></div>
    <div class="bg bg-blue"></div>
    <div class="bg bg-red"></div>
    <div class="bg bg-blue"></div>
  </div>
</div>

I appreciate any guidance or solution suggestions to help achieve the desired outcome.

Answer №1

To achieve the desired effect of color blending with red and blue backgrounds, you can utilize mix-blend-mode difference.

Set the background color of the circle to #ff00ff. This will result in a difference between blue (#0000ff) and red (#ff0000).

.fixed {
  position: fixed;
  top: 50px;
  left: 50px;
  width: 100px;
  height: 100px;
  border-radius: 50px;
  background-color: #ff00ff;
  /*ADDED*/
  mix-blend-mode: difference;
  
}

.bg {
  height: 200px
}

.bg-red {
  background-color: red;
}

.bg-blue {
  background-color: blue
}
<div>
  <div class="fixed"></div>
  <div class="backgrounds">
    <div class="bg bg-red"></div>
    <div class="bg bg-blue"></div>
    <div class="bg bg-red"></div>
    <div class="bg bg-blue"></div>
  </div>
</div>

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

Complete my search input by utilizing ajax

It's only been 30 minutes since my last post, but I feel like I'm making progress with my search posts input: I've developed a model that resembles this: function matchPosts($keyword) { $this->db->get('posts'); ...

Looking for CSS properties to personalize a dropdown list in an HTML select element

After spending several days scouring the internet and trying out numerous methods, I have yet to find a satisfying way to create my own dropdown list. Using CSS, I am able to customize the text color (A), the border style and color (B) of the dropdown fie ...

Is the behavior of a function with synchronous AJAX (XMLHttpRequest) call in JavaScript (Vanilla, without jQuery) the same as asynchronous?

I'm facing an issue with a file named tst.html and its content: part two (purely for demonstration, no extra markup) The challenge arises when I try to load this file using synchronous AJAX (XMLHttpRequest): function someFunc() { var str = &ap ...

What is the best way to declare a variable within a VueJS v-if condition without it being visible?

In my code, I have a specific template that is displayed when a certain condition is met using a v-if statement. In this template, I need to set a variable to N/A, but I am struggling with how to accomplish this simple task. <span v-if="se ...

The selected Google Font Family remains unchanged

After taking a break, I decided to dive back into web development in order to create a simple webpage. My starting point is Bootstrap, and I am constructing my page based on that framework. However, I'm facing an issue with changing the font family of ...

Unable to attach the script to recently added DOM elements

After spending considerable time working on this, I'm still unable to figure it out. You can find the page I am referring to at: The "show more" button at the bottom triggers additional posts to be displayed on the page using the following script: ...

What is the best way to ensure that my program runs nonstop?

Is there a way to have my program continuously run? I want it to start over again after completing a process with a 2-second delay. Check out my code snippet below: $(document).ready(function () { var colorBlocks = [ 'skip', 'yell ...

How can Ajax assist in utilizing a JavaScript variable within a Rails call that is made within a JavaScript function?

I'm facing a challenge where I need to update a Ruby on Rails partial within a view using a JavaScript object-defined variable, but I'm struggling to make it work. Despite my research pointing towards using an Ajax call as the only viable solutio ...

Is it possible for someone to assist me in making my map stay in a fixed position?

How can I make sure that my map stays in a fixed position on the screen even when the user scrolls? Here is the code snippet I am using: #map { float: right; height: 545px; width: 40%; margin-top: 12px; margin-right: 30px; posit ...

What is the best way to group all matched objects from an array based on multiple keys?

const data = [ { amount:10, gameId:7 , consoleId:3, id: 1 }, { amount:5, gameId:18 ,consoleId:3, id: 2 }, { amount:5, gameId:18 ,consoleId:3, id: 3 }, { amount:10, gameId:7 ,consoleId:3, id: 4 ...

Is it possible to install a meteor package for only a specific platform?

Adding a single package called ground:db from https://github.com/GroundMeteor/db to my meteor project is something I'd like to do, but I only want it to be used in the cordova builds. It would be ideal if it didn't clutter up the assets on the we ...

Adjustable div heights for text within inline elements

How can I make two inline-table divs (content and sidebar) have the same height regardless of their content? I've experimented with various attributes, but nothing seems to be working. You can view my website here. ...

What are some ways to ensure that my gltf loader in three.js operates synchronously?

I have a question regarding my code. I'm currently working on making the loadCone() function synchronous using async/await, but I'm facing some issues with it. import * as THREE from "three"; import { GLTFLoader } from "three/exam ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

Starting the stored procedure/function using the sequelize.query() method

Below is the stored procedure I have written: CREATE OR REPLACE FUNCTION GetAllEmployee() RETURNS setof "Employees" AS $BODY$ BEGIN RETURN QUERY select * from "Employees"; END; $BODY$ LANGUAGE plpgsql; I am attempting to execute this stored procedure fro ...

Utilizing the Vuetify pagination feature in your project

I am in need of some guidance regarding the configuration of vuetify pagination. I have a card component that I loop through, but I also want to implement pagination around it. Any insights on where to start would be greatly appreciated? <v-pagination ...

HTML makes column text wrapping automatic

Is it feasible to implement text wrapping into two columns or more in HTML using solely CSS? I have a series of continuous text enclosed within p tags only, structured as follows: <div id="needtowrap"> <p>Lorem ipsum dolor sit amet, ...&l ...

Attempting to render an image onto a canvas and apply Caman.js for image editing purposes

Currently, I have a code snippet that allows me to draw an image onto a canvas. Here is the code: var imageLoader = document.getElementById('imageLoader'); imageLoader.addEventListener('change', handleImage, false); var ...

Can someone help me create Three.js types using the frontend option I choose?

I'm currently developing a user-friendly browser application for editing shaders in three.js using react-three-fiber. I want to enhance the functionality by allowing users to add additional uniforms to the ShaderMaterial. However, I do not want to exp ...

Enhancing Vue Filtered Lists for Increased Dynamism

Currently, I am iterating through a list of items using the v-for directive. Before displaying the list, I apply a filter based on a specific value using the .filter() method. To switch between different filtered values, I utilize the v-on:click event. How ...