Challenges related to a hamburger icon menu

Trying to create a functional hamburger menu but facing some difficulty in getting it right. Here's the concept:

  1. The page layout on larger screens should resemble this:

https://i.sstatic.net/CBqMP.png

  1. Smaller screens will display a hamburger menu, like so:

https://i.sstatic.net/OpsD0.png

  1. The hamburger icon expands on click, as shown here:

https://i.sstatic.net/7B8z4.png

Proposed structure for implementation:

<div class="home-navbar">
      
      <div class="home-navbar-logo-hamburger">
        <!-- LOGO -->
        <h1 class="logo">
          <span class="text-primary"><i class="fas fa-book-open"></i> eL</span>Bi
        </h1>
        <!-- EO: LOGO -->

        <!-- Hamburger menu -->
        <div class="hamburger">
          <i id="hamburger" class="fa-2x fas fa-bars"></i>
        </div>
        <!-- EO: Hamburger menu -->
      </div>
      
      <!-- Menus-->
      <ul class="home-navbar-menu">
        <li><a href="#">Home</a></li>
        <li><a href="#">Work</a></li>
        <li><a href="#">About</a></li>
      </ul> 
      <!-- EO: Menus -->     
    </div>
</div>

In summary, for larger screens:

  • Hamburger menu icon is hidden
  • Logo and menu are visible with flex layout (row direction)

On smaller screens:

  • Hamburger icon is visible

  • Logo remains visible

  • Menu is initially invisible but with flex layout (column direction)

  • Clicking the hamburger icon reveals the menu using jQuery code:

     $(document).ready(function () {
         $("#hamburger").click(function () {
             var visible = $(".home-navbar-menu").is(":visible");
             if (!visible) {
                 $(".home-navbar-menu").show();
             } else {
                 $(".home-navbar-menu").hide();
             }
         });
    })
    

Issues encountered:

  1. Show/hide menu on expanding/shrinking screen
  2. Toggling between desktop and mobile views
  3. Functionality of opening/closing mobile menu
  4. Restoring visibility on expanding screen

Currently, the menu disappears on larger screens... Snippet provided:

... Any suggestions or guidance on refining the menu structure would be greatly appreciated.

Answer №1

The problem lies in your media query and jQuery combination.

To resolve this issue, add the following code:

@media screen and (min-width: 724px) {
  .home-navbar-menu {
        display: flex!important;
    }
}

Your existing js/jQuery code:

$(document).ready(function () {
    $("#hamburger").click(function () {
        var visible = $(".home-navbar-menu").is(":visible");
        if (!visible) {
            $(".home-navbar-menu").show();
        } else {
            $(".home-navbar-menu").hide();
        }
    });
})

The issue arises when the page switches to a smaller/mobile view, causing the menu to hide using your css. Then, when you attempt to display the menu again with the hamburger menu toggle, the jQuery script kicks in, performing a show/hide action (in this case, changing between display flex and display none).

By setting the menu to be hidden on a smaller screen, it essentially sets it as display:none. Consequently, when transitioning back to a larger screen size, the menu remains hidden.

The above media query enforces the re-display of the menu, overriding the jQuery hiding mechanism.

You can also simplify your js/jQuery logic to the following:

Improved version:

$(document).ready(function () {
    $("#hamburger").click(function () {
        $(".home-navbar-menu").toggle();
    });
})
[CSS styles here]
[HTML structure and scripts here]

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

Modifying the color of an SVG shape with a checkbox toggle

Is there a way to dynamically change the fill color of an SVG shape by clicking on a checkbox? I have managed to achieve this functionality, but when I enclose the checkbox input in a div tag, it stops working. #circles { fill: #00ffff; } #circ-toggl ...

Instantly change the default value with Ajax selection

