Archived topic
smooth-scroll links from other pages
4 replies · Started by JBD on October 22, 2019
If I'm linking to parts of a page using anchor links, is it possible to smooth scroll to the section in question if coming from another page?
The reason I ask is that I have a menu that appears on multiple page with links to parts of the page, but the links are absolute, eg href="/sub-page#section2"
I looked at adding the code previously mentioned:-
add_filter( 'generate_smooth_scroll_elements', function( $elements ) {
$elements[] = 'a[href*="#"]:not(.generate-back-to-top)';
return $elements;
} );
but I don't think this would work on links such as
<a href="/sub-page#section2" >menu link</a>
Update, If I change the links from
<a href="/sub-page#section2" >menu link</a>
to
<a href="/sub-page/#section2" >menu link</a>
Then the smooth-scroll works if I'm on the same page.
However it doesnt smooth-scroll if I'm linking from another page.
Is this possible?
Hi there,
smooth-scroll will only work on anchors that are on the same page. Not possible to make it apply when navigating to a new page.
ok, good to know, thanks
It may be possible if you use a jQuery smooth scroll script instead of our vanilla javascript smooth scroll script. This might help: https://stackoverflow.com/a/46673487