我正在使用固定在最大宽度 1000 像素的导航栏。高度为 60px。
我的问题是页面跳转不会到达它应该到达的地方。
$(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);
if (target.length <= 1000) {
$('html,body').animate({
scrollTop: target.offset().top - 60
}, 1000);
};
return false;
}
}
});
});
最佳答案
做到了!这将向后移动 60px,屏幕最大宽度为 1000px
$(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);
if (matchMedia('only screen and (max-width: 1000px)').matches) {
$('html,body').animate({
scrollTop: target.offset().top -60
}, 1000);
return false;
}
}}
});
});
关于javascript - 平滑滚动解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33877751/