It is about a project created with Bootstrap 5. The problem was that the offcanvas menu stayed open, by clicking an anchor link.
how can I close offcanvas menu after click on a anchor link.
<nav class="navbar fixed-top py-5 ">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="photo.jpeg" alt="Image"
class=" image img-fluid rounded-circle position-absolute top-100 start-50 translate-middle ">
</a>
<div style="position: fixed; width: 100%; z-index: 1;">
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas"
data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar"
aria-labelledby="offcanvasNavbarLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasNavbarLabel">Index</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas"
aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<ul class="navbar-nav justify-content-end flex-grow-1 pe-3">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#Home" >Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#service-section">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#work-section">Works</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#contact-section">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#moreinfo-section">More Info</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
2
Answers
Here is one possible solution.
The html is unchanged except for the top 3 lines (and removing an extra
</div>
at the bottom).No HTML was edited, only the addition of the 3 lines of jquery.