Designing a menu inspired by the layout of Google Plus

I am attempting to create a responsive menu that behaves similarly to the one on Google Plus. In this menu, main options are either added to or removed from the "more" dropdown as the window is resized.

Here is the current appearance of my menu:

Below is the code snippet:

// JQuery
$(document).ready(function() {
    $("a.drop-menu").click(function () {
        $('#drop-menu').toggle();
    });
});

<!-- HTML -->
<ul id="navigation">
  <li><a href="#" class="active">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Calendar</a></li>
  <li><a href="#">Forum</a></li>
  <li><a href="#">Gallery</a></li>
  <li><a href="javascript:;" class="drop-menu">More</a>
      <ul id="drop-menu">
        <li><a href="#">Contact</a></li>
        <li><a href="#">Contact</a></li>
      </ul></li>
</ul>

/* CSS */ 
#navigation {
  list-style-type: none;
  padding: 0px;
  margin: 0px;
}
#navigation li {
  display: inline-block;
}
#navigation li a:link, #navigation li a:visited, #navigation li a:active {
  display: block;
  width: 120px;
  line-height: 50px;
  text-align: center;
  font-weight: bold;
  text-decoration: none;
  background-color: #27383F;
  color: #CCC8C0;
}
#navigation li a:hover, #navigation li a.active {
  background-color: #2C3C53;
}

#drop-menu {
  display: none;
  position: absolute;
  padding: 0px;
  margin: 0px;
}
#drop-menu li {
  display: block;
}

JSFiddle Link

Currently, when resizing the browser window, the menu options collapse in this manner:

However, this image displays what I aim to achieve:

I am curious if there is a way to achieve this without utilizing media queries. More specifically, I have two questions:

  1. How can I dynamically determine whether the window size is adequate to display all li tags in the main navigation on a single line?
  2. How do I switch the placement of the li tags between the main menu and the "more" menu?

Answer №1

Instead of relying on media queries, one alternative is to utilize jQuery's $( window ).width(); function to retrieve the browser viewport width. Here's an example:

$(document).ready(function() {
    $("a.drop-menu").click(function () {
        $('#drop-menu').toggle();
    });
    if($( window ).width() < $("#navigation > li").length * (120 + 5)){
        //5px is the estimated gap between each <li>
        var html = $("#navigation > li").last().prev().html();
        $("#navigation > li").last().prev().remove();
        $("#drop-menu").append(html);
    }
    var bigger = $("#navigation > li").length + 1;
    var smaller = $("#navigation > li").length;
    $( window ).resize(function() {
      if($( window ).width() <= smaller * (120 + 5)){
          //5px is the estimated gap between each <li>
          var html = $("#navigation > li").last().prev().html();
          if(html != undefined){
              $("#navigation > li").last().prev().remove();
              $("#drop-menu").prepend("<li>"+html+"</li>");
              bigger = $("#navigation > li").length + 1;
              smaller = $("#navigation > li").length;
          }
      }
      if($( window ).width() >= bigger * (120 + 5)){
          //5px is the estimated gap between each <li>
          var html = $("#drop-menu > li").first().html();
          if(html != undefined){
              $("#drop-menu > li").first().remove();
              $("#navigation > li").last().before("<li>"+html+"</li>");
              bigger = $("#navigation > li").length + 1;
              smaller = $("#navigation > li").length;
          }
      };
    });
});

Take a look at thisFiddle for demonstration purposes. It may not be flawless, but it can serve as a helpful starting point. Hope you find it useful.

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

The printing function for the window system can cause disruptions to the layout of the table

After creating a page with a simple table that can be filled in and printed, I noticed some issues with the table formatting after printing. The intended table design was supposed to look like this: https://i.stack.imgur.com/aAk89.png However, upon print ...

Move the image inside the box without directly following the mouse cursor, but at a slightly faster pace

I am facing an issue with a Vue component that allows me to zoom in on an image and move it around the container. The problem is, when the image is zoomed in, it moves faster than the mouse due to using the scale transform. Additionally, I noticed that cl ...

Bringing in manageable RxJS operators

RxJS 5.5 has undergone a significant change and has introduced lettable operators to replace the traditional operators we were using previously. In the article, there is a note that states: Lettable operators can now be imported from rxjs/operators, bu ...

Material-UI: Creating Radio Button Groups

