jquery滚动效果扭曲

时间:2014-01-24 18:36:24

标签: jquery

我想向下滚动一点说20px然后滚动到顶部。我做了很多试验n错误但无法实现这种效果。到目前为止我的代码是:

$( "#moveToTop" ).on("click", function() {

        var n = $(document).height() - 20;
        $("html, body").animate({ scrollTop: n}, 1000); // scroll down only 20px but it scrolls all the way down!
        $("html, body").animate({ scrollTop: 0}, 1000); //then scroll to the top of the page
        return false;

    });

修改 如果我使用var n = $(document).height() - 20从文档高度减去20。它仍然一直向下滚动,即使我添加20仍然相同,我不知道该怎么做

2 个答案:

答案 0 :(得分:1)

您将n设置为文档高度,因此它将一直向下滚动...

答案 1 :(得分:0)

我想,我应该使用窗口作为参考添加20到窗口的scrollTop位置:

var n = $(window).scrollTop()+20;
相关问题