1. 程式人生 > >boostrap頂部導航條實現多級選單,點選後上一級選單保留狀態

boostrap頂部導航條實現多級選單,點選後上一級選單保留狀態

html:

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<style>
            body {
                padding-top: 50px;
            }
            .navbar-template {
                padding: 40px 15px;
            }

        </style>
        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">NavBar</a>
                </div>
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Home</a></li>
                        <li><a href="https://github.com/fontenele/bootstrap-navbar-dropdowns" target="_blank">GitHub</a></li>
                        <li>
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu 1 <b class="caret"></b></a>

                            <ul class="dropdown-menu">
                                <li><a href="#">Action [Menu 1.1]</a></li>
                                <li><a href="#">Another action [Menu 1.1]</a></li>
                                <li><a href="#">Something else here [Menu 1.1]</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link [Menu 1.1]</a></li>
                                <li class="divider"></li>
                                <li><a href="#">One more separated link [Menu 1.1]</a></li>
                                <li>
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 1.1] <b class="caret"></b></a>

                                    <ul class="dropdown-menu">
                                        <li><a href="#">Action [Menu 1.2]</a></li>
                                        <li>
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 1.2] <b class="caret"></b></a>

                                            <ul class="dropdown-menu">
                                                <li>
                                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 1.3] <b class="caret"></b></a>

                                                    <ul class="dropdown-menu">
                                                        <li><a href="#">Action [Menu 1.4]</a></li>
                                                        <li><a href="#">Another action [Menu 1.4]</a></li>
                                                        <li><a href="#">Something else here [Menu 1.4]</a></li>
                                                        <li class="divider"></li>
                                                        <li><a href="#">Separated link [Menu 1.4]</a></li>
                                                        <li class="divider"></li>
                                                        <li><a href="#">One more separated link [Menu 1.4]</a></li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu 2 <b class="caret"></b></a>

                            <ul class="dropdown-menu">
                                <li><a href="#">Action [Menu 2.1]</a></li>
                                <li><a href="#">Another action [Menu 2.1]</a></li>
                                <li><a href="#">Something else here [Menu 2.1]</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link [Menu 2.1]</a></li>
                                <li class="divider"></li>
                                <li><a href="#">One more separated link [Menu 2.1]</a></li>
                                <li>
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 2.1] <b class="caret"></b></a>

                                    <ul class="dropdown-menu">
                                        <li><a href="#">Action [Menu 2.2]</a></li>
                                        <li><a href="#">Another action [Menu 2.2]</a></li>
                                        <li><a href="#">Something else here [Menu 2.2]</a></li>
                                        <li class="divider"></li>
                                        <li><a href="#">Separated link [Menu 2.2]</a></li>
                                        <li class="divider"></li>
                                        <li>
                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 2.2] <b class="caret"></b></a>

                                            <ul class="dropdown-menu">
                                                <li>
                                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown [Menu 2.3] <b class="caret"></b></a>

                                                    <ul class="dropdown-menu">
                                                        <li><a href="#">Action [Menu 2.4]</a></li>
                                                        <li><a href="#">Another action [Menu 2.4]</a></li>
                                                        <li><a href="#">Something else here [Menu 2.4]</a></li>
                                                        <li class="divider"></li>
                                                        <li><a href="#">Separated link [Menu 2.4]</a></li>
                                                        <li class="divider"></li>
                                                        <li><a href="#">One more separated link [Menu 2.4]</a></li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </div>

        <div class="container">

            <div class="navbar-template text-center">
                <h1>Bootstrap NavBar (Updated: 15 Nov 2016)</h1>
                <p class="lead text-info">NavBar with too many childs.</p>
            </div>

        </div><!-- /.container -->

js:

$(document).ready(function() {
    $('.navbar a.dropdown-toggle').on('click', function(e) {
        var $el = $(this);
        var $parent = $(this).offsetParent(".dropdown-menu");
        $(this).parent("li").toggleClass('open');


        if(!$parent.parent().hasClass('nav')) {
            $el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4});
        }


        $('.nav li.open').not($(this).parents("li")).removeClass("open");


        return false;
    });
});