I have been working on a project using React and Bootstrap. I decided to switch to material-ui, which went smoothly except for the radio buttons. Below is the original code that worked well: <label> <input type={that.props.questionType} name ...

Is there a way to modify the package version with yarn, either upgrading or downgrading?

Is there a way to downgrade the version of a package using yarn's "dependencies" feature? ...

Tips for aligning a custom icon to the left in a Jquery mobile header

I have successfully implemented a custom icon in the header, but it is currently displaying in the center. How can I adjust it to be positioned on the left side instead? <div data-role="header" data-theme="a"> <h3> ...

JavaScript will not modify the content of the page

Initially, everything was working fine with this code when I was using a JavaScript window prompt to input the length. However, I decided to switch it up and use an if statement along with a dropdown window in the HTML, and now it doesn't seem to be w ...

Automatically adjust sizes with div elements

How can I automatically resize this iframe within a div? My current method is not effective. <iframe src="http://www.gamepuma.com/external.html?http://data.gamepuma.com/games/06/crash-bandicoot.swf" width="600" height="400" frameborder="0" margin ...

How do I go about updating my code for welcome messages from discord.js v12 to v13?

While watching a YouTube tutorial on welcome messages, I decided to copy the entire code. However, when I tried using this code with discord.js v13, it didn't work. Strangely enough, everything seemed to function perfectly fine with discord.js v12. In ...

Transfering information to handlebars in node.js

Being a beginner in node.js, I am currently working on making a get request in my router (index.js). After successfully obtaining the desired result (verified by logging it in console.log), I proceed to parse it into a JSON object and pass it to a render f ...

Expanding a responsive HTML background image using Javascript

I've spent countless hours grappling with this code, attempting to dynamically resize the background of HTML based on the browser window size using JavaScript. Here's what I have so far (I'm using Firefox for this): GM_addStyle('body ...

Adding a new key-value pair to a JSON data structure

Here is the JSON object that I am currently handling: { "name": "John Smith", "age": 32, "employed": true, "address": { "street": "701 First Ave.", "city": "Sunnyvale, CA 95125", "country": "United States" }, ...

Tips for ensuring a background image remains at the bottom of a webpage, covering the entire height

Is there a way to ensure all my pages have a consistent background like the one on my homepage? For instance, if you look at this page, you'll notice that the background doesn't match. Please let me know if you need me to provide the code, but yo ...

What is the best method for selecting or deselecting all checkboxes except for one using a single checkbox in angularjs

$scope.checkAll = function() { if ($scope.selectedAll) { $scope.selectedAll = true; } else { $scope.selectedAll = false; } angular.forEach($scope.MyProducts, function(item) { item.Selected = $scope.selectedAll; }); /*});*/ } <di ...

Tips on manually refreshing AngularJS view using ControllerAs syntax?

As I work on creating a user-friendly dashboard with widgets that can be sorted, docked, and floated, I encountered an issue. The controls I am using generate floating widgets as HTML at the bottom of the DOM, outside the controller scope where they were c ...

Tips for setting a uniform width for all bars in apexcharts bar column width

When working with dynamic data, the length of the data should also be variable. For instance, if the data has a length of 24, the column width should be 35px; similarly, even if the data has a length of 2, the column width should still be 35px. Apexchart ...

.ajax() triggers a page refresh upon pressing the ENTER key

Utilizing Ajax to update the database with a new folder leads to page refresh when hitting ENTER. On the form, I have onkeypress="if(event.keyCode==13) savefolder();". Below is the Javascript code where pressing enter calls savefolder function that sen ...

Is there a way to search for a sequence of bytes within a file and then substitute them with a different buffer of data?

In my current project, I am working with NodeJS to read files using the fs.readFile method which returns a buffer of the file contents. My goal is to identify a specific pattern of bytes within this buffer and replace them with either a new buffer of the ...

Fuzzy Background to Enhance Your Bootstrap 5 Offcanvas Navigation Menu

Currently utilizing Bootstrap v5.2, I am endeavoring to replicate the image displayed in the link below: Blurry Sidebar Backdrop As per the MDN Documentation, backdrop-filter is intended for blurring the background of an element. .offcanvas-backdrop { ...

Troubleshooting issues with a Node.js application on Azure App Service

Seeking assistance with deploying my first Node.js app on Azure App Service. Despite following Microsoft's guides and tutorials, my app is not functioning as expected. Although I can see my project in the Azure portal, when I attempt to access it via ...