Țaman ŞheƦzad Țaman ŞheƦzad - 1 month ago 19
CSS Question

jQuery auto scroll to elements

I came across this useful blog Smoothly scroll to an element without a jQuery plugin

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. Any solution here?

Answer

Try this:

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