I’m trying to add elements to a card while the mouse is hovering over the card (like netflix cards), however when the cursor is moved to where the new button appears the mouseout event is triggered and the button cannot be pressed. Here’s an example:
<div class="cards">
<img class="cardsImg" src="codePic.jpg" alt="">
<h3>Project 1</h3>
<div class="cardsDesc"></div>
</div>
and JS:
document.addEventListener('DOMContentLoaded', function () {
let cards = document.querySelectorAll('.cards');
for (let i = 0; i < cards.length; i++) {
cards[i].addEventListener('mouseover', function () {
let desc = cards[i].querySelector('.cardsDesc')
desc.innerHTML = `<div class="readMore" onmouseover="moveArrow('arrow1')" onmouseout="moveArrowBack('arrow1')">
<a href="About.html" class="readMoreLink">Click here to read more <span id="arrow1">➔</span></a>
</div>`;
})
cards[i].addEventListener('mouseout', function () {
let desc = cards[i].querySelector('.cardsDesc')
desc.innerHTML = ``;
})
}
})
I’m new to JavaScript, not sure what to try so any help is apprciated.
2
Answers
Try using mouseenter and mouseleave events. Working Example https://codepen.io/sethuramancbe/pen/poxoreK
Here is the solution for your problem, you should define it like this to achieve it.
For any queries do let me know.
Thank you.