我有一個水平導航我會鎖定固定到頁面的頂部時,導航的頂部屬性是在頁面的頂部。但是,當導航到達頁面的頂部時,它會增加寬度,並且會變得很麻煩。一旦我進一步向下滾動,這種現象就會停止。請注意,滾動備份時會發生同樣的情況。換句話說,當nav的頂部到達頁面的頂部時,發生寬度變化跳躍的事情。jQuery捕捉/固定菜單寬度更改和跳轉當滾動過去某些點
鏈接到下面的小提琴。我試圖儘可能地添加代碼,但是SO正在抱怨。
HTML
<div id="container">
<div class="phone-number">
...
</div>
<div id="phone-sub-text">
...
</div>
<div id="social-media">
<div>
<img id="fb-icon" src="img/fb-rect.jpg"/>
<img src="img/twitter-rect.jpg"/>
</div>
</div>
<h1>
...
</h1>
<div id="float-nav-bar"></div>
<div id="main">
<header>
...
</header>
<header class="sub-header">
...
</header>
<div id="main-content-top" class="main-content">
<section>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
</section>
<figure>
<img src="#" />
</figure>
</div>
<header>
...
</header>
<header class="sub-header">
...
</header>
<div id="main-content-bottom" class="main-content">
<section>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
<br/>
...
</section>
<figure>
<img src="#">
</figure>
</div>
</div>
<footer class="container-footer">
</footer>
</div>
</body>
</html>
的JavaScript
$(document).ready(function() {
var topOfNav = $('#float-nav-bar').offset().top;
$(window).scroll(function() {
if ($(this).scrollTop() > topOfNav){
$('#float-nav-bar').addClass('fixed');
}
else
{
$('#float-nav-bar').removeClass('fixed');
}
});
});
CSS
#container {
width: 75%;
margin: auto;
}
h1, h2, h3{
text-align: center;
font-family: arial;
}
h1 {
font-size: 36pt;
margin: 0;
}
h1 a {
color: inherit;
text-decoration: none;
}
section {
width: 60%;
margin-top: 10px;
}
header {
margin-top: 10px;
font-size: 20pt;
color: rgb(0, 64, 135);
}
.container-footer {
height: 100px;
background-color: rgb(0, 137, 96);
margin-top: 40px;
}
.fixed {
position: fixed;
top: 0;
width: inherit;
}
#float-nav-bar {
height: 100px;
background-color: #ccc;
}
你可以嘗試爲'#float-nav-bar'像'width:250px;'而不是使用'%'設置固定寬度嗎?或者你需要使用'%'? – afrin216
我想從它的父容器繼承。無論如何,這並不能解決跳動的問題,這對我來說更加緊迫。我對寬度的東西有一些「修復」,但我想看看修復跳躍事件在哪裏首先要把我帶到哪裏。 – Gregolopolis