jQuery平滑滚动到其他页面上带有偏移量的链接
我从CSS技巧中的代码开始,它可以很好地处理偏移量。问题是该站点有一个固定的标题,所以我需要它在从另一个页面导航到内部链接时应用偏移量。目前它正在被切断jQuery平滑滚动到其他页面上带有偏移量的链接,jquery,smooth-scrolling,Jquery,Smooth Scrolling,我从CSS技巧中的代码开始,它可以很好地处理偏移量。问题是该站点有一个固定的标题,所以我需要它在从另一个页面导航到内部链接时应用偏移量。目前它正在被切断 $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostnam
$('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-144
}, 1000);
return false;
}
}
});
任何帮助都将不胜感激。所讨论的页面是实际上没有任何本机js函数可以阻止在页面加载时进行散列锚定。但是有一个很好的解决方法,可以在这里找到。我以前用过这种方法,效果很好
setTimeout(function() {
var hash = location.hash
if (hash && $(hash).length) {
var target = $(hash).offset().top;
var headerH = $('header').height();
$(window).scrollTop(target - headerH)
/*
//or with animate,
//but you'll need to reset the scrollTop to 0 (the top) first:
$(window).scrollTop(0);
$('html,body').animate({scrollTop:target - headerH}, 1000);
*/
}
}, 1);
那正是我想要的。除了原始代码之外,我还使用了这段代码,它们一起玩得非常好。谢谢你,马克!