What steps should I take to ensure my navbar functions correctly?

I have recently designed a responsive navigation bar for my website, but I am encountering an issue. When the user reduces the browser size and tries to click the hamburger icon on the side, the navbar does not work as expected. The user needs to refresh the page in order to successfully click the hamburger icon and view the navbar in the reduced screen size. I have attempted to troubleshoot this problem without success. Can anyone provide guidance on how to ensure that the navbar functions properly when the screen is resized, eliminating the need for the user to refresh the page? Any assistance would be greatly appreciated. Below is the code snippet:

$(document).ready(function () {
  if (window.matchMedia('(max-width: 767.98px)').matches) {
    $(".navbar-toggle").click(function () {
      $(".navbar-toggle").toggleClass("cross");
      $("#navbarToggle").toggleClass("active");
      $("body").toggleClass("overflow-hidden");
    });
  }
});
header {
  -webkit-box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
  -moz-box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
  box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
  position: relative;
  z-index: 999;
  transition: 0.6s;
}
/* header fixed on top with transition */
header.fixed-top {
  -webkit-transition: .4s;
  -ms-transition: .4s;
  -o-transition: .4s;
  -moz-transition: .4s;
  transition: .4s;
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  transform: translateY(0);
  -webkit-box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
  -moz-box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
  box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
}
header .navbar-brand {
  padding: 0.5rem 0;
}

/* dropdown open on hover css start*/
.dropdown:hover > .dropdown-menu-hover {
  display: block;
}

