"Explore each row within the nested grid by hovering over them from the main parent

I want to implement a hover effect on each row within a nested grid that is displayed by columns instead of rows.

The layout appears as follows:

  • Grid
    • Column
      • The column displays a grid with only rows

Check out this jsfiddle for the structure. I have managed to apply the hover effect on nested rows only. Although I am aware that I could rearrange the div structure, it is not feasible due to multiple dependencies on columns such as resizing and drag-and-drop functionality, which would compromise performance if rendered in row format – something I definitely want to avoid.

UPDATE

Essentially, when hovering over a div element that is a child of the grid-wrapper__nested, I aim to trigger the hover effect on every div within the same row in every grid-wrapper__nested.

For a visual aid, refer to this illustration: https://i.sstatic.net/FUsnA.png

Answer №1

To achieve the selection of the nth-child in each column, you will need to utilize JavaScript with the current structure in place. It is important to note that a 'hover' class must be used instead of a ':hover' state. Below is an example of how this can be accomplished using pure JS:

let table = document.querySelectorAll('.grid-wrapper')[0];
let columns = table.children;
let cells = document.querySelectorAll('.grid-wrapper__nested__row');

// Iterate through each cell and attach event handlers
for(let element of cells) {
  // Add mouseenter event handler
  element.addEventListener('mouseenter', function() {
    // Obtain index of the cell
    let index = Array.prototype.indexOf.call(this.parentNode.children, this);

    // Loop through each column and add the 'hover' class
    for(let column of columns) {
      // Add 'hover' class to nth-child
      column.children[index].classList.add('hover')
    }
  })

  // Add mouseout event handler
  element.addEventListener('mouseleave', function() {
    for(let column of columns) {
      let cells = column.children;
      // Iterate through each cell and remove the 'hover' class
      for(let cell of cells) {
        cell.classList.remove('hover');
      }
    }
  })
}

Check out the fiddle

If jQuery or similar libraries are being used, the task becomes simpler as functions like on('hover', func) can be utilized alongside calls to index().

Answer №2

To enhance the appearance of your squares, you must adjust the method of how they are represented. Instead of listing them as columns, aim for a layout that showcases them in rows. Once this modification is made, introduce the following code snippet within your nested section:

&:hover &__row{
   background-color: yellow;
}

For further reference, visit https://jsfiddle.net/Lzsjhc8k/17/

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

Is the Angular Library tslib peer dependency necessary for library publication?

I have developed a library that does not directly import anything from tslib. Check out the library here Do we really need to maintain this peer dependency? If not, how can we remove it when generating the library build? I have also posted this question ...

Keeping firebase auth while removing firestore from Vue project

I have recently made the switch from Firestore to MongoDB, and I am currently in the process of removing references to Firestore in my App.vue file. However, I am still utilizing Firebase authentication. Upon checking the console error message, I came acr ...

Having trouble with data retrieval from MySQL using PHP and Angular on certain mobile devices, although it functions properly on desktops and laptops

The data retrieved from the mysql database is functioning properly on desktop and laptop, but not on mobile devices. The following HTML code is present in the html file: <table class="table table-default"> <thead> <tr> & ...

Adding a stylesheet dynamically in Angular 2: A step-by-step guide

Is there a method to dynamically add a stylesheet URL or <style></style> in Angular2? For instance, if the variable isModalOpened is set to true, I want to apply certain CSS styles to elements outside of my root component, such as the body or ...

Get the $_POST data from two different pages

I am experiencing an issue with saving and retrieving form data across multiple pages. The $_POST method only seems to work on the initial "action" page, but not on subsequent pages. Here is the code snippet I am using: HTML (form): <html> & ...

Is there a way to modify the height and width of a div layer?

How can I adjust the height and width of the layer on the card? Also, I want only the close button to be clickable and everything else to be unclickable. Can anyone help me find a solution? <link rel="stylesheet" href="https://stackpath.bootstrapcdn. ...

Do we need to have the 'input type file' field as

I am currently working on a PHP form that includes mandatory fields for saving the data. For example: <td><input class="text_area required" type="text" name="new field" This form also has Javascript code with file upload functionality, which I ...

Receiving a NaN output rather than a numerical value

Experiencing what seems to be a straightforward syntax error, but I'm unable to pinpoint it. Controller: $scope.startCounter = 3; $scope.startTimeouter = function (number) { $scope.startCounter = number - 1; mytimeouter = $t ...

Attempting to simulate a camera shutter effect using div elements

I've been attempting to create a circular camera shutter, but I'm facing difficulties in making it look accurate. This is the desired outcome: https://i.sstatic.net/oS7gT.jpg The first 'petal' should be positioned lower than the last ...

Ways to simultaneously apply fade in and fade out effects using CSS

body { background-image: url("background.jpg"); background-attachment: fixed; font-family: 'Roboto', sans-serif; color: #333333; } #container { height: 1000px; } /* HEADER WITH NAVIGATION BAR AND LOGIN OPTION */ #head { position: abso ...

Change from arbitrary sequence to structured array arrangement

Is there a way to rearrange the items in an array according to their position? Here's the code I have: setColor(colors[Math.floor(Math.random() * colors.length)]); Instead of choosing randomly, I would prefer to sort the items based on their array p ...

A TypeError is thrown when attempting to use window[functionName] as a function

I came across this page discussing how to call a function from a string. The page suggests using window[functionName](params) for better performance, and provides an example: var strFun = "someFunction"; var strParam = "this is the parameter"; //Creating ...

Top strategy for incorporating/displaying extra form fields

Seeking advice on improving user interface design. I want to develop a form that enables users to add more fields by clicking on a "+" or "add" button. For instance, the user can fill out a text field labeled as "Description," followed by another field n ...

Unlocking JSON data with identical keys using JavaScriptLearn how to access JSON data with

I need help converting my JSON training data that includes classes and sentences into a different format. [ { class: 'Reservation', sentence: 'make reservation' }, { class: 'Reservation', sentence: 'do reservation&a ...

Incorporating the Facebook Like button onto your fan page

My Facebook page is not allowing me to insert the Facebook Like button. I attempted to use an iframe code, which worked on a basic website, but it does not work on the Facebook page. Why is this happening? <iframe src="http://www.facebook.com/widgets ...

Establishing a connection to an API endpoint directly from the files stored

I have been experimenting with accessing website APIs from my local drive in order to interact with their data. Following the JSON documentation on MDN (https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON) proved successful as I was able ...

Why is it necessary to decode JSON stringified objects in Vue props?

When passing a stringifyed object via props to a component, it seems like there is an issue with the data transformation. <my-component :filter="stringobject" ></my-component> stringobject = "{"search_text":"(ciCl ...

The AppJS warning: A potentially dangerous JavaScript attempt to access a frame with a URL

I am currently working on a desktop application where I am trying to filter specific divs inside an iframe using AppJS. However, I am facing an issue with hiding some of the div elements. Here is the code snippet: <!DOCTYPE html> <html> <s ...

The width of the <ul> element does not properly accommodate the width of the <li> elements during the animation that updates the width of the <li>

I am currently working on implementing a typing animation in React. The li element has an animation that recursively changes its width from 0px to 100%. However, I have noticed that when the width of the li changes, the parent ul does not adjust its width ...

When it comes to for loops, one important aspect to consider is the value of each

Looking to include the "selected" attribute in the select field of my Django project: <form id="formselect" method="post"> {% csrf_token %} <select name="position_select" id="position_select"> <option value="0">all positions& ...