Reputation: 3183
I'm not sure if this the correct title to ask but I'm trying to scroll to an element in the DOM on page load.
I'd like to have the DOM element centered for the user but this code I'm currently using forces the element to the top:
$("body, html").animate({
scrollTop: $('#' + url_parameter).offset().top
});
My question is, how can I set a number of pixels for the element to be separated from the top? e.g. 100px
Upvotes: 1
Views: 41
Reputation: 18113
Define it, like you do, minus 100px;
var pos = $('#' + url_parameter).offset().top - 100;
$("body, html").animate({
scrollTop: pos
});
Upvotes: 1