在单击事件 jquery 时转到锚点 link

Go to anchor link on click event jquery

正如描述所说,我发现了很多关于平滑滚动和位置 属性 在 java 脚本上的东西,但似乎没有做我正在寻找的东西,它只是模仿的功能一个 html "a" 标签(我不需要为这个项目使用 html link 标签)

jquery方式:

$("body").scrollTop($("[name=elementname]").position().top)

这会获取指定元素的顶部并将滚动顶部位置设置为该位置。

有个jquery plug in which does this effectively, but if you want to stick to straight javascript you can just use element.scrollTo as described on this previous answer;

可以在 Mozilla's site 上找到更好的文档。随着 javascript.

所以我得到的是您想在不使用 html 锚标记的情况下向下滚动到某个元素 如果我是对的 假设你有一个 html 输入标签

<input type="button" id="top" value="button"/>

此脚本将完成工作

P.S #bottom_id 是你要滚动到的元素的id。 或锚 link:

<a href="#bottom" id="bottom_id"></a>

脚本:

 $("#top").click(function() {
    $('html, body').animate({
        scrollTop: $("#bottom_id").offset().top
    }, 2000);
});

要让 JQuery 将用户带到与页面不同的 URL(例如锚标记),请将 window.location 附加到活动中。

例如,这个:

$('#ClickMe').click(function(){ window.location = 'index.php'; });

如果用户点击具有 ID "ClickMe" 的元素,会将用户带到 index.php(例如 ...