简体   繁体   中英

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? eg 100px

Define it, like you do, minus 100px;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM