<div class="buttons">
<button class="parent">...</button>
<button class="parent">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="parent">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="parent">...</button>
</div>
const itemSelector = '.parent';
const className = 'xxx';
document.querySelectorAll(itemSelector).forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover(e) {
const state = e.type === 'mouseenter';
for (
let el = this;
(el = el.nextElementSibling) && !el.matches(itemSelector);
el.classList.toggle(className, state)
) ;
}
.parent
, чтобы создать видимость, будто бы стили не применялись:.parent:hover ~ .child {
...
}
.parent:hover ~ .parent ~ .child {
...
}
<div class="buttons">
<div class="row">
<button class="parent">...</button>
</div>
<div class="row">
<button class="parent">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
</div>
<div class="row">
<button class="parent">...</button>
<button class="child">...</button>
<button class="child">...</button>
<button class="child">...</button>
</div>
<div class="row">
<button class="parent">...</button>
</div>
</div>