How do I pass an anchor on an $element.link

Hi everyone,

I created a sub menu with buttons. I would like to call each of them to reach the specified anchor on this page.

At this moment I found this but need some help : https://www.wix.com/code/reference/$w.Anchor.html#scrollTo

Thanks

Hi,
You can simply link the button to the anchor without using code as explained here

Best,
Tall

Hello Tal,

Yes this did the trick for one specific page. But as this menu must be different on other specific (to use this menu ad sub-menu) + multilingual.

The text of the buttons is different thanks to the code, but the links not :-/

Hi Kevin,

scrollTo() can indeed help, but not with the ‘link’ attribute of a button, but with defining a button onClick .
Lets say you have two anchors, one for EN and one for FR (‘#anchor1’ and ‘#anchor2’ respectfully), and a button called ‘#button1’.
I don’t know how you determine the language in your code but I’ll assume it’s in the URL.
The onClick will look a bit like this:

import wixLocation from 'wix-location'; // for URL reading
export function button1_click() {
    const lang = wixLocation.path[0];
    if (lang === 'EN') {
        $w('#anchor1').scrollTo();
        return;
    } 
    if (lang === 'FR') {
        $w('#anchor2').scrollTo(); 
        return;
    }
}

Liran.

Great Liran! Thank you very much for your help!

I’m a bit confused. If I want to make a menu, with a button that point to a specific page with an #anchor in it. How will the code be? When I try to use only:

$w("#anchor").scrollTo();

It doesn’t point anywhere because the #anchor is in a different page, while the code reside in the Site Area. Now when I use:

wixLocation.to('/somewhere').then( () => {$w("#anchor").scrollTo();}

Well this only points to that page without auto scrolling to the anchor.

Thanks for the help guys! This was driving me nuts. peace