2016-03-13 130 views
0

所以,這是我迄今爲止所做的,當我的菜單被響應時固定到頂部,但是一旦我開始向下滾動它就會消失,並且當我回到全屏時菜單保持fixed.I需要幫助,我的js代碼navbar在響應時固定到頂部

$(window).resize(function() { 
 
     var w = $(window).width(); 
 
     menu = $('nav'); 
 
     if (w < 480) { 
 
      menu.addClass('navbar-fixed-top'); 
 
     } 
 
    }); 
 
     
 
}); 
 

 
$(function() { 
 
    var pull  = $('#pull'); 
 
     menu  = $('nav ul'); 
 
     menuHeight = menu.height(); 
 
    
 
    $(pull).on('click', function(e) { 
 
     e.preventDefault(); 
 
     menu.slideToggle(); 
 
    }); 
 
    
 
    $(window).resize(function(){ 
 
    var w = $(window).width(); 
 
    if(w > 480 && menu.is(':hidden')) { 
 
     menu.removeAttr('style'); 
 
    } 
 
}); 
 
});
.navbar-default{ 
 
    background-color: #b104aa; 
 
    border: none; 
 
    border-radius: 0; 
 
    max-width: 100%; 
 
    height: 45px; 
 
    margin-bottom: 0; 
 
} 
 
.navbar-default .navbar-nav>li>a{ 
 
    color: #ffffff; 
 
    font-weight: 300; 
 
    font-size: 20px; 
 
    margin-left: 50px; 
 
} 
 
.navbar-default .navbar-nav>li>a:hover{ 
 
    background-color: #8a0284; 
 
    color: #ffffff; 
 
} 
 
ul.nav.navbar-nav li:first-of-type{ 
 
    margin-left: -50px; 
 
} 
 
.clearfix:before, 
 
.clearfix:after { 
 
    content: " "; 
 
    display: table; 
 
} 
 
.clearfix:after { 
 
    clear: both; 
 
} 
 
.clearfix { 
 
    *zoom: 1; 
 
} 
 

 

 
@media screen and (max-width: 480px){ 
 
    .navbar-default .navbar-nav>li>a{ 
 
     color: #ffffff; 
 
     font-weight: 300; 
 
     font-size: 20px; 
 
     margin-left: 0px; 
 
     z-index: 10; 
 
     background-color: #b104aa; 
 
     width: 100%; 
 
    } 
 
    ul.nav.navbar-nav li:first-of-type{ 
 
    margin-left: 0px; 
 
    width: 100%; 
 
} 
 
nav div#pull { 
 
     display: block; 
 
     background-color: #b104aa; 
 
     width: 100%; 
 
     position: relative; 
 
     z-index: 10; 
 
     color: #ffffff; 
 
     font-size: 15px; 
 
     font-weight: 700; 
 
     margin-top: 0; 
 
    } 
 
    nav div#pull:after { 
 
     content:""; 
 
     background: url('nav-icon.png') no-repeat; 
 
     width: 30px; 
 
     height: 30px; 
 
     display: inline-block; 
 
    } 
 
}
<nav class="navbar navbar-default clearfix"> 
 
       <div class="container"> 
 
        <ul class="nav navbar-nav clearfix"> 
 
         <li><a href="#home">Kategorija 1</a></li> 
 
         <li><a href="#home">Kategorija 2</a></li> 
 
         <li><a href="#home">Kategorija 3</a></li> 
 
         <li><a href="#home">Kategorija 4</a></li> 
 
         <li><a href="#home">Kategorija 5</a></li> 
 
         <li><a href="#home">Kategorija 6</a></li> 
 
        </ul> 
 
        <ul class="nav navbar-nav navbar-right"> 
 
         <li><a href="#search"><span class="glyphicon glyphicon-search"></span></a></li> 
 
        </ul> 
 
        <div id="pull">Menu</div> 
 
       </div> 
 
      </nav>

+0

您是否嘗試過[mediaquery](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries)或特定的'@media(max-width:600px)' – Zamboney

+0

我有mediaquery for 480像素,它是固定的,當我調整大小,但當我點擊菜單拉起它的消除者 – purpulerain

+0

,所以你想拉上點擊只在移動(又名480px寬度) – Zamboney

回答

0

如果你只想導航欄,以保持固定,你需要添加.navbar-fixed-top類:

<nav class="navbar navbar-default navbar-fixed-top"> 
    <div class="container"> 
    ... 
    </div> 
</nav> 
+0

我不希望它被修復時,它不響應。 – purpulerain