转到单击事件jquery上的锚点链接

时间:2017-01-12 23:16:39

标签: javascript jquery html hyperlink anchor

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

3 个答案:

答案 0 :(得分:2)

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

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

这个脚本将完成工作

P.S #bottom_id是您要滚动到的元素的ID。 或锚链接:

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

脚本:

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

答案 1 :(得分:1)

jquery方式:

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

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

有一个jquery plug in可以有效地执行此操作,但如果你想坚持使用直接的javascript,你可以使用this previous answer所述的element.scrollTo;

可以在Mozilla's site找到更好的文档。用javascript。

答案 2 :(得分:0)

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

例如,这个:

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

如果用户点击具有ID&#34; ClickMe&#34;的元素,则会将用户带到index.php。 (E.G. ......