Site logo

Archived topic

smooth-scroll links from other pages

4 replies · Started by JBD on October 22, 2019

Viewing posts 1–5 of 5

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

This archived topic is closed to new replies.