My goal is to create a hover effect using JavaScript. I am utilizing the mouseenter and mouseout events to display the content when the mouse hovers over a button and remove the content when the mouse moves out. However, I am facing an issue where the content disappears on mouse enter if it contains elements, but it works fine when the content does not contain any elements.
const button = document.querySelector("button"),
dropdown = document.querySelector(".dropdown");
button.addEventListener("mouseenter", (e) => {
dropdown.classList.add("active")
dropdown.addEventListener("mouseenter", (e) => {
dropdown.classList.add("active")
})
button.addEventListener('mouseout', () => {
dropdown.classList.remove("active")
dropdown.addEventListener("mouseout", () => {
dropdown.classList.remove("active")
})
})
})
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: grid;
place-items: center;
height: 100vh;
font-family: sans-serif;
}
button {
padding: 1em;
cursor: pointer;
}
.dropdown {
position: absolute;
width: 100PX;
height: 200px;
background-color: red;
bottom: 52%;
display: none;
}
.dropdown.active {
display: block;
}
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<button>
Hover Me
</button>
<div class="dropdown">
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
<li>content</li>
</div>
</body>
</html>