我有以下 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 的固定标题。显然我需要少向下滚动 100px,所以标题不会覆盖标题。 我用谷歌搜索,我需要在某处输入“{offset: -100}”..但是在哪里?
最佳答案
只需从 scrollTop 量减少标题高度。
$('html, body').animate({
scrollTop: $(href).offset().top - $("header").outerHeight() + "px"
}, 1500);
关于javascript - 由于标题固定,jQuery 滚动偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805711/