find the intersection point of two elements

Is it possible to identify the exact point where two elements overlap?

Take a look at the code snippet below that demonstrates what I mean:

<html>
<head>
  <style>
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    .s1, .s2 {
      height: 100%;
    }
    .s1 {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .s2 {
      background-color: aquamarine;
    }
    .s1 p {
      position: fixed;
    }
  </style>
</head>
<body>
  <div class="s1">
    <p class="p">TEST</p>
  </div>
  <div class="s2">

  </div>
</body>
</html>

This code is quite simple with just two <div> elements and one <p> tag.

I applied the style position: fixed to the <p> tag to keep it centered on the screen at all times.

Now, my question arises.

Is there a method to detect when the <p> tag and .s2 element intersect?

If the <p> overlaps with the .s2, I would like to change its color.

https://i.sstatic.net/csvqA.png

(I am referring to this specific intersection point)

Any solutions or suggestions?

I prefer a solution using pure HTML/CSS/JS, but I am open to using libraries if they offer relevant functionality.

Thank you!

Answer №1

To achieve this, utilize Event Listeners.

window.onload = function() {  
  let p = document.querySelector('.s1 p');
  let s2 = document.querySelector('.s2');

  window.addEventListener('scroll', () => {
    let coordP = p.getBoundingClientRect();
    let coordS2 = s2.getBoundingClientRect();

    if(coordP.bottom > coordS2.top) {
      p.style.color = 'yellow';
    }
    else p.style.color = 'black';
  });
}
<html>
<head>
  <style>
    body {
      height: 100vh;
      margin: 0;
      padding: 0;
    }
    .s1, .s2 {
      height: 100vh;
    }
    .s1 {
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid green;
    }
    .s2 {
      background-color: aqua;
      border: 1px solid aqua;
    }
    .s1 p {
      position: fixed;
    }
  </style>
</head>
<body>
  <div class="s1">
    <p class="p">TEST</p>
  </div>
  <div class="s2">

  </div>
</body>
</html>

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

Embellishing Your Website with Unique Cursor Styles

Looking to add personalized cursors to my website using asp.net 4.0. On my master page, I aim to set a custom cursor as the default for the entire site and have different custom cursors for all links. Is this achievable? ...

The current media breakpoints are not performing as expected

Could someone help me unravel this simple issue I seem to be having? My page layout is functioning correctly, but when I apply media breakpoints, nothing changes. Here's an example of my CSS where I am just adjusting the font size in an attempt to mak ...

When attempting to initiate a new session, Webdriver.io receives an HTML response from selenium

Currently, I am attempting to execute my selenium tests using webdriver.io. However, the test runner is encountering failure when attempting to establish a session: [18:12:36] COMMAND POST "/session" [18:12:36] DATA {"desiredCapab ...

Navigate to a new page on button click using Row with Tanstack / React-Table and Typescript (2339)

Encountering a linting error when attempting to navigate to a new route by clicking on a table row. The functionality is working but how can I resolve this issue? It's showing an error message stating "The property "id" for type TData does not exist." ...

Working with nested SQL queries within a function in a Node.js environment

Hello, I am a beginner in JavaScript. I have a function that might require multiple requests to gather the final data. Can someone guide me on the correct approach to handle this? The issue I am facing is that participants are not being added to the dial ...

Retrieve data from multiple JSON tables using a JavaScript loop

Check out this Codepen for a working example. I am relatively new to Javascript, so I may not be approaching this problem the best way. If you have any suggestions on how I could improve my approach, I would greatly appreciate it; always looking to learn ...

How to insert text into a text input using onKeyPress in react.js

I am looking to simulate a typing effect where the phrase 'Surveillance Capitalism' is inputted letter by letter into a text input field as a user types. However, I encounter an issue when trying to add each individual letter into the input; I re ...

Comparing document.getElementById and the jQuery $() function

Is this: var contents = document.getElementById('contents'); The equivalent to this: var contents = $('#contents'); Considering that jQuery is present? ...

The alert box in Javascript appears before the execution of the statement preceding it

I am encountering a peculiar issue, which is not unexpected considering my limited experience with JavaScript. I am currently working on developing a basic high-low card game where two cards are drawn and the highest card wins. Below you can find the code ...

Logged-in users experiencing issues with their bookings not showing up

It's me again! I am still struggling to display user bookings when the user is logged in. I feel like I might be missing something really basic here, but I just can't figure it out. Below is the code I've been working on. Any fresh eyes or ...

Utilizing PHP, Javascript, and jQuery in mobile technology gadgets

Can anyone recommend mobile technology products that have been developed using PHP, Javascript, and jQuery? What are the newest mobile products available on the market that have been built using these languages? Do popular devices like iPhone, BlackBerry ...

Stop transparent icons from being covered by the background color

My current setup includes an icon within a button, where I want the icon to be transparent and the button background color to be white. However, the white background color is obscuring the transparency of the icon. Is there a workaround for this issue? Be ...

Float Property for Aligning DIVs

Is there a way to properly align divs like the ones shown below? I tried setting a float left for both div1 and div2, but div3 ended up floating to the top right. How can I avoid this issue? <div1> <div2> How hot is it? ...

Utilizing Dojo widgets in Xpages

I'm having trouble applying CSS to certain dojo elements within my Xpage. Here is the CSS style sheet I am using: .formFields { color: rgb(5, 56, 107); font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: nor ...

Strategies for removing duplicate vertices in geometry through Three.js updates

Currently, I am facing an issue with changing vertex positions in my mesh using three js. It appears that three js stores duplicates for each vertex in the mesh. In my specific scenario, I have 4 duplicates of a vertex, and when I modify one using mesh.g ...

I am struggling to decide which attribute to use for implementing image swap on mouseover() and mouseout()

I have a problem using jQuery to switch between images when hovering on and off. Here's the code snippet I'm working with: HTML <img class="commentImg" src="images/comments.png" data-swap="images/comment_hover.png" alt=""> jQuery $(" ...

CSS styling not appearing on HTML button

I'm attempting to create a login page similar to Instagram using HTML and CSS. However, I'm having trouble styling the button to match Instagram's login page. The button <input type="submit" class="sub-btn"> The CS ...

Enhance the appearance of a class within a shadow DOM using Angular

Displayed by the browser: https://i.sstatic.net/itcrx.png The super-tabs element is a resource. I aim to apply some style to the class .buttons-container from the main component. However, my attempt doesn't yield the desired result: @media (min-widt ...

Avoiding special characters in URLs

Is there a way to properly escape the & (ampersand) in a URL using jQuery? I have attempted the following methods: .replace("/&/g", "&amp;") .replace("/&/g", "%26") .replace("/&/g", "\&") Unfortunately, none of these are y ...

Is it possible to save round items within a session?

While utilizing Blocktrail's API for bitcoin wallet management, I encountered an issue with circular references within the returned wallet object. The goal is to store the decrypted wallet in the user's session to avoid password re-entry. Howeve ...