Offcanvas - scrolling after closing

On the following site I have this issue: I put the menu in an offcanvas and I use anchors to scroll to the different sections. On mobile, if I close the offcanvas it jumps back to the top and I do not know why. It happens on android and some desktops, but not on iOS. Any ideas?

Many thanks!

This might be of help

1 Like

Thatā€™s a nifty feature. I will actually include ask if it was a feature wish, but it is not what I was looking for. When Iā€™m on mobile / mobile size and I manually close the off-canvas it will automatically scroll back to top.
Thanks for your quick reply!
PS: where would I add this? ā€œdata-bs-dismiss=ā€œoffcanvasā€ā€ In the attributes panel? :slight_smile:

Edit: See here: Scroll to top after closing offcanvas

You would put it on each of your navigation links, then it should close the offcanvas automatically

Found, where to add it. This feature is nice. :slight_smile:
But it still keeps scrolling to the top after closing the offcanvas menu. See video above.

Iā€™ve just tried the same thing on a dummy document, it goes all a bit crazy

If its a problem with the offcanvas menu I can revert to the standard menu. I prefer the offcanvas menu but it is the first time I use it.

Edit: Itā€™s interesting that this issue appears on a scaled down browser window and android but not ios.

I have written a very rough js file that seems to work:

document.querySelector(".anchor").addEventListener("click", function(event){
   var link=this.getAttribute("data-link");
  
   function delay(time) {
       return new Promise(resolve => setTimeout(resolve, time));
     }
     
     delay(500).then(() =>  
     document.querySelector(link).scrollIntoView({ 
       behavior: 'smooth' 
     }));
 });



for each of your links that you want to scroll to add the class anchor and change the href to data-link

so href=ā€œ#leistungenā€ would become data-link=ā€œ#leistungenā€

what it does is adds a .5 second delay between the offcanvas closing and the scroll

hope this helps

1 Like

Cheers mate! I will test it soon and report back!

@Seb

1 Like

@kuligaposten That works, too. What did you change?

I think Iā€™ve added what you mentioned:

  • anchor-class to the sections.
  • I tried adding the anchor class to the links themselves too, but that didnt work reliably either.
  • data-link to the links instead of href

Now the links donā€™t work anymore. Did I do something wrong? Updated the site I posted above.

You need to add the anchor class to the link, not the section

so in your navbar

<li class="nav-item"><a class="nav-link" data-link="#leistungen">Osteopathie</a></li>

will become

<li class="nav-item"><a class="nav-link anchor" data-link="#leistungen">Osteopathie</a></li>

You could just change the js to reference ā€˜nav-linkā€™ instead of ā€˜anchorā€™. I did it this way just in case there were any non anchor links

Tried that. Now the links are not clickable anymore - except the Osteopathie in the desktop menu :smiley:

<a class="nav-link anchor" data-link="#osteopathie">Osteopathie</a>

Current version: https://aso.bss.design/

I think your better off using @kuligaposten solution, it is much more elegant, and Iā€™ve noticed a few bugs in mine that will give issues when not off canvas

Sure. Thanks for your code, anyway! Can you explain to me what he did? :slight_smile: I checked his code and only could find the merged .js file. But I didnā€™t learn a lot of javascript yet.

By the looks of things, if you copy his js into your file and then do the following it should work

also give your nav and ID of mainNav
and the offcanvas an ID of sidebar

hopefully @kuligaposten will share his bss file with you. :slight_smile:

@seb Update: he already has shared it, it is at the bottom of the page in his example.

https://bss.kulpost.com/designs/andrea.bsdesign

1 Like

@richards
I have shared the bsdesign already. Have a look at the example above and scroll down to the footer.
There you can download the design.

The data-bs-dismiss="offcanvas" on links in the offcanvas is for bootstrap 5.1.3 and older, it doesnā€™t work in current version

1 Like

@kuligaposten I found it about 30 seconds before you posted :slight_smile:

Great JS as usual :clap:

1 Like