Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery solution for keeping Dropdown submenu inside screen

I am using bootstrap for my dropdown menu. But it has a problem,

If My dropdown menu have multilevel submenu then it shows off the screen, and bottom scroll bar appear.

How do I keep my submenu inside the screen, I need a jQuery solution.

Check my Screenshot

dropdown


Here is the playground:

http://jsfiddle.net/howtoplease/svLKN/


Here is dropdown HTML

<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
      <div class="container-fluid">
        <div class="nav-collapse">
          <ul class="nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li class="dropdown">
              <a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
              <ul class="dropdown-menu">
                <li>
                    <a href="#">2-level Dropdown <i class="icon-arrow-right"></i></a>
                    <ul class="dropdown-menu sub-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li class="divider"></li>
                        <li class="nav-header">Nav header</li>
                        <li><a href="#">Separated link</a></li>
                        <li><a href="#">One more separated link</a></li>
                    </ul>
                </li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li class="divider"></li>
                <li class="nav-header">Nav header</li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>         
        </div><!-- /.nav-collapse -->
      </div>
  </div>
</div>
like image 993
user007 Avatar asked Feb 16 '23 17:02

user007


1 Answers

How about this: It checks to see if the sub menu is going to overflow and modifies it's position by the width of the sub menu so it's on the other side

$('.sub-menu').parent().hover(function() {
    var menu = $(this).find("ul");
    var menupos = $(menu).offset();

    if (menupos.left + menu.width() > $(window).width()) {
        var newpos = -$(menu).width();
        menu.css({ left: newpos });    
    }
});

http://jsfiddle.net/svLKN/4/

like image 88
Beno Avatar answered Feb 18 '23 10:02

Beno