I have a simple bootstrap 5 offcanvas element. This contains a simple link to an anchor in the body of the html page.
I configured the offcanvas to have no backdrop and enables body scrolling.
Now when I open the offcanvas en click the link. The body scrolls to that section of the page. But when I close the offcanvas. The body scrolls back to the top. How can I keep the body where it is?
It seems like that the button that is used to open the offcanvas gets the focus back. I tried something like this.
var myOffcanvas = document.getElementById('offcanvasExample');
//var bsOffcanvas = new bootstrap.Offcanvas(myOffcanvas)
myOffcanvas.addEventListener('hidden.bs.offcanvas', function (event) {
//event.stopPropagation();
//event.preventDefault();
// Give the document focus
// Remove focus from any focused element
if (document.activeElement) {
document.activeElement.blur();
}
window.focus();
});
but the page keeps scrolling back to the button.
Kind regards