.dropdown-menu.dropdown-menu-hover {
...

</header>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js'></script>

<script src="script.js"></script>

  </body>
</html>

Answer №1

This responsive navbar is created using only Bootstrap 4 classes.

While it may not be exactly what you're looking for, please review and use it as a reference. I've tried to replicate your design as closely as possible.

CSS Code

<style>
      header {
      -webkit-box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
      -moz-box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
      box-shadow: 0 0.05rem 1rem rgba(87, 87, 173, 0.77);
      position: relative;
      z-index: 999;
      transition: 0.6s;
    }
    /* CSS for fixing header on top with transition */
    header.fixed-top {
      -webkit-transition: .4s;
      -ms-transition: .4s;
      -o-transition: .4s;
      -moz-transition: .4s;
      transition: .4s;
      -webkit-transform: translateY(0);
      -ms-transform: translateY(0);
      transform: translateY(0);
      -webkit-box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
      -moz-box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
      box-shadow: 0 0.5rem 1rem rgba(154, 154, 197, 0.24);
    }
    header .navbar-brand {
      padding: 0.5rem 0;
    }
    
     .navbar-toggle {
        position: relative;
        background: #fff;
      }
    
    /* CSS for dropdown open on hover effect */
    .dropdown:hover > .dropdown-menu-hover {
      display: block;
      text-decoration: none;
      
    }
    
    a{
        color: darkgrey;
      }
      
    .dropdown-menu.dropdown-menu-hover {
      margin-top: 0;
      background-color:#f8f9fa!important;
      -webkit-transform: translateZ(0);
      -ms-transform: translateZ(0);
      transform: translateZ(0);
      -webkit-transform-style: preserve-3d;
      -webkit-backface-visibility: hidden;
      backface-visibility: hidden;
      -webkit-animation: .3s ease-out m-dropdown-move-up, .3s ease-out m-dropdown-move-up;
      -ms-animation: .3s ease-out m-dropdown-move-up, .3s ease-out m-dropdown-move-up;
      animation: .3s ease-out m-dropdown-move-up, .3s ease-out m-dropdown-move-up;
      border-radius: 6px !important;
    }
    
    /* Animation for dropdown */
    @-webkit-keyframes m-dropdown-move-up {
      from {
        margin-top: 10px;
      }
      to {
        margin-top: 0;
      }
    }
      @keyframes m-dropdown-move-up {
      from {
        margin-top: 10px;
      }
      to {
        margin-top: 0;
      }
    }
    @-webkit-keyframes m-dropdown-arrow-move-up {
      from {
        margin-top: 10px;
      }
      to {
        margin-top: 0;
      }
    }
    @keyframes m-dropdown-arrow-move-up {
      from {
        margin-top: 10px;
      }
      to {
        margin-top: 0;
      }
    }
      </style>

HTML code

<body>

<nav class="navbar navbar-expand-lg navbar-light bg-light" style="margin:24px 0;">
  <a class="navbar-brand" href="javascript:void(0)">Logo</a>
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navb">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navb">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Link</a>
      </li>
      <li class="nav-item">
        <div class="dropdown cursor-pointer align-items-center nav-link">
                            <a class="dropdown-toggle" id="data-nav" href="/data" data-toggle="dropdown"
                               aria-haspopup="true" aria-expanded="false">
                                Dropdown
                            </a>
                            <div class="dropdown-menu dropdown-menu-hover dropdown-menu-right shadow border-0 "
                                 aria-labelledby="data-nav">
                                <a class="dropdown-item d-flex" href="javascript:void(0)">
                                    <span>All</span>
                                </a>
                                <a class="dropdown-item d-flex" href="https://flickity.metafizzy.co/" target="blank">
                                    <span>Menu 1</span>
                                </a>
                                <a class="dropdown-item d-flex" href="javascript:void(0)">
                                    <span>Menu 2</span>
                                </a>
                                <a class="dropdown-item d-flex" href="javascript:void(0)">
                                    <span>Menu 3</span>
                                </a>
                                <a class="dropdown-item d-flex" href="javascript:void(0)">
                                    <span>Menu 4</span>
                                </a>
                                <a class="dropdown-item d-flex" href="javascript:void(0)">
                                    <span>Menu 5</span>
                                </a>
                                

                            </div>
                        </div>
      </li>
       <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Link</a>
      </li>
    </ul>
     <div class="nav-buttons">
                    <div class="nav-item">
                        <a class="btn btn-round btn-outline-primary btn-light ml-lg-3 my-sm-2 btn-sm" href="javascript:void(0)">
                            Login
                        </a>
                        <a class="btn btn-round btn-primary ml-lg-3 my-sm-2 btn-sm" href="javascript:void(0)">
                            Sign Up
                        </a>
                    </div>
              </div>
  </div>
</nav>

</body>

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

Angular: Leveraging real-time data updates to populate an Angular Material Table by subscribing to a dynamic data variable in a service

Seeking guidance on how to set up a subscription to a dynamic variable (searchData - representing search results) for use as a data source in an Angular Material Table. I have a table-datasource.ts file where I want to subscribe to the search results from ...

How to Make Client-Side Jquery Code Function in Karma Tests

I'm facing a challenging issue for which I can't seem to find a solution. In my React component, I am using jQuery to modify the classes of certain DOM nodes based on a click event. Here is a simplified version of the code: hide() { if ($(&ap ...

Utilize JavaScript to assign a value to a concealed Pardot field

Recently, I've been working on setting a hidden Pardot field within an iframe. Oddly enough, when I manually input the query selector in my Chrome console, I successfully locate the element. However, when running this code snippet (embedded in the &l ...

Unable to display routerLink within innerHTML in Angular

Within my component code, there is a field named "taggy" that contains a complete anchor tag. Initially, when I rendered it in my HTML, I only saw the contents of "taggy" displayed as text "<a href="blopp/1337">hazaa<a>". Clearly, this did not ...

Update each number in an array by appending a string within a table in an Angular component, rather than the

I have created a function that decides on a comment based on the result number added to an array and displays it in a table. However, calling this function within the template is causing a performance slowdown. Is there a way to achieve the same outcome w ...

What could be causing the Bootstrap Offset Class to malfunction?

I have been experimenting with Bootstrap 4 and encountered an issue. I am trying to adjust the layout of my web page by pushing the first column to the right side by 3 columns in the Bootstrap 12 columns Grid system, but it is not working as expected. Belo ...

Creating a Dynamic Form in Google App Engine

I need guidance on how to implement a form that communicates with the server in an AJAX manner when the submit button is clicked. The goal is to display information retrieved from the server on the same page. Specifically, I am looking for assistance on ac ...

Vue.js error: Reaching maximum call stack size due to failed data passing from parent to child component

I'm having trouble passing data from a parent component to a child component. I tried using props and returning data, but with no success. The parent component is a panel component that contains the data, while the child component is a panelBody. Thi ...

Obtaining JSON information with Svelte

I'm currently facing a mental block. My goal is to fetch JSON data using the Youtube API. The error message I am encountering is "Cannot read property 'getJSON' of undefined". Here's the code snippet I have provided: <script> ...

"Improprove your website's user experience by implementing Material UI Autocomplete with the

Recently, I experimented with the Autocomplete feature in Material UI. The focus was on adding an option when entering a new value. You can check out the demo by clicking on this link: https://codesandbox.io/s/material-demo-forked-lgeju?file=/demo.js One t ...

Is there a way to use HTML alone to center a navigation link without using CSS?

Can I center these navigation links using only HTML without needing CSS? The navigation links are currently aligned to the left and I would like to center them on the webpage. <ul class="nav nav-tabs" id="myTab" role="tablist&q ...

Tips for simultaneously submitting two forms or merging them into a single form

Trying to merge two different forms into one has proven to be quite challenging for me. The recommendation I received was to move the create method from ChargesController to OrderController, but it's not as simple as that. The Charges Form requires j ...

Managing multiple Socket.io connections upon page reload

I am currently developing a real-time application and utilizing Socket.io for its functionality. At the moment, my setup involves receiving user-posted messages through the socket server, saving this data to a MySQL database via the controller, and then b ...

Injecting multiple instances of an abstract service in Angular can be achieved using the following techniques

I am fairly new to Angular and currently trying to make sense of the code written by a more experienced developer. Please excuse me if I'm not adhering to the standard communication practices and vocabulary. There is an abstract class called abstract ...

What could be causing my bootstrap navbar button to hide the navigation items on mobile devices?

While working on creating a bootstrap navbar, I encountered a slight issue. I had referred to the code provided on this site to implement it. Everything seemed to be functioning well – the navigation items showed up, and the navbar collapsed appropriatel ...

Using JavaScript to assign one object to another object

I am facing an issue where I am trying to assign the local variable UgcItems to uploadedItems, but when I attempt to return the value, it shows as undefined. If I place the console.log inside the .getJSON function, then I get the expected value. However, t ...

Using keys with a hyphen (-) in Jquery templates

My JSON array has a key 'BASIS-TYPE'. When I try to use this value in the markup: <html> <head> <script src="/js/jquery-1.7.min.js"></script> <script src="/js/jquery-plugins/jquery.tmpl.min.js"></script> < ...

What is causing a 500 internal error in Django when using Ajax?

Can someone help me troubleshoot why I keep receiving a 500 internal error when trying to execute an Ajax function? I attempted to send the response from view.py to the Ajax function in two different ways: using JsonResponse (see 'else' section i ...

What is the process for converting/executing TypeScript into JavaScript?

Having trouble running https://github.com/airgram/airgram Encountering this warning message from the post (node:9374) Warning: To load an ES module, set "type": "module" Have already added {"type": "module"} To pa ...

Increase the loading speed of the tooltip when hovering over the Legend of the Doughnut chart

I have a similar implementation to the answer provided in this response, which I will share here for better understanding. When you hover between items in the legend and do so quickly, you may notice that the tooltip on the chart does not always display. ...