简体   繁体   中英

Open another page and scroll to anchor

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);
});

});

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.

 //Add this in your css html { scroll-behavior: smooth; } //Try this hope it will help <a href="ebooks.html#ebooks">Ebooks button</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM