我目前遇到了哈希錨鏈接的棘手問題。 這裏是我的HTML代碼的簡單表示:如何避免在哈希標籤上滾動點擊固定高度內容
<div class="main-wrap">
<header></header>
<aside>
<nav>
<ul>
<li><a href="#article1">Article1</a></li>
<li><a href="#article2">Article2</a></li>
<li><a href="#article3">Article3</a></li>
</ul>
</nav>
</aside>
<section>
<article id="article1">Content Here</article>
<article id="article2">Content Here</article>
<article id="article3">Content Here</article>
</section>
<footer></footer>
和CSS:
body{overflow: scroll;}
.main-wrap{
overflow: hidden;
position: relative;
}
header{
position: relative;
z-index: 3;
height: 10vh;
}
aside{
position: fixed;
width: 22%;
height: 84vh; /* Equal to 100vh - (header + footer)vh */
top: 10vh;
left: 0;
bottom: 6vh;
}
section{
position: relative;
min-height: 84vh; /* Equal to 100vh - (header + footer)vh */
width: 78%;
left: 22%; /* Equal to aside width*/
}
footer{
position: relative;
z-index: 3;
height: 6vh;
}
我創建了一個工具欄菜單與哈希鏈接有滾動導航,至於我一直在工作。但是當我點擊散列的錨點時,所有元素都向前移動一點,包括頁眉和頁腳,並被.main-wrap
元素的overflow:hidden;
屬性隱藏。另外,當我回到非哈希頁面時,問題仍然在運行,除非我重新加載它。
我無法找到我如何修復它的任何線索。有任何想法嗎 ?
編輯:我也使用reset.css是在body
和html
空白和邊距設置爲0
編輯2: 我想我知道發生了什麼事情。通過點擊一個錨鏈接,身體被迫滾動.main-wrap
股利,這就是爲什麼一切看起來像它已經上移。實際上.main-wrap
的overflow:hidden;
屬性剛剛進一步下移,隱藏了錯誤的部分。
這個問題類似W/A固定頭和被遮擋的錨?一種選擇是:將一個類添加到具有「padding-top」值的錨點以將其向下推。或者,使用':before' psuedo-element或JS來更改元素的偏移量。可能不會直接適用於您的側邊欄問題,但也許是正確的方向:http://stackoverflow.com/questions/4086107/html-positionfixed-page-header-and-in-page-anchors .... – mc01