Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll down to div + a certain margin

Tags:

jquery

scroll

I am using this script to scroll to a certain .div on my page:

$(".button").click(function() {
    $('html, body').animate({
        scrollTop: $(".scrolltothis").offset().top
    }, 500);
});

Works perfectly. This scrolls the page to the top of the "scrolltothis" div. Now here's the problem: I have a menu bar which has a fixed position at the top of the page. It's the kind of menu bar that stays on top of the page when scrolling down.

So when the script scrolls down to the "scrolltothis" div, a part of the div falls behind the menu bar.

What i need is a way to tell the browser to scroll down to "scrolltothis div" + down 50 pixels so that this div becomes completely visible under the menu bar.

Hope you guys can help me!

like image 384
NvdB31 Avatar asked Dec 29 '25 23:12

NvdB31


1 Answers

Just add 50 pixels to scrollTop while setting it.Try this:

$(".button").click(function() {
 $('html, body').animate({
    scrollTop: $(".scrolltothis").offset().top + 50;
 }, 500);
});
like image 73
Milind Anantwar Avatar answered Jan 01 '26 14:01

Milind Anantwar