Whenever I open my menu, click on a link, and scroll down the page using the menu, upon closing the menu I am automatically taken back to the top of the page.
I have experimented with the following scripts:
let y = window.scrollY;
let offcanvas = document.getElementById('offcanvasMenu');
offcanvas.addEventListener('hidden.bs.offcanvasMenu', (e) => {
window.scrollTo(0, y);
});
and
var myOffcanvas = document.getElementById('offcanvasNav')
myOffcanvas.addEventListener('hide.bs.offcanvas', function (e) {
console.log(e)
//e.preventDefault()
//e.stopPropagation()
})
Unfortunately, none of these scripts have provided a solution for my issue.
Code Snippet
<a class="bi bi-list offcanvas-icon" data-bs-toggle="offcanvas" href="#offcanvasMenu" role="button" aria-controls="offcanvasMenu"></a>
<div class="offcanvas offcanvas-end" data-bs-scroll="true" tabindex="-1" id="offcanvasMenu" aria-labelledby="offcanvasMenuLabel">
<div class="offcanvas-header">
<button type="button" class="btn-close ms-auto" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body d-flex flex-column justify-content-center align-items-center">
<nav>
<ul>
<li>
<a href="#contacts">Contacts</a>
</li>
</ul>
</nav>
</div>
</div>
<section class="contact-widget spad" id="contacts">lala </section>`
<!-- Bootstrap 5 + icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css">
<link href="https://cdn.jsdelivr.net/npm/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/dist/js/bootstrap.bundle.min.js"></script>