Can one look through a div to the one beneath it by moving the cursor?

Hey there! I have a unique question for you. I've been trying to achieve a specific effect where two divs are stacked on top of each other, and the content of the lower div is only revealed in a certain area around the cursor. Is it possible to make only part of a div transparent to achieve this effect? Or do you have any other suggestions on how to make this happen?

https://i.sstatic.net/WFOuR.jpg

Answer №1

Instead of placing the desired element in the background, consider positioning it in front and revealing only a portion using a clip-path;

When specifying coordinates, you can utilize CSS variables. Alternatively, you have the option to directly modify the style.

// Access DOM element
const container = document.querySelector('.container');

// Implement event listener
container.addEventListener('mousemove', updateCoords, false);

function updateCoords(event) {
  // Capture X and Y coordinates
  const { offsetX, offsetY } = event;
  
  // Update coordinates
  container.style.setProperty('--x', offsetX + 'px');
  container.style.setProperty('--y', offsetY + 'px');
}

.container {
  border: 1px solid #000;
  width: 300px;
  height: 300px;
}

/* Display child element upon hovering over the container */
.container:hover .child {
  display: block;
}

.child {
  clip-path: ellipse(30px 30px at var(--x) var(--y));
  display: none;
}

To achieve smoother movement of the circle, consider utilizing requestAnimationFrame.

// Obtain DOM element
const container = document.querySelector('.container');

// Implement event listener
container.addEventListener('mousemove', updateCoords, false);

function updateCoords(event) {
  // Capture X and Y coordinates
  const { offsetX, offsetY } = event;
  
  // Update coordinates
  requestAnimationFrame(() => {
    container.style.setProperty('--x', offsetX + 'px');
    container.style.setProperty('--y', offsetY + 'px');
  });
}

.container {
  border: 1px solid #000;
  width: 300px;
  height: 300px;
}

/* Show child element when container is hovered over */
.container:hover .child {
  display: block;
}

.child {
  clip-path: ellipse(30px 30px at var(--x) var(--y));
  display: none;
}

An example featuring text:

// Access DOM element
const container = document.querySelector('.container');

// Implement event listener
container.addEventListener('mousemove', updateCoords, false);

function updateCoords(event) {
  // Capture X and Y coordinates
  const {offsetX, offsetY} = event;

  // Update coordinates
  requestAnimationFrame(() => {
    container.style.setProperty('--x', offsetX + 'px');
    container.style.setProperty('--y', offsetY + 'px');
  });
}

.container {
  min-height: 100vh;
  min-width: 100vh;
  overflow: hidden;
}

.container:hover .code {
  display: flex;
}

.display,
.code {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  background-color: rgb(49, 49, 49);
  color: rgb(240, 191, 29);
  pointer-events: none;
}

.code {
  clip-path: ellipse(100px 100px at var(--x) var(--y));
  display: none;
  background-color: rgb(3, 3, 3);
  color: rgb(101, 253, 101);
}
This showcases how to reveal content with a virtual circle effect:

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

My Angular7 app.component.html file is not displaying the routing. What could be the issue?

After implementing the code in app.component.html in Angular 7 like this: <div id="wrapper"> <header id="header-container" class="fullwidth"> <div id="header"> <div class="container"> <div class="left- ...

callback triggering state change

This particular member function is responsible for populating a folder_structure object with fabricated data asynchronously: fake(folders_: number, progress_callback_: (progress_: number) => void = (progress_: number) => null): Promise<boolean ...

Ways to transfer a value between two different card elements

I have designed a component with three div cards side by side using bootstrap. The first card displays a list of products, and my intention is that when a product is clicked, the details should appear in the second card on the same page. However, this fun ...

Tips for using nested flexbox with images

I am facing a straightforward use case where I have an image and a div containing text. The current setting for the div is flex column. My goal is to change the flex setting to row so that the text and image appear side by side. Despite having the corre ...

Trouble with Alignment in Bootstrap 3 Form

I'm currently facing an issue with aligning a form group in Bootstrap 3. My goal is to have them aligned vertically, but I am struggling to achieve this. Any help would be greatly appreciated! Please refer to the screenshot below for reference: Her ...

