Seb
February 8, 2023, 6:45pm
1
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!
Seb
February 8, 2023, 7:14pm
3
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?
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
Seb
February 8, 2023, 7:32pm
5
Found, where to add it. This feature is nice.
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
Seb
February 8, 2023, 7:44pm
7
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
Seb
February 8, 2023, 8:58pm
9
Cheers mate! I will test it soon and report back!
Seb
February 8, 2023, 9:11pm
11
@kuligaposten That works, too. What did you change?
Seb
February 9, 2023, 8:28am
12
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
Seb
February 9, 2023, 9:28am
14
Tried that. Now the links are not clickable anymore - except the Osteopathie in the desktop menu
<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
Seb
February 9, 2023, 10:48am
16
Sure. Thanks for your code, anyway! Can you explain to me what he did? 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.
@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
Great JS as usual
1 Like