부드러운 스크롤이 데스크톱에서 완벽하게 작동하는 고정 상단 탐색 모음 (높이 75px)이 있습니다. 작은 화면에있을 때 높이가 낮은 다른 navbar (50px 높이)가 있으므로 앵커가 올바른 위치에 도착합니다.
// Smooth Scoll
$('a[href*="#"]')
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
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) {
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top -75
}, 1200, function() {
var $target = $(target);
$target.focus();
if ($target.is(":focus")) {
return false;
} else {
$target.attr('tabindex','-1');
$target.focus();
};
});
}
}
});
클릭 한 navbar에 따라 target.offset (). top
을 설정할 수 있기를 원합니다. CSS 솔루션이 없습니다.
화면 width 확인을 추가하고 75를 50으로 변경
// Smooth Scoll
$('a[href*="#"]').not('[href="#"]') .not('[href="#0"]').click(function(event) {
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) {
event.preventDefault();
$('html, body').animate({
// change dependant on screen width
if ($(window).width() < 960) { // set in px
scrollTop: target.offset().top -50
} else {
scrollTop: target.offset().top -75
}
}, 1200, function() {
var $target = $(target);
$target.focus();
if ($target.is(":focus")) {
return false;
} else {
$target.attr('tabindex','-1');
$target.focus();
};
});
}
}
});
또는 클릭시 탐색 높이를 얻는 것이 더 나을 수 있습니다.
//set nav
var nav = $('.nav-bar'); // update to your nav class
// Smooth Scoll
$('a[href*="#"]').not('[href="#"]').not('[href="#0"]').click(function(event) {
// get nav height
var NavHeight = nav.height();
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) {
event.preventDefault();
$('html, body').animate({
// minus nav height
scrollTop: target.offset().top - NavHeight
}, 1200, function() {
var $target = $(target);
$target.focus();
if ($target.is(":focus")) {
return false;
} else {
$target.attr('tabindex','-1');
$target.focus();
};
});
}
}
});