ScrollTop或ScrollTo无法正常工作

时间:2014-06-18 05:10:17

标签: jquery

我正在努力实现Scroll动画,但它不起作用!我使用过各种各样的图书馆,但没有人工作。

一小段JQuery脚本:

<script src="jquery-1.11.1.min.js"></script>
<script>
    $("#menu a").click(function() {
    var menuID = $(this).attr("href");
    $("body").animate({scrollTop: $(menuID).offset().top}, "slow");
    return false;
});

</script>

完整代码:here 网站:here

4 个答案:

答案 0 :(得分:1)

<script src="jquery-1.11.1.min.js"></script>
<script>
    $("#menu a").click(function() {
    event.preventDefault();
    var menuID = $(this).attr("href");
    $("html, body").animate({scrollTop: $(menuID).offset().top}, "slow");
    return false;
});

</script>

您需要将其更改为html, body,而不仅仅是body

答案 1 :(得分:1)

使用event.preventDefault()

<script>
    $("#menu a").click(function(event) {
    event.preventDefault();
    var menuID = $(this).attr("href");
    $("html, body").animate({scrollTop: $(menuID).offset().top}, "slow");
    return false;
});

</script>

答案 2 :(得分:0)

要使其工作,请尝试下面的代码,使用html,body而不是body

<script src="jquery-1.11.1.min.js"></script>
<script>
    $("#menu a").click(function() {
    var menuID = $(this).attr("href");
    $("html, body").animate({scrollTop: $(menuID).offset().top}, "slow");
    return false;
});

</script>

答案 3 :(得分:0)

试试这个

$('html, body').animate({   scrollTop: $(menuID).offset().top}, "slow");
相关问题