Implement JQuery UI Accordion to enhance navigation on mobile devices

I am implementing the JQuery UI Accordion to expand content on a website.

Below is the basic markup structure:

<div class="accordion">
    <h2>Heading</h2>
    <div>Content</div>
    <h2>Heading</h2>
    <div>Content</div>
    <h2>Heading</h2>
    <div>Content</div>
    <h2>Heading</h2>
    <div>Content</div>
</div>

I plan to utilize the JQuery UI Accordion for my mobile menu as well.

Here is the current navigation setup:

<!-- Desktop menu -->
<nav class="desktop">
    <ul>
        <li><a href="page-one">Page One</a></li>
        <li><a href="page-two">Page Two</a></li>
        <li><a href="page-three">Page Three</a></li>
        <li><a href="page-four">Page Four</a></li>
    </ul>
</nav>

<!-- Mobile menu -->
<nav class="mobile accordion">
    <span>Menu</span>
    <ul>
        <li><a href="page-one">Page One</a></li>
        <li><a href="page-two">Page Two</a></li>
        <li><a href="page-three">Page Three</a></li>
        <li><a href="page-four">Page Four</a></li>
    </ul>
</nav>

The menus are triggered by a basic media query:

.mobile {
    display: none;
}

@media screen and (max-width: 48em) {
    .desktop {
        display: none;
    }

    .mobile {
        display: block;
    }
}

Is there a more efficient way to handle this so that I don't have to duplicate my navigation markup?

Answer №1

If you want to achieve this with a touch of JavaScript, keep in mind that a div or span cannot be directly inserted into a ul without being wrapped in an li element first. To incorporate animations and more, refer to the following example.

<nav>
<span class="show-nav">Menu</span>
<ul class="accordion">
    <li><a href="page-one">Page One</a></li>
    <li><a href="page-two">Page Two</a></li>
    <li><a href="page-three">Page Three</a></li>
    <li><a href="page-four">Page Four</a></li>
</ul>

//CSS

.show-nav{
  display:none;
 }
@media screen and (max-width: 767px) {
  .show-nav{
    display:block;
  }
  .accordion{
    display:block;
     max-height: 0;
    overflow:hidden;
    -webkit-transition: all .3s ease-in-out;
    -moz-transition: all .3s ease-in-out;
    -o-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;   
  }
  .accordion.open{
    max-height: 500px;
  }
}

//Javascript

    $('.show-nav').click(function(){
      $('.accordion').toggleClass('open');
    })

https://codepen.io/marcosefrem/pen/aLqvrw

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

Error: The module you are trying to import from the package is not found. Please check the package path and make sure that

I encountered an issue when trying to compile a code in Reactjs. As a beginner in Reactjs, I'm struggling with this. Module not found: Error: Package path ./cjs/react.development is not exported from package /Users/mansi/letsgrowmore/to-do-list/my-rea ...

I want to display a div above an image when hovering over it

