简体   繁体   English

jQuery-如何动画滚动到元素并在顶部添加100px

[英]jQuery - How to animate scroll to element and add 100px in top

Here is the code that i use to animate scroll to specific element. 这是我用来动画滚动到特定元素的代码。

$('html, body').animate({
    scrollTop: $("#establishment3").offset().top
}, 2000);

Question: How can i animate the scroll to this element but to scroll in 100px from top of the element ? 问题:我如何为滚动到此元素的动画设置动画,但要从元素顶部滚动100像素?

Thanks in advance! 提前致谢!

You can use this way: 您可以使用这种方式:

$('html, body').animate({
    scrollTop: $("#establishment3").offset().top - 100
}, 2000);

scroll in 100px from top of the element 从元素顶部滚动100像素

I don't understand whether you wanna go below or above. 我不知道你想低于还是高于。 Give a + 100 if you wanna go below, or - 100 if you wanna go 100px above the element. 如果想低于该元素,则给+ 100如果要高于该元素100px ,则给- 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM