Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a drop-down menu scrollable

I am trying to implement a drop down menu in a HTML page using CSS and jquery. Here is a sample of the HTML and javascript code.

<nav id="topNav">
  <ul>
    <li><a href="#" title="Nav Link 1">Menu 1</a></li>
    <li>
      <a href="#" title="Nav Link 2">Menu 2</a>
      <ul>
        <li><a href="#" title="Sub Nav Link 1">Sub Nav Link 1</a></li>
        <li><a href="#" title="Sub Nav Link 2">Sub Nav Link 2</a></li>
        <li><a href="#" title="Sub Nav Link 3">Sub Nav Link 3</a></li>
        <li><a href="#" title="Sub Nav Link 4">Sub Nav Link 4</a></li>
        <li class="last"><a href="#" title="Sub Nav Link 5">Sub Nav Link 5</a></li>
      </ul>
    </li>
    <li>
        <a href="#" title="Nav Link 3">Menu 3</a>
    </li>
  </ul>
</nav>

Here is the Javascript code:

var nav = $("#topNav");

//add indicators and hovers to submenu parents
nav.find("li").each(function() {
    if ($(this).find("ul").length > 0) {

        $("<span>").text("^").appendTo($(this).children(":first"));

        //show subnav on hover
        $(this).click(function() {
            $(this).find("ul").stop(true, true).slideToggle();
        });
    }
});

I will be adding content to menu programmatically, and I want the dropdown menus to be scrollable when the content of the dropdown menu gets too large.

How can I do this?

like image 893
siva82kb Avatar asked Feb 24 '14 09:02

siva82kb


People also ask

Do I need a scrollable submenu?

All countries are split into three groups. Each group has a title (item in the main menu) with a scrollable submenu. If your site is based on a sidebar menu, a user has a more vertical screen space, but it is still not enough area, and a scrollable menu is required. No matter what orientation of your menu bar.

How to create a horizontal scrollable menu with CSS?

Learn how to create a horizontal scrollable menu with CSS. ... The trick to make the navbar scrollable is by using overflow:auto and white-space: nowrap: Tip: Go to our CSS Navbar Tutorial to learn more about navigation bars. Thank You For Helping Us!

How to create a hoverable dropdown menu with CSS?

Learn how to create a hoverable dropdown menu with CSS. A dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list: Create a dropdown menu that appears when the user moves the mouse over an element. Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element.

What is a dropdown menu?

A dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list: Try it Yourself » Create A Hoverable Dropdown Create a dropdown menu that appears when the user moves the mouse over an element.


2 Answers

Try this using css like,

#topNav ul li ul{
   max-height:250px;/* you can change as you need it */
   overflow:auto;/* to get scroll */
}

Demo

like image 61
Rohan Kumar Avatar answered Oct 16 '22 22:10

Rohan Kumar


There is a css property max-height you can use it:

#topNav ul ul{
   max-height:150px;  // you choice of number in pixels
   overflow-x:hidden; // hides the horizontal scroll
   overflow-y:auto;   // enables the vertical scroll
}
like image 33
Jai Avatar answered Oct 16 '22 21:10

Jai