.vpbutton {padding:4px;background-color:#EFEFEF;} .userbox img{padding:8px;background-color:#EFEFEF;} .userbox img:hover{opacity:.2;} <div class="userbox"> <img src='img.png' style='height:120px;width:120px;border:1p ...

Why does my ajax call always send a GET request instead of a POST?

$.ajax({ type:"post", url: server_url, dataType: "jsonp", jsonpCallback: callback, data:req_json, cache: false, timeout: 60000, success: succeeded, error: got_error }); I've ...

Determining the screen width of mobile devices across the board

While using the Google Chrome inspector to simulate the Galaxy S5 (360px) screen, I am encountering issues with detecting the correct screen width. The CSS for 360px is being overridden by the 768px CSS instead. Is there a more effective solution to this ...

What is the best way to integrate an admin template into a website without relying on popular CMS platforms like WordPress?

Do I need to use a WordPress system in the backend in order to utilize an admin template? I've noticed that there are several sleek web admin templates available in Bootstrap. I'm interested in using one of them but unsure how to do so without re ...

HTML 4.01 character and character offset attributes

I'm curious about the purpose of the charoff attribute in HTML 4.01 Transitional. Specifically, consider a table column that contains the following cells: <td>1.30</td> <td>10</td> <td>100.2</td> <td>1000 ...

Version 2.7.2 of Chart.js now allows users to retrieve the value of a data point by

I need to retrieve the value for each point clicked on, but I am currently only able to get the value of the first line when I click on a point. The GetElementsAtEvent function provides me with an array of 3 active elements, but how can I specifically extr ...

What is the best way to make Bootstrap 3 move to a new line when there is not enough space?

I am facing an issue with a lengthy text inside a div with a class of "col-md-3". The text is so long that it appears to be overlapping with the content of another div. Adding spaces is not an option as it is an email address, and it displays fine on some ...

Fuzzy text in drop-down box on Chrome, clear on Firefox

I've encountered an issue with a dropdown menu in Google Chrome where the content appears blurry, while it displays correctly in Firefox. The problem arises when the dropdown exceeds a certain height, and I've tried setting a max-height with over ...

Pass Form ID To Function In A Dynamic Way

I have multiple forms on my webpage and I want to use the same ajax function for all of them. It currently works well for one form by fetching the id with getElementById and then passing it to the ajax function. My goal is to dynamically pass down the form ...

What is the best way to align a div right below an image that has been clicked on?

I am designing a website that features social media icons spread out horizontally across the page. When a user clicks on one of these icons, I envision a pop-up window appearing below it, displaying additional information. Here is a visual representation o ...

What methods can I utilize to increase the speed of my JavaScript animation?

Recently, I implemented a Vue component designed to loop a div over the X axis. While it functions correctly, I can't help but notice that it is consuming an excessive amount of CPU. I am interested in optimizing this animation for better performance. ...

The Chartjs bar graph fails to display data upon initial page load

My HTML page includes a bar chart created using the ChartJS library. However, upon loading the page, the chart appears empty without displaying the data I provided and without rendering the bars. It only responds after clicking on the legend - first displa ...

Different ways to create audio using GWT

Looking for ways to incorporate audio into your GWT app? I am considering creating a simple game, but it seems like there hasn't been much progress on direct audio support within GWT. This is likely due to the lack of underlying browser support, but I ...

Tips for ensuring an image remains consistently positioned alongside another element

I have a request - In my code, I have multiple h2 headings that I want to display with an image of the Orioles Logo on both sides. There are about 100 of them, and now I need to change all of them to show the Ravens Logo instead. Doing this manually would ...

unable to receive JSONP response through JQuery AJAX

How can I make a cross-domain request to invoke a REST service using Jquery ajax? Here is the code snippet I am currently using. The variables ad1, city, sp, pc, and cn are retrieved using getElementById: url='http://admin:admin@*******:***/rest/aru ...

Centralize Arabic symbol characters

Is it possible to center the symbols that by default go above characters such as مُحَمَّد? .wrapper { text-align: center; vertical-align: center; } span { font-size: 4rem; display: inline-block; padding: 2rem; margin: 0.2rem; ba ...

Exploring the possibilities of integrating Storybook/vue with SCSS

After creating a project with vue create and installing Storybook, everything was running smoothly. However, as soon as I added SCSS to one of the components, I encountered the following error: Module parse failed: Unexpected token (14:0) File was process ...

margin around the masterpage

Currently, I am utilizing a master page in ASP.NET (using Visual Studio Express 2015) and overall everything is functioning properly. However, there seems to be an unsightly space around the edges when viewing it in a browser. While not a critical issue, ...

Choosing a specific column in an HTML table using jQuery based on the text within it

I have a table with a similar structure as shown below: <table> <c:forEach ...> <tr> <td>test</td> // this is the initial value <td>random value</td> <td>random value</td&g ...