1

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>
StudioTime
  • 22,603
  • 38
  • 120
  • 207
Jamie Belcher
  • 123
  • 2
  • 13

1 Answers1

1

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

Community
  • 1
  • 1
Mihai T
  • 17,254
  • 2
  • 23
  • 32
  • I love the top one, the only issue is that it doesn't seem to work for me even though I have the identical copy of the JSFiddle – Jamie Belcher Jun 29 '16 at 10:29
  • be sure to activate jQuery in your jsfiddle. plus you can post it here so i can see what's wrong – Mihai T Jun 29 '16 at 10:31
  • I pasted my entire code into the Fiddle and it works there, but not for my local environment lol this has thrown me into confusion – Jamie Belcher Jun 29 '16 at 10:38
  • inspect element and see in console if there is an error . also be sure you have the jQuery library loaded. if it works in jsFiddle then my answer is correct. you should check your local settings. .check the html and css on your local environment . be sure everything is in the right place – Mihai T Jun 29 '16 at 10:40
  • I got it to a point where It's always there, but it doesn't show and hide on click – Jamie Belcher Jun 29 '16 at 10:42
  • And the error I'm getting is Uncaught ReferenceError: $ is not defined – Jamie Belcher Jun 29 '16 at 10:48
  • that's because you don't have jQuery added to your local environment use this ` ` add it in the `head` section of your html. for more info check http://stackoverflow.com/questions/1458349/installing-jquery – Mihai T Jun 29 '16 at 10:50
  • Okay JQuery has been added and it's there, but the section still won't show and hide on click, also getting 0 errors which is strange – Jamie Belcher Jun 29 '16 at 10:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115954/discussion-between-mihai-t-and-jamie-belcher). – Mihai T Jun 29 '16 at 11:11