What is the method for achieving the equivalent effect of "background-size: cover" on an <img>?

I am looking to ensure that an <img> has the smallest possible size without any empty spaces inside a div, while also being perfectly centered both horizontally and vertically. The image size may vary. To better illustrate, here is an example: http: ...

Add the content of a JavaScript variable to a label without using any scripting

Is there a way to concatenate the value of a JavaScript variable with a label without utilizing jQuery? For example: Var global_message = "1234"; <label id="my-label">Test</label> I desire the concatenated value "Test1234" to be displayed o ...

Responsive design causing images to be invisible in Internet Explorer

Hey there! I have a live website (check it out here, although it's currently behind a "coming soon" cover). The site has two stylesheets: one for regular styles (style.css) and one specifically for responsive design (width.css). The responsive stylesh ...

identifying HTTP requests originating from an embedded iframe

I have a unique scenario where there is an iframe on my page that calls a URL from a different domain. Occasionally, this URL will tunnel to a different URL than the one I specified. Unfortunately, due to cross-domain restrictions, I am unable to view the ...

Creating stunning light effects with camera flash using three.js

I'm working on a website using the amazing three.js library. My current challenge is figuring out how to incorporate a camera flash effect into three.js. Currently, I have a rotating cube in my scene and I would like to have a camera flash occur after ...

The art of revealing and concealing code blocks in AngularJS

I am currently working on a task in my AngularJS project. The requirement is that when both point directives within the md-autocomplete code are filled, the results directive should be displayed immediately without the need for any button. Additionally, if ...

Attempting to abbreviate repetitive Javascript instructions

I have this functional javascript code, but I feel like there might be a more efficient way to write it. Any suggestions on simplifying this? let searchTerm = 'Some search text'; const isMatch = entry.title.toLowerCase().includes(searchTer ...

The overflow:hidden feature is ineffective in Firefox, yet functions properly in both IE and Chrome browsers

I'm in the process of creating a webshop and facing an issue with my sidebar due to varying product counts on different pages. For now, I have implemented the following workaround: padding-bottom: 5000px; margin-bottom: -5000px; overflow: ...

Creating dynamic routes in react-router-dom using conditions

I'm currently developing an application using react-router-dom for navigation. I've encapsulated all my <Routes> inside a container provided by Material UI. However, I want my home page to be outside of this container so that I can display ...

Steps for implementing a reset button in a JavaScript slot machine game

I need assistance with implementing a reset button for my slot machine game prototype written in JS. I attempted to create a playAgain function to restart the game by calling the main game function, but it's not working as expected. Do I need to pass ...

Failing to upload a file along with other form elements in Flask results in a 400 error

Encountering a 400 error when attempting to upload a file and send other form elements to Flask from HTML. Tried using AJAX, but it also gives me an error. Python: @app.route('/prod_diff_result', methods=['POST']) def prod_diff_result ...

Need help restarting a timer I've built in Angular with just a click?

I am in the process of developing a compact application that will help me keep track of my activities within a specific time frame. Once I input an activity into a field and click "OK," it should be added to a list. My current challenge lies in resetting ...

Scan for every header tag present and verify the existence of an id attribute within each tag. If the id attribute is absent, insert

Looking to locate all header tags within the content and verify if each tag has an id attribute. If not, then jQuery should be used to add the id attribute. Here is the code snippet: var headings = $("#edited_content").find("h1,h2,h3,h4,h5,h6"); $.each( ...

Maximizing Bootstrap's Potential: Achieving Right Alignment for Divs

I am facing a challenge with aligning my div in the layout_cshtml. Currently, it is aligned to the left and the search bar and dropdownlist are not displaying properly. This negatively impacts the user experience. Additionally, my navigation menu is not ...

While reviewing my HTML code, I couldn't locate a particular line of code responsible for changing the display of my webpage. However, upon inspecting the element, I discovered that

<h2><a class="label label-info" target="_blank" href="http://www.avis.com.pk/"> Avis</h2> </a> <h4> <span class="label label-primary"> Rent A Car Service</span></h4> There is an Inspect Element image attach ...