I have a JavaScript file that updates the index based on the cursor's position over items in a menu. Here is the code I used:
const menu = document.getElementById("menu");
Array.from(document.getElementsByClassName("menu-item"))
.forEach((item, index) => {
item.onmouseover = () => {
menu.dataset.activeIndex = index;
}
});
The index update is supposed to change the background image using CSS.
#menu[data-active-index="0"] > #menu-background-pattern {
background-position: 0% -25%;
}
#menu[data-active-index="1"] > #menu-background-pattern {
background-position: 0% -50%;
}
#menu[data-active-index="2"] > #menu-background-pattern {
background-position: 0% -75%;
}
#menu[data-active-index="3"] > #menu-background-pattern {
background-position: 0% -100%;
}
The background image is not moving even though it should have moved. Here is the HTML:
<div id="menu">
<div id="menu-items">
<div class="menu-item">Home</div>
<div class="menu-item">Shop</div>
<div class="menu-item">About</div>
<div class="menu-item">Contact Us</div>
</div>
<div id="menu-background-pattern"></div>
<div id="menu-background-image"></div>
</div>
<script></script>
<link rel="stylesheet" href="main.css">