I have a flyout menu which I am trying to toggle using the mouseover event, however it doesn't seem to work as intended. I tried using a hover event with CSS by targeting the div like this: .hero-list-block a:hover .flyout-menu
However this didn't work. Any suggestions on how I can fix this/improve this? Thanks
const flyoutLink = document.querySelector('.flyout-link');
const flyoutMenu = document.querySelector('.flyout-menu');
flyoutLink.addEventListener('mouseover', () => {
flyoutMenu.classList.toggle('.flyout-menu-show');
})
.grid-hero-wrapper {
grid-template-columns: 100px 1fr;
gap: 15px;
margin-top: 15px;
display: grid;
}
.hero-categories-block {
background: #fff;
border: 1px solid #28282b;
}
.hero-categories-list {
display: flex;
flex-direction: column;
padding: 5px 0;
height: 100%;
}
.flyout-menu-show {
opacity: 1;
visibility: visible;
}
.flyout-menu {
background: #fff;
border: 1px solid #28282b;
position: absolute;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
visibility: hidden;
position: absolute;
z-index: 999;
}
.hero-categories-list a {
display: flex;
align-items: center;
flex-grow: 1;
font-size: 0.75rem;
height: 22px;
color: #333;
}
.hero-slider-block {
position: relative;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<div >
<div >
<div >
<a href="#" >Flyout</a>
<a href="#" >Flyout</a>
</div>
</div>
<div >
<div id="carouselExampleSlidesOnly" data-bs-ride="carousel">
<div >
<div >
<img src="https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1173&q=80" >
</div>
</div>
</div>
<div >
<h1>This is my flyout menu</h1>
</div>
</div>
</div>
CodePudding user response:
This is not working because of the specificity of your css selectors.
.flyout-menu-show selector is declared before .flyout-menu selector and they got the same specificity. So only the latest applies to your element.
change
.flyout-menu-show {
opacity: 1;
visibility: visible;
}
to
.flyout-menu.flyout-menu-show {
opacity: 1;
visibility: visible;
}
This will create a selector with higher specificity, and it will be applied properly :). Working codepen of your sample: https://codepen.io/aSH-uncover/pen/PoaboWo
probably worth reading this page too : https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity