Alexander Hein Alexander Hein - 5 months ago 11
jQuery Question

How can I add an offset of 50px to this jquery animate function?

How can I add an offset of 50px from top to this function?

$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
});

Answer

You can simply add 50px like:-

$('html, body').stop().animate({
    scrollTop: target.offset().top + 50
}, 1000);