我有以下jQuery脚本可向下滚动页面。
$(document).ready(function() {
$('a[href^=#]').on('click', function(e){
var href = $(this).attr('href');
$('html, body').animate({
scrollTop:$(href).offset().top
},1500);
e.preventDefault();
});
});
该页面具有固定的标题,高度为100px。显然,我需要向下滚动100像素,以便标题不会覆盖标题。
我用谷歌搜索,需要在某个地方输入“ {offset:-100}”。但是在哪里?
最佳答案
只需从scrollTop量减小标题高度即可。
$('html, body').animate({
scrollTop: $(href).offset().top - $("header").outerHeight() + "px"
}, 1500);