Combine two distinct menus to create a unified menu

I currently have two separate menus positioned in different areas on a webpage structured like this:

<div class="TopNav">
<ul>
    <li><a href="">one</a></li>
    <li><a href="">two</a></li>
    <li><a href="">three</a></li>
</ul>
</div>

<div class="LowerNav">
 <ul>
  <li><a href="">Item 1</a></li>
  <li><a href="">Item 2</a></li>  
  <li><a href="">Item 3</a></li>
 </ul>
</div>

Is there a way to merge both navigation menus into a full-width style toggle drop-down when the screen width is less than 768 pixels?

So they would transform into:

<div class="BothNav">
 <ul>
  <li><a href="">one</a></li>
  <li><a href="">two</a></li>
  <li><a href="">three</a></li>
  <li><a href="">Item 1</a></li>
  <li><a href="">Item 2</a></li>  
  <li><a href="">Item 3</a></li>
 </ul>
</div>

Answer №1

Experience the LIVE DEMO:

var $LowerNavLI = $('.LowerNav li'),
    $TopNav = $('.TopNav');    
function resizeNavigation(){  
  var mobileView = window.innerWidth<768;
  $LowerNavLI.appendTo((mobileView?".TopNav":".LowerNav")+' ul');
  $TopNav[mobileView?"addClass":"removeClass"]('BothNav');
}    
resizeNavigation();
$(window).resize(resizeNavigation); 

...which showcases:

LIVE DEMO

var $LowerNavLI = $('.LowerNav li');

function resizeNavigation(){

  var winWidth = window.innerWidth;
  var isAppended = false;
  if(winWidth < 768 && !isAppended){
    isAppended = true;
    $LowerNavLI.appendTo('.TopNav ul');
    $('.TopNav').addClass('BothNav');
  }else{
    isAppended = false;
    $LowerNavLI.appendTo('.LowerNav ul');
    $('.TopNav').removeClass('BothNav');
  }

}

resizeNavigation();

$(window).resize(function(){
  resizeNavigation();
});

Answer №2

Using jquery, you have the option to implement the following code snippet:-

Code Example on Fiddle

if($(window).width() < 768)
{
   $('.TopNav ul').append($('.LowerNav ul li')
     .unwrap()).parent()
     .removeClass("TopNav")
     .addClass("BothNav");
    $('.LowerNav').remove();
}

Answer №3

There are a few different approaches you could take in this situation. One option is to create three separate navigation menus - one for the top, one for the bottom, and one for mobile - and then hide the ones that should not be visible.

For example:

<div class="TopNav hideOnPhone">
 <ul>...</ul>
</div>
<div class="LowerNav hideOnPhone">
 <ul>...</ul>
</div>
<div class="mobileNav hideOnDesktop">
 <ul>...</ul>
</div>

@media screen and (max-width: 767px) {
 .hideOnPhone {
   display:none;
  }
}

@media screen and (min-width: 768px) {
 .hideOnDesktop{
   display:none;
  }
}

Alternatively, if you prefer to handle this on the server side, you can use a PHP script like mobile_detect http://code.google.com/p/php-mobile-detect/

Here's an example using PHP:

<?php
 include 'Mobile_Detect.php';
 $detect = new Mobile_Detect();

 if ($detect->isMobile()) { ?>

 <div class="TopNav">
  <ul>...</ul>
 </div>
 <div class="LowerNav">
  <ul>...</ul>
 </div>

<?php } else { ?>

 <div class="mobileNav">
  <ul>...</ul>
 </div>

<?php } ?>

There have already been two answers posted using jQuery which may also suit your needs. :)

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

Using the typed plugin in jQuery to create a string of text that includes an HTML hyperlink

I am currently utilizing the typed library (https://github.com/mattboldt/typed.js/) and facing an issue with adding a link within the string. Directly inserting HTML into the string results in it breaking. Here is the code snippet I am working on: $(func ...

Include a "Click for more" and "Click to collapse" button on all thumbnails, not limited to just one

I am struggling to make the toggle script below work for all thumbnails in the gallery, not just the first thumbnail. Each card is filled with paragraphs, bullets, and links, making the page overwhelming. That's why I need the "Read More" and "Read Le ...

Show the components of an associative array with a click of a button

I have a total of four div elements and array elements. My goal is to link these two components together effectively. Specifically, when a user clicks on the first div element with the class .news, I want to display the values of the first element in the ...

Utilizing JQuery UI autocomplete for dynamically loaded textbox using AJAX

<div id='toolbox'> <div class='placeholder'></div> </div> In my project, I am using a click event to dynamically load a text box into the placeholder. $('#toolbox .placeholder').load('http:// ...

Exploring ways to tailor regex for tel input type in HTML5

Here is the current Regex pattern I am using: <input type="tel" name="phone" value="" placeholder="Phone Number" title="019XXXXXXX" required pattern="[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{4}"> Currently, this forces users to enter exactly 12 digits to pass ...

I am attempting to adjust the color of the active tab, but I seem to be encountering issues in my code. Can anyone help me

The currently active tab should change along with the text inside the box, but it's not working as expected. I'm struggling to find out why. Here is a fiddle to demonstrate my progress so far: var btn1 = document.getElementById("btn1"); va ...

Is there a more efficient method to choose from various potential attribute values?

Can we use jQuery to select by multiple possible attribute values without using a comma separated list of selectors? Instead of: #list1 > option[value="1"], #list1 > option[value="2"], etc Is there a way to do something like this: #list1 > opt ...

If Node doesn't receive a response, the Ajax request will automatically restart

Presented here is a single ajax request: function send(){ document.getElementById('result'); $.ajax({ method: "POST", url: "/processdata", data: mainData, success: function(res){ $("#result") ...

Adding Elements to an Array with JavaScript

I am currently working on simulating a mailbox using Materialize for the design. In my collection view, I want to add cells dynamically, but I need help with appending HTML using JavaScript. <div class="row" id="mailBox"> <div class=" ...

ending the AJAX request in servlet

I have designed a login form: <div class="top1"> <span id="book_store">Book Store</span> <div class="login_signup_form"> <input type="email" id="email_id" placeholder="Email" name="email" /> <input ...

Incorporating a discreet progress bar into traditional file uploading processes

Have you noticed the trend of new, advanced file uploaders, many of which are Flash-based like SWFUpload? These uploaders can display a progress bar while files are being uploaded, making it much easier for users with shaky or low-bandwidth connections. H ...

I prefer using HTML over Jade in my MEAN stack development - how can I accomplish this?

Currently in the process of building an application using the MEAN stack, but I'm facing some challenges with 'Jade'. I constantly rely on an HTML to Jade translator and overall prefer working directly in HTML. Is there a way to avoid using ...

Switch the CSS class when clicking on a responsive table within a table

I am looking to update the background color of a row within a table when a tr is clicked. Below is the code for the table: <div class="table-responsive"> <table class="table table-hover"> <thead> ...

Can you use a lightbox to showcase multiple images while only displaying one image at a time to trigger them?

Currently, I am utilizing Lightbox in order to showcase photos. For example, I have divided the photos into two categories: "work" and "vacation". To implement this, I would follow this code snippet... <a href="images/image-1.jpg" rel="lightbox[work]"& ...

Using JavaScript to implement CSS3 with multiple background images

Is there a way to programmatically define multiple background images in CSS3 using JavaScript? While the common approach would be: var element = document.createElement( 'div' ); element.style.backgroundImage = "url('a.png') 0 100%, ur ...

Activate the active class on the icon when hovering over a dynamically generated span using Vue

Is it possible to activate multiple music notes when hovering over one? For example, if I hover over music note 2, both note 1 and note 2 should get active classes. And if I hover over note 3, then all three spans/icons should become active. I have succes ...

What could be the reason behind the ineffectiveness of my recently acquired Google Maps API key

For years, I had a function working with the Google API flawlessly. However, after obtaining a new API key, everything seems to have gone haywire. The issue is that there is no output after the `alert(address)` line because the code from `geocoder.geocod ...

Is it possible to switch the summernote editor between airmode and toolbar mode?

Currently, I am working on creating a report editor that displays only one toolbar when multiple summernote WYSIWYG editor sections are used. My solution involves having the first section as a full editor and the other section in airmode. Below is the HTM ...

Use jQuery to retrieve HTML content excluding comments

Take a look at the code snippet below. HTML: <div> <p>sdfsdfsfsf</p> <!--<p>testing</p>--> </div> JQUERY $(document).ready(function(){ alert($("div").html()); }); OUTPUT <p>sdfsdfsfsf</p> & ...

Choose all HTML checkboxes that have the onClick attribute linked to href

I need help with setting the onClick function on a href element that can select all checkboxes. My current code successfully selects all checkboxes, but I would like to be able to deselect them when clicking the button again. Any suggestions? HTML <in ...