jQuery 使用 ID 偏移元素运行 ScrollTop
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9618997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run ScrollTop with offset of element by ID
提问by Staffan Estberg
Trying to make the browser scroll to a specific ID with an added offset -
尝试使浏览器滚动到具有附加偏移量的特定 ID -
$('html, body').animate({scrollTop: $('#contact').offset().top}, 'slow');
What I need to do is to set the offset by -100px. How can I accomplish this?
我需要做的是将偏移量设置为-100px。我怎样才能做到这一点?
回答by charlietfl
No magic involved, just subtract from the offset top of the element
不涉及魔法,只需从元素的偏移顶部减去
$('html, body').animate({scrollTop: $('#contact').offset().top -100 }, 'slow');
回答by Rai Ahmad Fraz
var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top;
if (!isNaN(top)) {
$("#app_scroler").click(function () {
$('html, body').animate({
scrollTop: top
}, 100);
});
}
if you want to scroll a little above or below from specific div that add value to the top like this.....like I add 800
如果你想从特定的 div 向上或向下滚动一点,像这样将值添加到顶部......就像我添加 800
var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top + 800;