我有以下問題。我想要的是,當用戶點擊「聯繫人」上的導航欄時,它將鏈接到聯繫人頁面。這是一個單一頁面。當您聯繫並點擊頁面底部時,例如「Over ons」,它應該重定向到主頁(單頁)並停在該部分。這是有效的,但是當你來自另一個頁面時,當前部分與標題重疊。從另一個頁面出來時段會被標題重疊
只有當您在index.html
內進行導航時,jQuery代碼纔會使用標題的偏移量。
有沒有辦法解決這個問題,所以該部分不會被頭部重疊?
活生生的例子:
http://codepen.io/anon/pen/NqxxQd
jQuery代碼:
// An offset to push the content down from the top
var offset = $('#header').outerHeight();
$('#primary-navwrapper li:not(.prev-page, .next-page), .list-of-links li').find('a[href^="#"]').click(function(event) {
event.preventDefault();
$('#primary-navwrapper li a').removeClass("current");
$(this).addClass("current");
var anchorId = $(this).attr("href");
var target = $(anchorId).offset().top - offset;
$('html, body').animate({ scrollTop: target }, 500, function() {
window.location.hash = anchorId;
});
});
function setActiveListElements(event){
// Get the offset of the window from the top of page
var windowPos = $(window).scrollTop();
$('#primary-navwrapper li a[href^="#"]').each(function() {
var anchorId = $(this);
var target = $(anchorId.attr("href"));
var offsetTop = target.position().top - offset;
if (target.length > 0) {
if (target.position().top - offset <= windowPos && (target.position().top + target.height() + offset) > windowPos) {
$('#primary-navwrapper li a').removeClass("current");
anchorId.addClass("current");
}
}
});
}
$(window).scroll(function() {
setActiveListElements();
//updateLocationHash();
});
我們不能去「聯繫」頁面,從您的代碼鋼筆。 – krillgar
這是聯繫頁面http://codepen.io/anon/pen/RPrabw的codepen。它適用於我,當你點擊頂部導航欄中的「contact」。如果它不工作,讓我知道! @krillgar – Caspert
@Caspert該例中沒有頂部導航。 – newmediafreak