I have been playing with a pure html-css gantt-styled chart. Still a lot to go.
But I can’t find the solution to trigger "hover" on the time-axis vertical divs, which are position absolute, and are below all the contents drawn by each row.
Hovering the rows works as expected and also on their child-bars, but obstructs overlapped elements to be triggered.
I quickly made this shortened example on fiddle
https://jsfiddle.net/diegoj2020/52snopkb/17/
My objective is to highlight the date where the mouse is pointing at.
I am trying to avoid javascript for layouting or styling.
I would like to find the pure-css method if any.
In the fiddle example, the expected behavior is to also highlight the vertical bars when the mouse sits on any row.
Any help would be very appreciated.
/*
The attempt her eis to highlight the row, the column, and the insight bar/object, all of 3 at the same time when the mouse is over.
*/
* {
margin: 0;
padding: 0;
}
xy-plot {
position: absolute;
width: 95%;
border: 1px solid red;
padding: 5px;
margin: 10px;
}
cols {
position: absolute;
display: block;
left: 100px;
right: 10px;
height: 100%;
}
rows {
position: relative;
display: block;
margin-top: 30px;
}
day {
box-sizing: border-box;
display: inline-block;
background-color: #2222;
;
width: calc(100% / 365);
height: 100%;
}
day:nth-child(even) {
background-color: #5552;
}
task {
display: block;
position: relative;
}
taskbar {
display: inline-block;
position: absolute;
left: 250px;
width: 100px;
background-color: red;
height: 100%;
border-radius: 8px;
}
task:hover {
background-color: darkgray;
}
day:hover {
background-color: darkgray;
}
taskbar:hover {
background-color: black;
}
<xy-plot>
<cols>
<script>
// populate columns time-axis
document.write('<day></day>'.repeat(365));
</script>
</cols>
<rows>
<script>
// populate rows and inside elements
document.write('<task>A Task<taskbar></taskbar></task>'.repeat(10));
</script>
</rows>
</xy-plot>
2
Answers
The heart of your question is the ability for a mouse hover to apply to all elements in the z-index at the pointer location.
CSS does not support this. By default,
:hover
is only triggered on the element in front, or with the highest z-index, at the current pointer location.The only control CSS supports is the ability to hide an element from all mouse interaction by styling it with
pointer-events: none
. This effectively makes the element invisible to the mouse pointer, so:hover
will trigger on the next-highest element in the z-order. (Of course, this won’t be of any help in your scenario.)To solve it you will need to use script, because the DOM provides the method
document.elementsFromPoint(x, y)
which returns all elements at the specified coordinates. Set up amousemove
event listener on the parent element which calls this method.To trigger a
:hover
effect on multiple overlapped tags with markers on both the X-axis (rows) and Y-axis (columns), you can use CSS combined with HTML structure and, if necessary, a touch of JavaScript.HTML Structure
Define a grid-like structure with rows and columns. Each cell can have overlapping elements, and you use classes or data attributes to identify them.
CSS Styling
Use the
:hover
pseudo-class to apply styles to cells and their corresponding markers on hover.Advanced Dynamic Marker Triggering with JavaScript
For dynamic interaction when cells overlap or for advanced visual effects, JavaScript can help.
CSS with JavaScript
Add a
highlight
class for the dynamic effects.Explanation: