我正在使用滚动到 div 功能的脚本。现在我将标题固定在顶部,因为我希望它具有粘性。但现在当我单击 div 链接时,它会转到标题下方,因为标题是固定的。我可以在实际 div 之前停止滚动 80px 吗?
这是我正在使用的代码。
<script>
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
</script>
<script src="js/scroll-startstop.events.jquery.js" type="text/javascript"></script>
<script>
$(function() {
var $elem = $('#content');
$('#nav_up').fadeIn('slow');
$('#nav_down').fadeIn('slow');
$(window).bind('scrollstart', function(){
$('#nav_up,#nav_down').stop().animate({'opacity':'0.2'});
});
$(window).bind('scrollstop', function(){
$('#nav_up,#nav_down').stop().animate({'opacity':'1'});
});
$('#nav_down').click(
function (e) {
$('html, body').animate({scrollTop: $elem.height()}, 800);
}
);
$('#nav_up').click(
function (e) {
$('html, body').animate({scrollTop: '0px'}, 800);
}
);
});
</script>
最佳答案
您可以从滚动 div 的顶部偏移量减去标题高度
$('html,body').animate({
scrollTop: target.offset().top - 80
}, 1000);
关于javascript - 由于我使用的是固定标题,如何才能停止在 div 之前平滑滚动 80px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451449/