You don't really need to rely on JS to assign incremental z-index. Instead, we can just increase the z-index of the element you have hovered, so it overlaps the rest.
My trick is to rely on jQuery to duplicate the content of each .article
element, which is hidden by default and only shown when hovered over:
$(function() {
$('.article').each(function() {
// Create duplicate
var $dup = $('<div />', {
'class': 'duplicate'
}).html($(this).html());
// Append duplicate
$dup.appendTo($(this));
});
});
And here is the updated CSS:
.article{
width:200px;
height:200px;
background-color: blue;
margin: 20px;
float: left;
display: inline-block;
/* We set their positions to relative so that z-index works */
position: relative;
z-index: 1;
}
.article:hover {
/* Increase z-index when hovered on, ensures overflowing content overlaps neighbouring/adjacent divs */
z-index: 2;
}
.article > .duplicate {
background-color: red;
display: none;
position: absolute;
}
.article:hover > .duplicate {
display: block;
height: 400px;
top: 0;
left: 0;
right: 0;
}