问题
Trying to make the browser scroll to a specific ID with an added offset -
$('html, body').animate({scrollTop: $('#contact').offset().top}, 'slow');
What I need to do is to set the offset by -100px. How can I accomplish this?
回答1:
No magic involved, just subtract from the offset top of the element
$('html, body').animate({scrollTop: $('#contact').offset().top -100 }, 'slow');
回答2:
var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top;
if (!isNaN(top)) {
$("#app_scroler").click(function () {
$('html, body').animate({
scrollTop: top
}, 100);
});
}
if you want to scroll a little above or below from specific div that add value to the top like this.....like I add 800
var top = ($(".apps_intro_wrapper_inner").offset() || { "top": NaN }).top + 800;
来源:https://stackoverflow.com/questions/9618997/run-scrolltop-with-offset-of-element-by-id