I am using a bootstrap 4 navbar with dropdowns that open with CSS animation/fade-in on desktop, and a separate toggle button for mobile. Everything works fine, but when I open the dropdowns on mobile and then resize the window screen, they remain open which doesn't work well with the animation. Is there a way to automatically close the dropdowns when the window is resized or reaches the desktop breakpoint?
HTML:
<ul class="navbar-nav">
<li class="nav-item dropdown">
<div class="nav-title-flex-container">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href=“home.html"
id="navbar-drop-downs"
aria-haspopup="true" aria-expanded="false">
<span class="underline"> Home</span>
</a>
<span class="openNav"><i class="fa fa-angle-right"></i></span>
</div>
<div class="dropdown-menu dropdown-menu-nav-bottom"
aria-labelledby="navbar-drop-downs">
<a class="dropdown-item" href=“#”>Sub Page</a>
<a class="dropdown-item" href=“#”>Sub page</a>
</div>
</li>
<li class="nav-item dropdown">
<div class="nav-title-flex-container">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="how-to-be-vocal.html"
id="navbar-drop-downs"
aria-haspopup="true" aria-expanded="false">
<span class="underline”>About </span>
</a>
<span class="openNav"><i class="fa fa-angle-right"></i></span>
</div>
<div class="dropdown-menu dropdown-menu-nav-bottom"
aria-labelledby="navbar-drop-downs">
<a class="dropdown-item" href=“#”>Sub Page</a>
</div>
</li>
</ul>
CSS:
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@media (min-width: 1199px) {
.dropdown:hover > .dropdown-menu {
display: block;
z-index: 100;
position: absolute;
animation: fadein .7s;
}
}
JQuery: (toggling dropdowns/mobile)
$openNavToggle.on('click', function () {
$(this).parent().next('.dropdown-menu-nav-bottom').toggleClass('show');
$(this).find('i').toggleClass('fa-angle-right--active');
});