2016-11-21 319 views
0

以下代碼是我的css文件。我在這裏想要做的是當我沒有選擇導航按鈕<a href="#navbar-more-show">時隱藏<div class="container navbar-more visible-xs">。但是,當我選擇導航按鈕時,容器導航欄將出現。這裏的問題是我不確定如何在默認情況下隱藏div類,它會一直顯示在我的頁面上,當我點擊一個導航按鈕時,我想讓它出現。Javascript隱藏顯示div

$(document).ready(function() { 
 
     $('a[href="#navbar-more-show"], .navbar-more-overlay').on('click', function(event) { 
 
     event.preventDefault(); 
 
     $('body').toggleClass('navbar-more-show'); 
 

 
     if ($('body').hasClass('navbar-more-show')) { 
 

 
      $('a[href="#navbar-more-show"]').closest('li').addClass('active'); 
 
     } else { 
 
      $('a[href="#navbar-more-show"]').closest('li').removeClass('active'); 
 
     } 
 
     return false; 
 
     }); 
 
    });
body.navbar-more-show { 
 
    overflow: hidden; 
 
} 
 
.animate { 
 
    -webkit-transition: all 0.3s ease-in-out; 
 
    -moz-transition: all 0.3s ease-in-out; 
 
    -o-transition: all 0.3s ease-in-out; 
 
    -ms-transition: all 0.3s ease-in-out; 
 
    transition: all 0.3s ease-in-out; 
 
} 
 
.navbar { 
 
    height: calc(100%); 
 
    max-height: 300px; 
 
    -webkit-transform: translate(0px, calc(-100% + 69px)); 
 
    transform: translate(0px, calc(-100% + 595px)); 
 
} 
 
.navbar .container:not(.navbar-more) { 
 
    padding: 0px; 
 
} 
 
.navbar-more-overlay { 
 
    background-color: rgba(102, 102, 102, 0.55); 
 
    display: none; 
 
    height: 100%; 
 
    left: 0px; 
 
    position: fixed; 
 
    top: 0px; 
 
    width: 100%; 
 
    z-index: 1029; 
 
} 
 
.navbar-more-show > .navbar-more-overlay { 
 
    display: block; 
 
} 
 
.navbar-more-show > .navbar { 
 
    -webkit-transform: translate(0px, 0px); 
 
    transform: translate(0px, 295px); 
 
} 
 
.navbar-nav.mobile-bar { 
 
    list-style: none; 
 
    -ms-box-orient: horizontal; 
 
    display: -webkit-box; 
 
    display: -moz-box; 
 
    display: -ms-flexbox; 
 
    display: -moz-flex; 
 
    display: -webkit-flex; 
 
    display: flex; 
 
    -webkit-justify-content: space-around; 
 
    justify-content: space-around; 
 
    -webkit-flex-flow: row wrap; 
 
    flex-flow: row wrap; 
 
    -webkit-align-items: stretch; 
 
    align-items: stretch; 
 
    margin: 0px 0px; 
 
} 
 
.navbar-nav.mobile-bar > li { 
 
    -webkit-flex-grow: 1; 
 
    flex-grow: 1; 
 
    text-align: center; 
 
} 
 
.navbar-nav.mobile-bar > li > a > span.menu-icon { 
 
    display: block; 
 
    font-size: 1.8em; 
 
} 
 
.navbar-more { 
 
    background-color: rgb(255, 255, 255); 
 
    height: calc(100% - 69px); 
 
    overflow: auto; 
 
} 
 
.navbar-more .navbar-form { 
 
    border-width: 0px; 
 
} 
 
.navbar-more .navbar-nav > li > a { 
 
    color: rgb(64, 64, 64); 
 
} 
 
.navbar-more > .navbar-nav > li > a > span.menu-icon { 
 
    margin-left: 10px; 
 
    margin-right: 10px; 
 
} 
 
@media (min-width: 768px) { 
 
    .navbar { 
 
    height: auto; 
 
    -webkit-transform: translate(0px, 0px); 
 
    transform: translate(0px, 0px); 
 
    } 
 
    .navbar-nav.mobile-bar { 
 
    display: block; 
 
    max-height: 64px; 
 
    margin: 0px -.; 
 
    } 
 
    .navbar-nav.mobile-bar > li > a > span.menu-icon { 
 
    display: none; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="navbar-more-overlay"></div> 
 
<nav class="navbar navbar-inverse navbar-fixed-top animate"> 
 
    <div class="container navbar-more visible-xs"> 
 
    <form class="navbar-form navbar-left" role="search"> 
 
     <div class="form-group"> 
 
     <div class="input-group"> 
 
      <input type="text" class="form-control" placeholder="Search for..."> 
 
      <span class="input-group-btn"> 
 
    \t \t \t \t \t \t \t <button class="btn btn-default" type="submit">Submit</button> 
 
    \t \t \t \t \t \t </span> 
 
     </div> 
 
     </div> 
 
    </form> 
 
    <ul class="nav navbar-nav"> 
 
     <li> 
 
     <a href="#"> 
 
      <span class="menu-icon fa fa-picture-o"></span> 
 
      Photos 
 
     </a> 
 
     </li> 
 
     <li> 
 
     <a href="#"> 
 
      <span class="menu-icon fa fa-bell-o"></span> 
 
      Reservations 
 
     </a> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
    <div class="container"> 
 
    <div class="navbar-header hidden-xs"> 
 
     <a class="navbar-brand" href="#">Brand</a> 
 
    </div> 
 

 
    <ul class="nav navbar-nav navbar-right mobile-bar"> 
 
     <li> 
 
     <a href="#"> 
 
      <span class="menu-icon fa fa-home"></span> 
 
      Home 
 
     </a> 
 
     </li> 
 
     <li> 
 
     <a href="#"> 
 
      <span class="menu-icon fa fa-info"></span> 
 
      <span class="hidden-xs">About the Boat</span> 
 
      <span class="visible-xs">About</span> 
 
     </a> 
 
     </li> 
 

 
     </li> 
 

 
     <li> 
 
     <a href="#"> 
 
      <span class="menu-icon fa fa-phone"></span> \t 
 
      <span class="hidden-xs">Contact Us</span> 
 
      <span class="visible-xs">Contact</span> 
 
     </a> 
 
     </li> 
 
     <li class="visible-xs"> 
 
     <a href="#navbar-more-show"> 
 
      <span class="menu-icon fa fa-bars"></span> 
 
      More 
 
     </a> 
 
     </li> 
 
    </ul> 
 
    </div> 
 
</nav>

滾動至底部查看內容的結果!

+0

這可能是巨大的,如果你也添加了一個[jsfiddle](https://jsfiddle.net) – M98

回答

0

像你需要添加代碼隱藏要素入手,您可以使用以下這些方法

$('#YOURDIV').show() // Show your div like display:block; 
$('#YOURDIV').hide() // Hide your div like display:none; 
0

從你的描述,它只是聽起來:

$(document).ready(function() { 
    $(".container.navbar-more.visible-xs").hide(); 
    // your other code... 
} 
+0

im實際上是在默認情況下試圖隱藏#container navbar-more visible-xs。這仍然顯示導航欄,即使我沒有點擊#navbar多顯示 – stackanon

+0

更新我的回答 – Joe

+0

不幸的是,這並沒有工作 – stackanon