After building a Bootstrap menu with a lightbox effect that overlays the page content when expanded, I encountered some issues.
I attempted to use JavaScript functions to hide and display the lightbox_container
element when toggling the nav. However, adding the second function to toggle the nav off when clicking outside of it on the lightbox_container
resulted in unexpected behavior, as it would set the dropdown to display:none
, preventing it from expanding again.
$("#navbarSupportedContent").on('show.bs.collapse hide.bs.collapse', function(e) {
var lightbox = document.getElementById("lightbox_container");
if (e.type == 'hide') {
lightbox.style.display = "none";
} else if (e.type == 'show') {
lightbox.style.display = "block";
}
});
$("#lightbox_container").on("click", function() {
if ($("#navbarSupportedContent").hasClass("show")) {
$("#navbarSupportedContent").hide();
}
var lightbox = document.getElementById("lightbox_container");
lightbox.style.display = "none";
});
#lightbox_container {
position: fixed;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 40;
display: none;
top: 0px;
left: 0px;
}
nav {
z-index: 50;
background-color: red;
}
.icon {
border: 1px solid black;
padding: 5px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="SupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<img src="media/icons/menu.png" alt="" class="icon">
</button>
<div class="collapse navbar-collapse" id="SupportedContent">
<ul class="navbar-nav mx-auto">
<li class="nav-item text-center dropdown">
<a class="nav-link nav dropdown-toggle products-dropdown" href="#" data-toggle="dropdown" aria-expanded="false">Products</a>
<ul class="dropdown-menu">
<li class="dropdown-item d-block">
<a class="submenu-item" href="store">All Products ›</a>
</li>
<li class="dropdown-item d-block">
<a class="submenu-item" href="store#1">Heading 1 ›</a>
<ul class="dropdown-submenu">
<li class="dropdown-item d-block"><a href="1">1</a></li>
<li class="dropdown-item d-block"><a href="2">2</a></li>
<li class="dropdown-item d-block"><a href="3">3</a></li>
<li class="dropdown-item d-block"><a href="4">4</a></li>
</ul>
</li>
<li class="dropdown-item d-block">
<a class="submenu-item" href="store#2">Heading 2 ›</a>
<ul class="dropdown-submenu">
<li class="dropdown-item d-block"><a href="1">1</a></li>
<li class="dropdown-item d-block"><a href="2">2</a></li>
<li class="dropdown-item d-block"><a href="3">3</a></li>
<li class="dropdown-item d-block"><a href="4">4</a></li>
</ul>
</li>
<li class="dropdown-item d-block">
<a class="submenu-item" href="store#3">Heading 3 ›</a>
<ul class="dropdown-submenu">
<li class="dropdown-item d-block"><a href="1">1</a></li>
<li class="dropdown-item d-block"><a href="2">2</a></li>
<li class="dropdown-item d-block"><a href="3">3</a></li>
<li class="dropdown-item d-block"><a href="4">4</a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item text-center"><a class="nav-link nav" href="blog">Blog</a></li>
<li class="nav-item text-center"><a class="nav-link nav" href="about">About Us</a></li>
<li class="nav-item text-center"><a class="nav-link nav" href="contact">Contact Us</a></li>
</ul>
</div>
</nav>
<div id="lightbox_container"></div>