Open another page and scroll to anchor

740 Views Asked by At

I am currently linking to an anchor point on the same page and the code below achieves this with a scrolling effect. However, I want to put the anchor on a different page.

What I'd like is that when I click the button (.ebooks-button) it opens the page and scrolls down to the anchor.

Here is the code that I'm currently using. Can anybody please suggest a modification in the javascript to help me achieve this?

$(document).ready(function() {
var away = false;

$('.ebooks-button').click(function() {

    $("html, body").animate({scrollTop: $('#ebooks').offset().top}, 700);
});

});

2

There are 2 best solutions below

0
jobayersozib On BEST ANSWER

//Add this in your css
html {
  scroll-behavior: smooth;
}

 //Try this hope it will help

<a href="ebooks.html#ebooks">Ebooks button</a>

0
Jason Kohles On

The browser security model won't let you do that. You can have code that sends the user to another page and includes an anchor, but the code that handles smoothly scrolling to that anchor needs to be on the page the user gets sent to. Once the browser has navigated away from your page, no other code on that page is going to run.