Как применить класс к разным блокам при наведении?
При наведении на карточку сменяются картинки, проблема в том что при наведении на первую карточку товара, вторая начинает тоже пропадать. Подскажите как применить наведении для каждой карточке отдельно? Спасибо
const galleryItems = document.querySelectorAll('.product-card__item');
if (galleryItems) {
galleryItems.forEach(item => {
item.addEventListener("mouseenter", function (e) {
galleryItems.forEach(el => { el.classList.remove('_active'); });
item.classList.add('_active')
});
item.addEventListener("mouseleave", function (e) {
galleryItems.forEach(el => { el.classList.remove('_active'); });
galleryItems[0].classList.add('_active')
});
});
}
.product__body{
display: flex;
}
.products__column{
flex: 0 1 auto;
margin: 0px 20px 0px 0px;
}
.product-card__images {
display: flex;
position: relative;
height: 363px;
width: 300px;
}
a {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
display: block;
left: 0;
}
img {
transition: opacity 0.2s ease-in-out;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
border: 0;
vertical-align: middle;
max-width: 100%;
max-height: 100%;
pointer-events: none;
opacity: 0;
}
.product-card__item._active img{
opacity: 1;
}
.product-card__items{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: row;
z-index: 2;
}
.product-card__item{
flex-grow: 1;
-webkit-box-flex: 1;
-ms-flex-positive: 1;
background-color: #fff;
background: 0 0;
margin: 0px 2px;
}
<div class="product__body">
<div class="products__column product-card">
<div class="product-card__images">
<a href="#">
<span class="product-card__items">
<span class="product-card__item _active">
<img src="https://kisa.club/uploads/posts/2023-03/1679858194_kisa-club-p-stilnie-komplekti-odezhdi-instagram-4.jpg" alt>
</span>
<span class="product-card__item">
<span class="product-card__item-nav"></span>
<img src="https://kisa.club/uploads/posts/2023-04/1680336969_kisa-club-p-stil-odezhdi-devochki-krasivo-1.jpg" alt>
</span>
</span>
</a>
</div>
</div>
<div class="products__column product-card">
<div class="product-card__images">
<a href="#">
<span class="product-card__items">
<span class="product-card__item _active">
<img src="https://kisa.club/uploads/posts/2023-03/1679858194_kisa-club-p-stilnie-komplekti-odezhdi-instagram-4.jpg" alt>
</span>
</span>
</a>
</div>
</div>
</div>
Источник: Stack Overflow на русском