悬停在Bootstrap导航栏中的下拉子菜单

时间:2017-07-27 12:50:48

标签: jquery css twitter-bootstrap css3 laravel-5

我正在使用Bootstrap导航栏进行菜单,我在子菜单下添加了下拉子菜单,即菜单,我想在点击时打开子菜单,我使用了一些方法,但没有一个正常工作,我的代码为如下:

<nav class="navbar navbar-default navbar-static-top">
<div class="container">
    <div class="navbar-header">
        <!-- Collapsed Hamburger -->
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-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>

        <!-- Branding Image -->
        <div class="logo-container">
            <a class="navbar-brand" href="{{ url('/home') }}">
                {{-- {{ config('app.name', 'Laravel') }} --}}
                <img src="{{URL::asset('/')}}Images/group_65.jpg" class="img-responsive logo-images imgcenter">
            </a>
        </div>                    
    </div>

    <div class="collapse navbar-collapse" id="app-navbar-collapse" style="margin-left: 150px">
        <!-- Left Side Of Navbar -->
        <ul class="nav navbar-nav main-menu">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#">My Classes<span class="caret"></span></a>
                <ul class="dropdown-menu">
                    <li><a href="#">Change Class</a></li>
                    <li><a href="#">Groups</a></li>
                </ul>
            </li>
            <li><a href="">Roster</a></li>
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#">Admin<span class="caret"></span></a>
                <ul class="dropdown-menu">                       
                    <li class="dropdown-submenu">
                        <a class="" href="#">Manage Users</a>
                        <ul class="dropdown-menu">
                            <li><a href="#">All Users</a></li>
                            <li><a href="#">Admin</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Programs</a></li>
                    <li><a href="#">Documents</a></li>
                    <li class="dropdown-submenu">
                        <a class="" href="#">Gallery</a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Calendar</a></li>
                            <li><a href="#">Events</a></li>
                            <li><a href="#">Holidays</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Staff Attendance</a></li>
                    <li><a href="#">Payroll</a></li>
                    <li><a href="#">Gallery</a></li>
                    <li><a href="#">Food</a></li>

                </ul>
                <li><a href="">SoCo Help</a></li>
            </li>
        </ul>

        <!-- Right Side Of Navbar -->
        <ul class="nav navbar-nav navbar-right">
            <!-- Authentication Links -->
            @if (Auth::guest())
                <li><a href="{{ route('login') }}">Login</a></li>
                <li><a href="{{ route('register') }}">Register</a></li>
            @else

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                       {{--  {{ Auth::user()->username }} <span class="caret"></span> --}}
                       <img class="avtar" src="uploads/profile/{{Auth::user()->user_profile_pic}}" style="height: 19px;">
                        {{ Auth::user()->first_name}} {{Auth::user()->last_name }}<span class="caret"></span>
                    </a>

                    <ul class="dropdown-menu" role="menu">
                        <li>
                            <a href="{{ route('logout') }}"
                                onclick="event.preventDefault();
                                         document.getElementById('logout-form').submit();">
                                {{-- <i class="fa fa-fw fa-sign-out"> Logout</i> --}}
                                <i class="glyphicon glyphicon-log-out"></i> Logout
                            </a>

                            <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                {{ csrf_field() }}
                            </form>
                        </li>
                    </ul>
                </li>
            @endif
        </ul>
    </div>
</div>

我已将下拉子菜单的脚本添加为

$('.dropdown-submenu a').on("click", function(e){                    
                    $(this).next('ul').toggle();
                    e.stopPropagation();
                    e.preventDefault();                    
            });

那么实现这个目标的正确方法是什么?它也应该适用于崩溃。

1 个答案:

答案 0 :(得分:2)

您可以使用jQuery。在将鼠标悬停在<li>上时,找到.dropdown-menu这是子菜单列表并将其设为fadeInfadeOut

$('ul.nav li.dropdown').hover(function() {
  $(this).find('.dropdown-menu').fadeIn(500);
}, 
function() {
  $(this).find('.dropdown-menu').fadeOut(500);
});