我正在尝试为我的 div 元素制作一个 scrollTop,但不是它的确切位置。我想 在我的 div 元素之前移动 20px。我想我可以更好地向您解释我的代码:
HTML:
<div id="arrow-down">Click here and go to content!</div>
<div id="content">The content is here!</div>
J查询: 我已经有一个运行良好的代码,但我想让它有所不同。
$(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);
});
});
好吧,我不知道它看起来是否困惑......我希望你们能帮助我!
最佳答案
你可以这样做:
$('html, body').animate({
scrollTop: $("#content").offset().top - 20
}, 800);
关于javascript - ScrollTop 到元素 - 20px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912980/