如何使用固定导航修复 "Smooth Scrolling"
How to fix "Smooth Scrolling" with fixed navigation
一旦我设置了平滑滚动和固定导航,我就无法让锚停在上面添加的 100 像素处。它跳回到原来的散列。
我试过将偏移量设置为 100,但跳跃不成功。它在 Safari 上运行良好,但在 Chrome 或 Firefox 上运行不佳。
这是我用过的:
// URL updates and the element focus is maintained
// originally found via in Update 3 on http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
// filter handling for a /dir/ OR /indexordefault.page
function filterPath(string) {
return string
.replace(/^\//, '')
.replace(/(index|default).[a-zA-Z]{3,4}$/, '')
.replace(/\/$/, '');
}
var locationPath = filterPath(location.pathname);
$('a[href*="#"]').each(function () {
var thisPath = filterPath(this.pathname) || locationPath;
var hash = this.hash;
if ($("#" + hash.replace(/#/, '')).length) {
if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, '')) {
var $target = $(hash), target = this.hash;
if (target) {
$(this).click(function (event) {
event.preventDefault();
$('html, body').animate({scrollTop: $target.offset().top - 100}, 1000, function () {
location.hash = target;
$target.focus();
if ($target.is(":focus")){ //checking if the target was focused
return false;
}else{
$target.attr('tabindex','-1'); //Adding tabindex for elements not focusable
$target.focus(); //Setting focus
};
});
});
}
}
}
});
这是我的开发者 link:http://dev.savagebrands.com
我希望锚点比 link 所在的实际部分高 100 像素。
尝试始终 return false
在您的点击功能结束时。
这将阻止默认的浏览器行为
$(this).click(function (event) {
event.preventDefault();
// Your code
return false;
});
一旦我设置了平滑滚动和固定导航,我就无法让锚停在上面添加的 100 像素处。它跳回到原来的散列。
我试过将偏移量设置为 100,但跳跃不成功。它在 Safari 上运行良好,但在 Chrome 或 Firefox 上运行不佳。
这是我用过的:
// URL updates and the element focus is maintained
// originally found via in Update 3 on http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
// filter handling for a /dir/ OR /indexordefault.page
function filterPath(string) {
return string
.replace(/^\//, '')
.replace(/(index|default).[a-zA-Z]{3,4}$/, '')
.replace(/\/$/, '');
}
var locationPath = filterPath(location.pathname);
$('a[href*="#"]').each(function () {
var thisPath = filterPath(this.pathname) || locationPath;
var hash = this.hash;
if ($("#" + hash.replace(/#/, '')).length) {
if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, '')) {
var $target = $(hash), target = this.hash;
if (target) {
$(this).click(function (event) {
event.preventDefault();
$('html, body').animate({scrollTop: $target.offset().top - 100}, 1000, function () {
location.hash = target;
$target.focus();
if ($target.is(":focus")){ //checking if the target was focused
return false;
}else{
$target.attr('tabindex','-1'); //Adding tabindex for elements not focusable
$target.focus(); //Setting focus
};
});
});
}
}
}
});
这是我的开发者 link:http://dev.savagebrands.com
我希望锚点比 link 所在的实际部分高 100 像素。
尝试始终 return false
在您的点击功能结束时。
这将阻止默认的浏览器行为
$(this).click(function (event) {
event.preventDefault();
// Your code
return false;
});