我将平滑滚动集成到了页面的锚点中。它非常适合以下代码:
$(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;
}
}
});
});
现在,当我单击链接时,它会滚动到该位置。但是因为我在顶部有一个固定的菜单栏(高度:100像素),所以它与内容有点重叠。我可以以某种方式解决此问题吗?就像在代码中说的那样:滚动到减去100px的锚点...我认为它可能与
scrollTop: target.offset(-100).top
是对的吗?
jQuery的offset是只为您提供元素偏移的函数。这并不意味着要更改偏移量本身。之后只减去100px。
scrollTop: target.offset().top - 100
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句