Instructions for adding a secondary navigation bar to a Bootstrap navbar

I currently have a bootstrap navbar displayed on my website.

<nav class="navbar navbar-inverse" role="navigation">
   <div class="navbar-header">
      <a class="navbar-brand" href="#">Mi Thai Restaurant</a>
   </div>
   <div>
      <ul class="nav navbar-nav">
         <li class="active"><a href="#">About us</a></li>
         <li><a href="#">Other locations</a></li>
      </ul>
   </div>
</nav>

My goal is to create an additional horizontal bar that appears underneath the existing navbar when the user clicks on "Other locations". This secondary bar will contain links to different locations. What would be the most effective way to achieve this?

Answer №1

To achieve a toggle effect for the subnavigation on click, you can incorporate some jQuery into your code.

Here is a live demo showcasing this functionality: http://www.example.com/demo

HTML Structure:

<nav class="navbar navbar-inverse" role="navigation">
   <div class="navbar-header">
      <a class="navbar-brand" href="#">My Restaurant</a>
   </div>
   <div>
      <ul class="nav navbar-nav">
         <li class="active"><a href="#">Home</a></li>
         <li><a href="#" class="subnav-trigger">Services</a></li>
      </ul>
   </div>
</nav>

<nav id="subnav" class="navbar navbar-default" role="navigation">
      <ul class="nav navbar-nav">
         <li><a href="#">Link 1</a></li>
         <li><a href="#">Link 2</a></li>
         <li><a href="#">Link 3</a></li>
         <li><a href="#">Link 4</a></li>
      </ul>
</nav>

jQuery Implementation:

$( ".subnav-trigger" ).click(function() {
  $( "#subnav" ).toggle();
});

CSS Styling:

#subnav {
 display:none; 
}

Answer №2

After some searching, I stumbled upon a helpful piece of code that might assist you. Take a look at this Fiddle: http://jsfiddle.net/3zh4jxgh/. Remember to place the jQuery code in the HEAD section of your HTML file:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/ui/1.8.18/jquery-ui.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

   $("#main-menu").hover(
      function() {
         $("#submenu").animate({
         height: '+=102' /* add 51 to this number for each li class="text" in the submenu */
      }, '0' 
  );

   $(".text").css({"display": "block"
      }
   );


},

      function() {
         $("#submenu").animate({
         height: '-=102', /* add 51 to this number for each li class="text" in the submenu */
      }, '0'
  );

        $(".text").css({"display": "none"
      }
   );

   }
   );

   });


</script>  

This is the HTML snippet utilized in the Fiddle:

<div id="main-menu" onClick="menu()">MENU</div>
<div id="submenu">

    <li class="text">A title</li>
    <li class="text">Another title</li>

</div>

And here's the corresponding CSS styling:

#main-menu {
    width:100%;
    height:50px;
    color:white;
    font-family:helvetica;
    position:relative;
    background-color:black;
    text-align:center;
    line-height:50px;
    cursor:pointer;
    font-weight:bold;
    transition: 0.5s ease-in-out;
    font-size:18pt;
}

#submenu {
    position:relative;
    width:100%;
    height:0px;
    background-color:grey;
}

.text {
    position:relative;
    display:block;
    width:100%;
    height:50px;
    text-align:center;
    line-height:50px;
    font-family:helvetica;
    display:none;
    color:white;
    transition: 0.5s ease-in-out;
    font-size:16pt;
    border-bottom:1px solid #434347;
}

Just a heads up: In the Fiddle environment, I had to embed the jQuery directly into the HTML file for it to function correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a solution to manage Adblocker's blocking of Ajax URLs that contain the term "ad" without having to modify the URL?

The Adblocker is blocking URLs that contain the keyword "advertiser," like: http://localhost/project1/advertiser/users/get_user_listing/ I have numerous URLs and AJAX calls that include this keyword. Is there a way to work around this blocker? Note: Cha ...

Change the size of a custom cursor on click using React

I have customized the cursor on my react app, but I want to make it animate when the user clicks. Perhaps by decreasing its size or some other effect. The cursor is within a component that I've included in my Index.js file. I am unsure how to create a ...

Hover over buttons for a Netflix-style effect

https://i.stack.imgur.com/7SxaL.gif Is there a way to create a hover effect similar to the one shown in the gif? Currently, I am able to zoom it on hover but how can I also incorporate buttons and other details when hovering over it? This is what I have ...

