const card = document.querySelectorAll('.process__card');
card.forEach((cardList) => {
cardList.addEventListener('mouseover', animationBackround);
cardList.addEventListener('mouseout', animationBackround);
})
function animationBackround(event) {
const eventType = event.type;
const index = Array.from(card).indexOf(event.target);
if (eventType === 'mouseover') {
let color1 = getRandomColor();
let color2 = getRandomColor();
card[index].style.backgroundColor = `linear-gradient(45deg, ${color1}, ${color2})`
}
if (eventType === 'mouseout') {
card[index].style.backgroundColor = 'none';
}
}
function getRandomColor() {
const letters = '0123456ABCDEF';
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
.process {
padding-top: 140px;
&__wrapper {
display: flex;
margin: 0 auto;
flex-direction: column;
gap: 30px;
width: 1234px;
}
&__card {
display: flex;
flex-direction: column;
align-items: center;
background-color: #F3F3F3;
height: 159px;
border-radius: 45px;
border: 1px solid #191A23;
box-shadow: 0 5px 0 0 #191A23;
&_active {
height: 279px;
background-color: #B9FF66;
}
}
&__content {
display: flex;
padding-top: 41px;
justify-content: space-between;
align-items: center;
width: 1117px;
}
&__lable {
display: flex;
align-items: center;
font-size: 60px;
font-weight: 500;
color: #000;
span {
padding-left: 25px;
;
font-size: 30px;
font-weight: 500;
color: #000;
}
}
&__plus {
display: flex;
position: relative;
flex-wrap: wrap;
align-items: center;
justify-content: center;
width: 58px;
height: 58px;
background-color: #F3F3F3;
border-radius: 100%;
border: 1px solid #191A23;
cursor: pointer;
&:after {
content: '';
display: block;
position: absolute;
width: 25px;
height: 5px;
background-color: #000;
}
&:before {
content: '';
display: block;
position: absolute;
width: 5px;
height: 25px;
background-color: #000;
}
&_active {
&:before {
content: '';
display: block;
position: absolute;
width: 5px;
height: 25px;
background-color: #000;
transform: rotate(90deg);
transition: transform 0.3s ease-in-out;
}
}
&:not(:active) {
&:before {
transition: transform 0.3s ease-in-out;
}
}
&:hover {
background-color: #B9FF66;
transition: 0.7s all;
}
}
&__subcontent {
display: flex;
flex-direction: column;
width: 1117px;
hr {
width: 1114px;
background-color: #000;
height: 1px;
border: none;
}
&_hidden {
display: none;
}
&_active {
display: block;
transition: transform 0.3s ease-in-out;
}
}
&__descrp {
padding: 30px 0 41px 0;
font-size: 18px;
font-weight: 400;
color: #000;
}
}
<div class="process__card">
<div class="process__content">
<div class="process__lable">01<span>Consultation</span>
</div>
<div class="process__plus"></div>
</div>
<div class="process__subcontent process__subcontent_hidden">
<hr>
<div class="process__descrp">During the initial consultation, we will discuss your business goals and objectives, target audience, and current marketing efforts. This will allow us to understand your needs and tailor our services to best fit your requirements.</div>
</div>
I believe that the problem lies in the line with the code:
const index = Array.from(card).indexOf(event.target);
Perhaps the Array.from()
array method is not appropriate here..
I did a check and the card elements that come into the array have a negative index (index = -1) when I move the mouse away from the element
3
Answers
You already have the target node from
event.target
. So just save that to a local variable in your event handler function and use that directly to update the style.Try to
console.log(index)
inside the animationBackground. I would assume it will be-1
because the event target is one of the inner elements, not the parent.process__card
.You can use
event.currentTarget
as outlined in the MDN DocsDelegate and simplify
You also have two typos.