简体   繁体   English

jQuery自动滚动到元素

[英]jQuery auto scroll to elements

I came across this useful blog Smoothly scroll to an element without a jQuery plugin 我遇到了这个有用的博客,无需jQuery插件即可平滑滚动到一个元素

In the below code 在下面的代码中

$('body#sliderOn').animate({
   scrollTop: $("#target-element").offset().top
}, 1000);

Auto scrolls down to target-element if sliderOn id exists within the body, but, because i have a fixed navbar the target-element goes behind it and cause not to show 20px of the target-element on top. 如果体内存在liderOn id,则自动向下滚动到目标元素,但是,由于我有固定的导航栏,因此目标元素在其后面,并且导致顶部未显示20px的目标元素。 Any solution here? 有什么解决办法吗?

Try this: 尝试这个:

$('body#sliderOn').animate({
   scrollTop: $("#target-element").offset().top - 20
}, 1000);

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

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