"Trouble arose when I tried to incorporate additional functions into my JavaScript code; my prompt feature is not

As a beginner in HTML and JS coding, I am working on creating a page that prompts user input for a name and then executes animations using radio buttons. However, after adding functions for radio button changes, my prompt is no longer functioning properly. ...

Creating overlapping layouts with absolute positioning in Bootstrap leads to a visually appealing

I'm currently working on a layout that involves absolute positioning elements, similar to the one shown in this example: https://i.sstatic.net/TixbZ.jpg It seems like the elements are overlapping and not clearing properly. I've been looking for ...

Bootstrap 5 NavBar Toggle hamburger Menu not displaying menu items

I attempted to create a navigation bar using the navbar instructions in Bootstrap 5. I included a Toggle hamburger Menu and linked it to the list that should be displayed. However, when I decrease the screen size, the Toggle feature does not function as ex ...

Do not include the loading of <embed> items while the page is loading

I am currently working on a website project for a friend that involves incorporating a lot of large images. To ensure a smooth loading process, I have implemented a loading overlay while the background content loads. However, I have also added background m ...

Learn how to change the icon in Vuejs when the class 'active' is present

I have a bottom navigation bar with icons. How can I make it so that when the router-link has the class 'active', the icon associated with it also becomes active? By default, the first icon is active. <li> <router-link v-if="ac ...

The overflowing issue with the Nextjs Tailwind Marquee Component is causing a display

I've recently developed a unique nextjs/tailwind component. This component displays an isometric marquee that scrolls horizontally. However, I'm facing an issue where the content overflows to the right and causes the page to become horizontally s ...

Embed one div within another div in a flexible layout design

I am working on a design where I need to place a div (profile picture) inside another div (profile banner) in a way that the profile picture is slightly outside of the outer div. This may sound complicated, but I only need it to display this way on desktop ...

Sass compilation with source mapping

Is there a more efficient method to compile my sass files with sourcemap enabled other than using the command below? sass --compass --sourcemap --style compact --watch sass:css In the case of utilizing compass, 'compass watch' would be the pref ...

Guide to designing a dropdown navigation menu in GWT

Hey, I'm currently working on setting up a drop down menu using GWT and here's the layout I have in mind: | Categories | Pictures | Other | When the categories dropdown opens, my goal is to display the categories grouped in pairs. For example: ...

Steps to position a dropdown within a panel while keeping the dropdown content visible using an anchor tag

I'm struggling to display a dropdown menu within a panel without it being hidden. I was advised to use position:absolute in the dropdown's css, but that solution isn't working for me. I need the dropdown to appear on hover over the anchor t ...

Separation between inline elements with a line break

I am faced with the challenge of placing the a tag on a new line while maintaining center alignment, without being able to modify the HTML. The display:block option extends the clickable area beyond the text. See the screenshot below for the desired outco ...

Create a spectrum of vibrant colors depending on the numerical value

I'm attempting to create a function that generates rainbow colors based on a numerical value. var max = 10000; var min = 0; var val = 8890; function getColor(min, max, val) { // code to return color between red and black } Possible Colors: Re ...

Presentation with multi-directional animations

Curious to know if it's possible to create a unique slideshow that scrolls in multiple directions? The concept is to display various projects when scrolling up and down, and different images within each project when scrolling left and right. Is this i ...

Steps to obtain the precise source code of a webpage

Is there a way to download the exact source code of a webpage? I have tried using the URL method and Jsoup method, but I am not getting the precise data as seen in the actual source code. For example: <input type="image" name="ctl00$dtlAlbums$ct ...

Centered flex item with a flexbox grid underneath

Looking to create a layout with an intro section on the left side and a sidebar on the right within a container. Below the intro section, I want four divs evenly spaced like a grid. But I'm struggling with setting up this grid, possibly due to flexbo ...

Dash reveals the empty space between the border of the card and the header

I am currently facing an issue with the bootstrap cards in my Dash app. Specifically, my app is using the Flatly stylesheet and includes only one card as shown below. import dash import dash_bootstrap_components as dbc app = dash.Dash(__name__,external_st ...

Issue with jQuery .hover() not functioning as expected

The issue I'm encountering is just as described in the title. The code functions correctly on all divs except for one! $(".complete-wrapper-1").hide(); var panelHH = $(".file-select-wrapper").innerHeight; $(".files-button").hover(function(){ $(" ...