简体   繁体   English

打开另一个页面并滚动到锚点

[英]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.我想要的是,当我单击按钮 (.ebooks-button) 时,它会打开页面并向下滚动到锚点。

Here is the code that I'm currently using.这是我目前正在使用的代码。 Can anybody please suggest a modification in the javascript to help me achieve this?任何人都可以建议修改 javascript 以帮助我实现这一目标吗?

$(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.浏览器安全 model 不会让你这样做。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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