1

我正在尝试为我的 div 元素创建一个 scrollTop,但不完全是它的位置。我想在我的 div 元素之前移动 20px。我想我可以更好地为您展示我的代码:

HTML:

<div id="arrow-down">Click here and go to content!</div>

<div id="content">The content is here!</div>

JQuery:我已经有一个运行良好的代码,但我想让它与众不同。

$(document).ready(function() {
    $('#arrow-down').click(function() {
        $('html, body').animate({
               scrollTop: $("#content").offset().top
        }, 800);
    });
});

这段代码将我带到了 div#content,但我想从它的顶部走 20px!

像这样的东西:

$(document).ready(function() {
    $('#arrow-down').click(function() {
        $('html, body').animate({
               scrollTop: $("#content" - 20px).offset().top
        }, 800);
    });
});

好吧,我不知道它是否看起来很困惑......我希望你们能帮助我!

4

2 回答 2

5

你可以这样做:

$('html, body').animate({
     scrollTop: $("#content").offset().top - 20
}, 800);
于 2013-11-11T18:12:21.023 回答
2
$(document).ready(function() { 
    $('#arrow-down').click(function() {
         $('body').animate({
         scrollTop: $("#content").offset().top-20
    }, 800);
  }); 
});

试试这个

于 2013-11-11T18:14:52.893 回答