Jamie Belcher Jamie Belcher - 5 months ago 17
CSS Question

Additional Navigation bar on click

I have a navigation bar with the section "Contracts" and I was wondering if it were possible, and how would I go about adding an additional navigation bar to expand underneath when this button is tagged, (For example, on the Apple Store site, when you click a product, this adds another bar)

Apple Navigation Bar

I can provide my entire CSS style sheet if needed! I think this will require JavaScript but I'm trying to keep it as pure CSS for now!

All help is greatly appreciated!

HTML Code: This is the Navigation HTML




<header>
<div class="title">
<img src="img/logo2.png"/>
</div>
<div class="navbar">
<ul>
<li style="float: left"><a href="#home">Home</a></li>
<li><a href="#contact">Contracts</a></li>
<li><a href="#about">About Us</a></li>
<li><a href="#other">Other</a></li>
<li> <a href="#release">Release Notes</a></li>
<li> <a target="_blank" href="http://www.phpartnership.com">Pinnacle Health Partnership</a></li>
</ul>
</div>
</header>


Creates this

My nav bar

CSS Code: My entire stylesheet

body {
background-color: #fff;
margin: 0;
font-family: Arial;
color: #333;
}

header {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
}

.navbar {
display: block;
text-align: center;
}

.navbar ul {
list-style-type: none;
padding: 0;
margin: 0;
overflow: hidden;
}

.navbar li {
display: inline-block;
vertical-align: middle;
-webkit-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-osx-font-smoothing: grayscale;
position: relative;
overflow: hidden;
}
.navbar li:before {
content: "";
position: absolute;
z-index: -1;
left: 50%;
right: 50%;
bottom: 0;
background: white;
height: 4px;
-webkit-transition-property: left, right;
transition-property: left, right;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
}
.navbar li:hover:before, navbar li:focus:before, .navbar li:active:before {
left: 0;
right: 0;
}

.navbar li a {
padding: 25px;
display: block;
height: 100%;
color: white;
text-decoration: none;
}

.title {
height: 80px;
padding: 2px;
background-color: #fff;
}

.container {
margin-top: 150px;
padding-top: 50px;
}

.home {
margin-top: 10px;
text-align: center;
padding: 40px !important;
}
.wrap {
width: 100%;
margin: 0 auto;
}
.left_col {
float: left;
width: 50%;
}
.right_col {
float: right;
width: 50%;
}
.right_col img {
width: 80%;
margin-top: 50px;
border: 2px solid black;
border-radius: 5px;
}
.left_col img {
width: 80%;
margin-top: 50px;
border: 2px solid black;
border-radius: 5px;
}


This is the JavaScript I tried to use to hide and show the div on click

<script>
$(index.php).ready(function(){
``$("#contract").click(function(){
$("<div class="contracts">").toggle();
});
});
</script>

Answer

guess you want smth like this : jsfiddle

first add jQuery to your local environment use this <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

add it in the head section of your html. for more info check how to install jQuery

added html inside the .navbar

<ul class="aditional">
     <li>test1</li>
      <li>test2</li>
      <li>test3</li>
      <li>test4</li>
</ul>

added css :

.aditional {
position:absolute;
top:100%;
width:100%;
background:#000;
display:none;
}
.aditional li {
color:#fff;
}

added js :

$('.navbar ul li:nth-child(2) a').click(function() {
          $(".aditional").slideToggle()
});

OR if you want a more responsive solution

check this :jsfiddle with target

use data-target on the li a like this

<li><a href="#contact" data-target="contracts">Contracts</a></li>
<li><a href="#about" data-target="aboutus">About Us</a></li>

added html :

 <ul class="aditional contracts">
     <li>test1</li>
     <li>test2</li>
     <li>test3</li>
     <li>test4</li>
 </ul>
 <ul class="aditional aboutus">
      <li>about</li>
      <li>about</li>
      <li>about</li>
      <li>about</li>
 </ul>

jq added :

$('.navbar ul li a').click(function() {
  $(".aditional").slideUp()
  var target = '.' + $(this).data('target');
  $(target).slideDown();
})

OR u can target the href of the li a. simply add this

<li><a href="#contract">Contracts</a></li>
<li><a href="#about">About Us</a></li>
------
<ul class="aditional" id ="contract">
....
<ul class="aditional" id ="about">
....

and js :

$('.navbar ul li a').click(function() {
$(".aditional").slideUp()
var target = $(this).attr('href');
$(target).slideDown();

})

see here jsfiddle

one of these solutions should work for you . let me know