Jquery scrolltop偏移固定头
我正在使用JQuery平滑滚动图像:Jquery scrolltop偏移固定头,jquery,html,Jquery,Html,我正在使用JQuery平滑滚动图像: function smoothScrollTo(hash) { $("html:not(:animated).,body:not(:animated)").animate({ scrollTop: $(hash).offset().top }, 650, function () { location.hash = hash; }); } $(function () { $("#content-i
function smoothScrollTo(hash) {
$("html:not(:animated).,body:not(:animated)").animate({
scrollTop: $(hash).offset().top
}, 650, function () {
location.hash = hash;
});
}
$(function () {
$("#content-images li a[href*=#]").click(function () {
this.blur();
smoothScrollTo(this.hash);
return false;
});
});
它的工作很好,但我有一个固定的导航栏上的网站停留在顶部的页面,因为它滚动。当页面向下滚动到下一幅图像时,它会滚动到导航栏下方,使其与视线分离
我的问题是,如何修改上述代码以补偿固定导航条的高度
任何帮助都将不胜感激
变化:
scrollTop: $(hash).offset().top
致:
这应该考虑到固定导航条的高度
希望有帮助:)使用威尔的答案,尝试以下方法:
function smoothScrollTo(hash, t) { // two params
$("html:not(:animated).,body:not(:animated)").animate({
scrollTop: $(hash).offset().top + $('#fixed_nav_bar').outerHeight()
}, 650, function () {
var tmp = t.id; // hold the id
t.id = ''; // remove it so we don't jump
location.hash = hash;
t.id = tmp; // now that we didn't jump we can move it back
});
}
$(function () {
$("#content-images li a[href*=#]").click(function () {
this.blur();
smoothScrollTo(this.hash, this); // two args
return false;
});
});
非常感谢!它确实可以工作,但是现在似乎发生的事情是,它平滑地向下滚动,然后一旦到达元素,它将快速偏移高度,导致页面跳到正确的位置,从而使页面不再非常平滑。有解决方案吗?这是因为动画回调更改了location.hash,这与单击链接相同。给我一点时间,我最近不得不处理这件事。编辑:事实上,这不起作用,但是,试试这个:谢谢!有一个读取,似乎无法让它工作,它只是创建了一个错误,你不能再向下滚动点击后的图像。在这种情况下,有没有办法删除location.hash回调?非常感谢!刚刚对它进行了测试,并对您的代码和will的代码进行了进一步的观察,似乎它只是没有考虑页眉高度。图像仍会滚动到浏览器窗口的顶部(固定导航下方)是否有方法手动设置高度?
function smoothScrollTo(hash, t) { // two params
$("html:not(:animated).,body:not(:animated)").animate({
scrollTop: $(hash).offset().top + $('#fixed_nav_bar').outerHeight()
}, 650, function () {
var tmp = t.id; // hold the id
t.id = ''; // remove it so we don't jump
location.hash = hash;
t.id = tmp; // now that we didn't jump we can move it back
});
}
$(function () {
$("#content-images li a[href*=#]").click(function () {
this.blur();
smoothScrollTo(this.hash, this); // two args
return false;
});
});