Not all divs are triggering the hover event as expected

I am facing an issue while creating a webpage with overlapping background and content divs. The hover event works properly on the div with the class "content," but not on the div with the class "background." There is no interference with the event in the JavaScript code.

.container {
  height: 100%;
  width: 100%;
  opacity: 1;
}

.background {
  height: 100%;
  width: 100%;
  position: absolute;
}

.content {
  height: 100%;
  width: 100%;
  position: absolute;
}
<div class="container">
  <div class="background" onmouseover="test(this)">
  </div>
  <div class="content">
    <!-- PANELS -->
    <div class="facePanel" onmouseover="hoveringFacePanel(this)" onmouseleave="hoveringFacePanelOff(this)">
      <img class="imageShown" src="https://i.pinimg.com/736x/70/d4/22/70d422d5972596f603a94c0faf24a43d--advertising-design-advertising-campaign.jpg" />
      <img src="https://apple.insidercdn.com/gallery/21413-24435-Screenshot_1-l.jpg" class="imageShown" />
    </div>
    <!-- PANELS -->
  </div>
</div>

Fiddle: https://jsfiddle.net/6ardv95r/

UPDATE:

The conversation about the bubbling nature of events has been brought up, but it fails to explain why this behavior persists even when the HTML is simplified to this:

<div class="container">
        <div class="background" onmouseover="test(this)">
        </div>
        <div class="content">
        </div>
    </div>

In this scenario, the content class does not trigger any hover event, yet the background class still does not respond to any events. Removing the content class allows the background class to function as expected.

Answer №1

Browser events operate using a concept known as "bubbling," where they start at the most specific element and then move up the hierarchy of elements (from child to parent to grandparent, and so on) until they reach the root element (typically <html>).

In the scenario you described, the mouseover and mouseout events are triggered within the "content" div and bubble up through the DOM tree. However, even though visually the events may appear to be contained within the "background" div, in terms of the DOM tree structure, they are not. This is because the "content" div is positioned after the "background" div, causing it to receive all event triggers while the "background" div receives none. To ensure that the background div also receives these events, place the "content" div inside the "background" div.

For more information on event bubbling, you can visit: https://javascript.info/bubbling-and-capturing

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

The functionality of JQuery fails to work properly when receiving an AJAX response

I have separated my code into three main sections: a PHP file, a jQuery section with an AJAX function that requests data, another PHP file. The response from the AJAX request is HTML that needs to be added at the beginning of a specific DIV inside the i ...

Exploring Nested Data in MongoDB Aggregation using $match and $project

Struggling with crafting a Mongoose Aggregate Query to extract the permissions object of a specific member within a deeply nested structure of business data. MongoDB's documentation on this topic is lacking, making it hard for me to progress. Sample ...

Adjust the HTML content prior to displaying it to prevent any flickering

Is there a way to run code before the page is rendered, such as updating dates or converting text to HTML, without causing flickering when reloading multiple times? How can I ensure the code runs as it's drawn instead of waiting until the end to redra ...

Is it possible to utilize HTML as a platform for interfacing with a C/C++ program?

As I work on a new product with USB interface, I need to create a control app for it. However, my GUI programming skills are limited, so I have thought of using a local web page within the app's installation directory as the interface for the program. ...

Change the order of numbering in ordered lists

I am seeking a way to change the ordering of an ordered list to be in descending order. You can view a live example here. Instead of having the counter span multiple ol elements, I would like the counter to reset after each ol. For the live demo, the des ...

What is the best way to configure my Express API endpoint so that it can generate customized responses based on the parameters in the URL?

Currently working on a react-admin project where I need to establish a connection with an express server using a data provider. Despite my efforts, I have been unable to implement sorting and pagination functionality. It seems like modifications are requi ...

Display issue with React TypeScript select field

I am working with a useState hook that contains an array of strings representing currency symbols such as "USD", "EUR", etc. const [symbols, setSymbols] = useState<string[]>() My goal is to display these currency symbols in a select field. Currently ...

jQuery .load() pause, content loading

Can anyone provide assistance with achieving the desired effect of fading out the #portfolio-wrapper, then loading the portfolio2.html and fading it in? Currently, the load function is executing before the fadeOut operation. Any help would be greatly app ...

Generate a dynamic jqplot chart using data retrieved from a variable and JSON data

I'm struggling with loading data into a jqplot chart via variables as it only displays the first value. I am puzzled as to why this is happening. JavaScript: $(document).ready(function () { var sin = [[20, 10, 0, 10, 15, 25, 35, 50, 48, ...

Increased pixels being incorporated into my line spacing

Looking for a solution: A <div> containing only one word at a font size of 16px with a line-height set to 1. Encountering issues with extra space above the text in Chrome and Firefox, while the text bleeds slightly out of the bottom. IE exacerbates t ...

Examples, templates, and best practices for creating top-notch API documentation

Currently, I am in the process of developing the user interface for a web service, while another organization is handling the back end. I am looking for a clear, simple, and easily comprehensible method of creating a document outlining API calls that will ...

What is the best way to stop "a" style from affecting a hyperlink?

I'm facing a challenge in designing a banner for my website that doesn't involve using an image. This banner also needs to be clickable as it will serve as a link. Is there a way for me to prioritize the CSS styling of my div over the default "a ...

The checkbox appears unchecked, yet the content normally associated with a checked checkbox is still visible

As the title suggests, I am encountering an issue with my code. Here is the snippet: $('#somediv').change(function() { if(this.checked) { $('.leaflet-marker-pane').show(1); } else { $('.leaflet-marker-p ...

The error message "Error: 'x' is not a defined function or its output is not iterable"

While experimenting, I accidentally discovered that the following code snippet causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable I also found out ...

Utilizing webpack to import both d3 and d3-cloud libraries

I've been attempting to integrate d3 and d3-cloud (for word cloud) into my AngularJs(v - 1.4) app by using: import d3 from 'd3' import d3Cloud from 'd3-cloud'. However, when trying to use d3-cloud with d3.layout.cloud(), ...

Steps for adding a forked version of a library in package.json

Recently, I came across a React JS library called React Pacomo. Since the original version of this library is no longer being maintained, I decided to use my own forked version for my project. However, I am facing issues with compiling or building the libr ...

The customized cursor image fails to load after switching the application URL from http to https

After implementing a redirect from http to https in my application, I encountered an issue with custom cursor images not displaying as intended. Prior to the redirect, the custom cursor images worked fine and were visible on the page. The URL for these cur ...

Unveiling the magic: Dynamically displaying or concealing fields in Angular Reactive forms based on conditions

In my current scenario, there are three types of users: 1. Admin with 3 fields: email, firstname, lastname. 2. Employee with 4 fields: email, firstname, lastname, contact. 3. Front Office with 5 fields: email, firstname, lastname, airline details, vendo ...

The alignment of inline-block elements is not being maintained on the same line

Here's a question I have about my embedded form. Why does the display property of inline-block make the "submit" and "terms" elements appear higher than the email field? And more importantly, how can I fix this issue? I've attempted to use the t ...

Is there a CSS rule that can alter the appearance of links once they have been clicked on a different webpage?

Just starting out here. Imagine I have 4 distinct links leading to the same webpage - is there a way to modify the properties of the destination page depending on which link was clicked? For instance, clicking on link1 changes the background color of the d ...