accordion menu: stuck in my list like a trapped diver due to the overflow


I need assistance with creating a side menu. Here is the current code I have:

function opensubmenus() {
  $('#submenus').css("display", "block");
}
#menus {
  overflow-y: scroll;
  width: 150px;
  background-color: blue;
}
#submenus {
  background-color: green;
  display: none;
}
submenus ul {
  float: right;
  position: relative;
}
nav {
  overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div id='menus'>
    <ul>
      <li>
        <span onclick='opensubmenus()'>Menu 1</span>

        <ul id='submenus'>
          <li>SubMenu 1
          </li>
          <li>
            SubMenu 2
          </li>
          <li>
            SubMenu 3
          </li>
        </ul>



      </li>
      <li>
        Menu 2
      </li>
      <li>
        Menu 3
      </li>
    </ul>
  </div>
</nav>

I am facing an issue where my submenu does not appear outside of the box as desired. I would like to achieve a menu layout similar to this example:
https://i.sstatic.net/KuYsE.png It is crucial for me to maintain the "overflow-y" property due to the large number of menu items requiring a scrolling option. Can you provide guidance on how to achieve this result?

Answer №1

If you wish to keep the sub menus separate from the main menu, place them in a new <div>:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div id='menus'>
    <ul>
      <li>
        <span onclick='opensubmenus()'>Menu 1</span>

      </li>
      <li>
        Menu 2
      </li>
      <li>
        Menu 3
      </li>
    </ul>
  </div>

  <div>
    <ul id='submenus'>
      <li>SubMenu 1
      </li>
      <li>
        SubMenu 2
      </li>
      <li>
        SubMenu 3
      </li>
    </ul>
  </div>
</nav>

To align them side-by-side, use float:left; on #menus:

#menus {
  overflow-y: scroll;
  width: 150px;
  background-color: blue;
  float: left;
}

#submenus {
  background-color: green;
  display: none;
}

submenus, ul {
  float: right;
  position: relative;
}

nav {
  overflow: hidden;
}

Answer №2

You have the ability to modify your CSS and apply an absolute positioning technique to the submenu:

function displaySubmenus() {
  $('#submenus').css("display", "block");
}
#menus {
  position:relative;
  display:inline-block;
}
.main {
  overflow-y: scroll;
  margin:0;
  width: 150px;
  background-color: blue;
}
#submenus {
  position:absolute;
  left:100%;
  top:0;
  background-color: green;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div id='menus'>
    <ul class="main">
      <li>
        <span onclick='displaySubmenus()'>Menu 1</span>
        <ul id='submenus'>
          <li>SubMenu 1
          </li>
          <li>
            SubMenu 2
          </li>
          <li>
            SubMenu 3
          </li>
        </ul>
      </li>
      <li>
        Menu 2
      </li>
      <li>
        Menu 3
      </li>
    </ul>
  </div>
</nav>


Bonus -- Efficiently Handling Multiple SubMenus

$('span').click(function(){
  var col = $(this).data('color');
  $('.submenus').hide();
  $('span').css('background', '');
  $(this).css('background', col);
  $(this).next('.submenus').show().css('background', col);
})
#menus {
  position:relative;
  display:inline-block;
}
.main {
  overflow-y: scroll;
  margin:0;
  width: 150px;
  background-color: blue;
}
.submenus {
  position:absolute;
  left:100%;
  top:0;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div id='menus'>
    <ul class="main">
      <li>
        <span data-color="red">Menu 1</span>
        <ul class='submenus'>
          <li>SubMenu 1
          </li>
          <li>
            SubMenu 2
          </li>
          <li>
            SubMenu 3
          </li>
        </ul>
      </li>
      <li>
        <span data-color="yellow">Menu 2</span>
        <ul class='submenus'>
          <li>SubMenu 21
          </li>
          <li>
            SubMenu 22
          </li>
          <li>
            SubMenu 23
          </li>
        </ul>
      </li>
      <li>
        <span data-color="green">Menu 3</span>
        <ul class='submenus'>
          <li>SubMenu 31
          </li>
          <li>
            SubMenu 32
          </li>
          <li>
            SubMenu 33
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

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

What is the reason behind the optional assignment of "window.$ =" in Vue.js imports, but mandatory for jQuery in JavaScript files?

There is an interesting behavior I've observed when using the "require" function with Vue versus jQuery. With Vue, I have found that the following statement structures work seamlessly in my application: window.Vue = require('vue'); window.$ ...

Is there a way to determine if a table cell contains overflowing text compared to another cell?

https://i.sstatic.net/IYafA.png Is there a way to detect if the content of the fourth td overflows from the second td? I am looking for a method to determine which td has overflowed text and identify which td's text is overflowing. What approach ca ...

Is there a way to extract rows from a React MUI DataGrid that are identical to how they are displayed, including any filtering and sorting applied?

My goal is to make a selected row move up and down on arrow clicks, and in order to achieve this, I need to retrieve rows from the MUI DataGrid. I am using the useGridApiRef hook to do so, ensuring that the rows are filtered and sorted accordingly to match ...

Exploring the functionalities of objects in Typescript through the use of the for...in loop

After successfully converting the JavaScript function to Typescript, I encountered an issue with the line if (!(key * key) in frequencyCounter2) {. The error message displayed was: "The left-hand side of an 'in' expression must be of type &a ...

Breaking down a statement into individual elements within an array, incorporating keys alongside the elements within the array

let condition = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]&gt;[ARID] || [DOB]&gt;bbb&&[DOS]&gt;=[ARID]&&[DOS]&lt;[Gender]&&[66642]&lt;=cccc&&[66642] I ...

Limiting the DatePicker in React JS to only display the current year: Tips and Tricks!

I'm currently implementing the KeyboardDatePicker component in my React application to allow users to choose a travel date. However, I am looking to restrict the date selection to only the current year. This means that users should not be able to pick ...

Set the cookie to expire in 5 minutes using PHP, JavaScript, or jQuery

Is there a way to set a cookie in PHP that expires in 5 minutes? I am comfortable with the setcookie() function, but unsure about how to set the expiration time. Any explanation would be greatly appreciated. Could someone please guide me on how to achieve ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

Conceal elements that are overflowing slightly

I need help coming up with a purely CSS solution to hide div 3, which has extended beyond its container. Look at the picture linked below for reference. https://i.stack.imgur.com/isfvU.jpg ...

Utilizing ng-repeat to display a list of all items organized by various categories in rows

Currently, I have a messy code that prints out items from a list and desperately needs refactoring. For example, if we have an array of objects like this: var items = [{name: "toaster", price: 10, category: "appliance"},{name: "spade", price: 5, category ...

How can one retrieve an element based on its attribute?

Below is a custom tag/directive that I am using: <tile class="ng-scope gridster-item" tilevalue="1" gridster-item="tile" row="0" col = "0" ng-repeat="tile in selectedTiles"> </tile> I'm trying to figure out how to set focus on this eleme ...

JavaScript Time and Amount Formatting

My issue involves the formatting of returned data. I am looking to compile all values into a list with adjustments made to the time format and fare amount as indicated. Specifically, I need to remove commas from fare amounts and AM/PM from departure and ar ...

What causes the generation of an outdated object when utilizing the let and new keywords to create a new object within a service function?

Hey there, looking for a basic auth authentication service in angular2? Here's the issue I'm facing: When a user logs in for the first time, everything works smoothly. However, if they try to log in with a different account for the second time, ...

Encountering the error 'node' getProperty of undefined while trying to retrieve data from an array stored in my state variable

Hello, I am currently developing an app that retrieves images from Instagram using axios. I have successfully stored the image files in an array named 'posts' within my state. Looping through this array to display each image is not an issue for m ...

Screen rendering can be a challenging task

As I dive into learning how to use THREE.js for creating browser games, I've encountered a roadblock. Every time I attempt to import a model using the code, the screen just renders black. Surprisingly, I've been smoothly coding and running it in ...

Error loading advertising box within the list item due to nan issue

After adding an advertising image, my code seems to show a NaN error. Can someone please help me identify the source of this error and suggest how I can fix it? Here is a snippet of my code in HTML: <div> <ul class="list-groupJournalList&qu ...

Operating on a duplicate of the array is necessary for mapping an array of objects to function properly

I'm starting to uncover a mysterious aspect of Javascript that has eluded me thus far. Recently, I've been pulling an array of objects from a database using Sequelize. This array is quite intricate, with several associations included. Here' ...

Utilize data from a dynamically loaded component within a parent component in Vue.js

In my Vuejs application, I've implemented a wizard-type flow with dynamically loaded components. The parent component contains the Save button, while the child components have all the text fields. Upon clicking the Save button, I need to achieve two m ...

Tips on dividing or forming an array based on a duplicate key that repeats multiple times

const test = [{a:1},{b:2},{c:3},{d:4},{a:5},{b:6},{c:7},{a:8},{c:9}] To achieve the desired output where a new array is created on repeat of key "a," follow the format below: [{a:1},{b:2},{c:3},{d:4}] [{a:5},{b:6},{c:7}] [{a:8},{c:9}] ...

checkbox-activated navigation bar

Struggling to implement a Nav-drawer that should open when a checkbox is selected, everything seems to be set up correctly but the issue arises when it comes to actually opening it. Here is the code snippet: <div class="leftside"> <input typ ...