【问题标题】:Open another page and scroll to anchor打开另一个页面并滚动到锚点
【发布时间】:2020-05-12 09:27:01
【问题描述】:

我目前正在链接到同一页面上的锚点,下面的代码通过滚动效果实现了这一点。但是,我想将锚点放在不同的页面上。

我想要的是,当我单击按钮(.ebooks-button)时,它会打开页面并向下滚动到锚点。

这是我目前正在使用的代码。任何人都可以建议修改 javascript 来帮助我实现这一目标吗?

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

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

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

});

【问题讨论】:

    标签: javascript anchor-scroll anchorpoint


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      浏览器安全模型不允许您这样做。您可以拥有将用户发送到另一个页面并包含锚点的代码,但处理平滑滚动到该锚点的代码需要位于用户被发送到的页面上。浏览器离开您的页面后,该页面上的其他代码将不会运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-05
        • 2022-11-13
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 2021-10-08
        • 2013-04-16
        • 2021-09-18
        相关资源
        最近更新 更多