0
我一直在開發一個項目一段時間,其中我創建了一個導航欄,它是static
,直到滾動到,此時它變成fixed
到頂部屏幕。這個導航欄效果很好,但是當它的粘性,它下面的元素向上跳躍,以填補其留下的空白。我怎樣才能使它所以這些元素保持不動,一旦導航欄的position
變化?我已經在下面列出了相關的代碼,以防萬一需要。當我粘貼導航欄時,如何保持我的其他屬性靜止
HTML:
<div id="navbar"><ul>
<li id="activePage"><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Upcoming events </a><li>
<li><a href="">Contact Us</a></li>
<li><a href="">Contribute</a></li>
</ul></div>
CSS:
#navbar {
background-color: white;
z-index: 1;
}
#navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
text-align: center;
width: 100%;
}
#navbar li {
text-align: center;
display: inline;
}
#navbar ul #activePage a {
color: #cec8c8;
}
#navbar li a {
color: black;
padding: 14px 35px;
text-decoration: none;
font-family: 'Roboto Slab';
font-size: 25px;
text-align: center;
}
JS:
$(document).ready(function(){
var elementPosition = $('#navbar').offset();
$(window).scroll(function() {
if ($(window).scrollTop() > elementPosition.top) {
$('#navbar').css('position', 'fixed').css({'top':'0','left':'0','right':'0');
} else {
$('#navbar').css('position', 'static');
}})});