My current setup involves using Ajax to populate a select HTML element with values from an array returned by PHP. Here is the Ajax code snippet: <script type = "text/javascript"> $(document).ready(function(){ $('#fds_categories&ap ...

Adding Proxy-Authorization Header to an ajax request

It's surprising that there isn't much information available online about this issue I'm facing. When attempting to include a proxy authorization in the header of my ajax call, I encounter some difficulties. If I send it as shown below, no er ...

Tips for arranging a menu horizontally with buttons in CSS

I am currently working with a dropdown menu that I coded using CSS, similar to the one shown at . My issue is that I have 4 menu items and I want the total width of the parent div to be divided equally among each item, regardless of the resolution. Curre ...

Fetching data from database and populating dropdown menu through Struts 2 action class with the assistance of jtable

Utilizing the jtable jQuery plugin (http://jtable.org/) in my current project has been a game-changer. It allows for the creation of AJAX based CRUD tables without the need to code HTML or JavaScript. For more details, refer to this image: The jtable cod ...

Tips for iterating through JSON Data:

Struggling with looping through JSON data retrieved from an ashx page to add values to li's in a ul. How can I effectively loop through and extract the values from the following JSON data? The format of the returned data looks like this: {"ImageCol ...

When hovering over an image, another image is revealed

<div> <a href="#"><img src="images/Informasjon_button.png" width="120px" height="120px" /></a> </div> <div> <a href="#"><img src="images/Mail_button.png" width="120px" height="120px" /></a> </div ...

Using a combination of HTML and CSS3, craft this specific geometric design

Here's a sample image demonstrating how to create this button using CSS3. I would greatly appreciate any assistance! ...

Tips on restricting dates to be equal to or earlier:

I have written a code to determine if two given dates are equal or not. The code should allow for the current date to be smaller than or equal to the provided date, but it should not allow for it to be greater. var date = '10-11-2015'; var toda ...

Extracting URLs of latest news articles from dynamic websites using web scraping technology

I've been exploring ways to extract investing news articles from a dynamic website using Python. Despite following some tutorials that worked for static websites, I've encountered difficulties in retrieving the URL to a specific article. The code ...

Discover the best way to implement aliases for embedded base 64 images within HTML code

I am trying to incorporate Base64 embedded images into my HTML file, but the image sizes are over 200k, making the base64 data part very large. To address this issue, I would like to place these images in another tag, such as a script, and attach them at ...

Challenges with handling Ajax responses in Ruby on Rails

I'm currently facing an issue with the Ajax response in my Rails application, and I'm unsure of how to troubleshoot it. Here is the code that is functioning correctly: View <div id="<%= dom_id(comment) %>_count"> <%= Like.wh ...

Getting the nth-child rule for CSS in IE9 to function properly in IE8

This code is functioning in IE9: body.country-XYZ .abc:nth-child(3) { display:none; } As :nth-child() is effective in IE9 and newer versions, what can be done to ensure it also functions on IE8? Can you suggest an alternative method for achieving th ...

What's the reason popstate does not trigger in Safari during the loading of an iframe?

When using Safari, I've noticed that if an iframe is loading and the user changes the history state by navigating back or forward, the popstate event doesn't get triggered. This results in the application state being out of sync with the window l ...

Angularjs search filter performance slows down significantly when handling large datasets

I am currently working on an AngularJS client application that displays data in a table using the dir-paginate directive from dirPagination.js. Here is the code snippet: <input type="text" ng-change="SomeTask()" ng-model="vm.searchKeyword" /> &l ...

Interpolation problem with AngularJS translation service

I've successfully implemented a ternary operator in my HTML: {{ qp.QP_TRANSFERS === '1' ? qp.QP_TRANSFER_TYPE_NAME + ' transfer' : 'No transfer' }} In addition, I am utilizing a translation service with JSON objects. He ...

Exploring the mechanics of callbacks in jQuery's Ajax functionality

Greetings fellow developers! I've embarked on a new programming project with the firm intention of writing cleaner and more easily maintainable code than ever before. However, I have encountered my old nemesis - jQuery AJAX returns. The last time I t ...

In Internet Explorer, the list items are overlapping, while functioning perfectly in all other web browsers

I've encountered a strange issue with my list item in Internet Explorer. Despite working perfectly in Chrome and other browsers, it fails to function properly in IE. I've built this application using the Vue framework and have faced various probl ...

How to easily align an image and blockquote vertically in CSS with automatic width for the blockquote

I am struggling with a layout issue involving a list of items containing images and blockquotes. My goal is to set a maximum width for the images and have the blockquotes automatically adjust their size to fit next to the images, all while keeping both ele ...

The issue of the jQuery ajax script being inadvertently reloaded is persisting even after

I've successfully integrated a bootstrap modal and included a .js file that contains the necessary Ajax function. However, I have encountered an issue where the ajax function is triggered multiple times when the load more button is clicked multiple ti ...