user2028856
user2028856

Reputation: 3183

jQuery scroll to top but not quite top

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

Answers (1)

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18113

Define it, like you do, minus 100px;

var pos = $('#' + url_parameter).offset().top - 100;
$("body, html").animate({ 
   scrollTop: pos 
});

Upvotes: 1

Related Questions