Bootstrap下拉菜单在悬停时不会掉线

时间:2017-05-24 13:39:21

标签: javascript jquery html css twitter-bootstrap

我试图按照this example在悬停时在引导程序中创建下拉菜单。现在,当您将鼠标悬停在下拉菜单上时,没有任何反应 - (try hovering over services)

对我来说有意义的是,它实际上适用于JSFiddle

HTML:

<nav class="navbar navbar-default">
    <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <div>
            <a class="navbar-brand" href="http://nowordpress.gatewaywebdesign.com/">
            <img src="assets/images/gatewaylogo.png">
            </a>
        </div>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
            <li><a href="http://nowordpress.gatewaywebdesign.com/index.html">Home <span
                    class="sr-only">(current)</span></a></li>
            <li><a href="http://nowordpress.gatewaywebdesign.com/about.html">About</a></li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                   aria-expanded="false">
                    Services
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="http://nowordpress.gatewaywebdesign.com/website-design.html">Website Design</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/graphic-design.html">Graphic Design</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/promotional-products.html">Promotional Products</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/search-engine-marketing.html">Search Engine Marketing</a></li>
                    <li><a href="http://nowordpress.gatewaywebdesign.com/wordpress-conversion.html">WordPress Conversion</a></li>
                </ul>
            </li>
            <li><a href="/store.html">Store</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">My Account</a></li>
        </ul>

    </div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->

CSS:

#bottom-nav-menu {
height: 100px;
}

#gatewaylogo {
height: 85px;
padding-left: 6px;
padding-top: 10px;
}

.navbar-li, .navbar-li a {
text-decoration: none;
font-family: 'oswald_regularregular';
color: #1B3764;
list-style-type: none;
font-size: 22px;
}

.navbar-li a:hover {
color: #76abdc;
text-decoration: none;
}

#my-account {
position: relative;
}

.navbar-brand {
height: inherit;
}

.navbar-brand img {
height: 82px;
padding: 10px 0;
}

.navbar-default {
border: none;
background: #ffffff;
line-height: 100px;
margin-bottom: 0;
}

.navbar-default .navbar-nav > .active > a,
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
background: none;
background-color: transparent;
color: #1971b9;
transition: ease-in-out 0.3s;
}

.navbar-default .navbar-nav > li > a {
font-family: 'oswald_regularregular';
color: #1B3764;
text-transform: uppercase;
font-size: 18px;
line-height: 100px;
}

.navbar-default .navbar-nav > li:last-child > a {
margin-right: 0;
}

的JavaScript

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

JavaScript在开始和结束<head>标记内的<script>部分中加载。该网站又是实时here。如果您有任何建议,请告诉我 - 谢谢!

3 个答案:

答案 0 :(得分:1)

请参阅this article,其中介绍了如何使用CSS。

CSS:

.dropdown-menu .sub-menu {
left: 100%;
position: absolute;
top: 0;
visibility: hidden;
margin-top: -1px;
}

.dropdown-menu li:hover .sub-menu {
visibility: visible;
}

.dropdown:hover .dropdown-menu {
display: block;
}

.nav-tabs .dropdown-menu, .nav-pills .dropdown-menu, .navbar .dropdown-menu 
{
margin-top: 0;
}

.navbar .sub-menu:before {
border-bottom: 7px solid transparent;
border-left: none;
border-right: 7px solid rgba(0, 0, 0, 0.2);
border-top: 7px solid transparent;
left: -7px;
top: 10px;
}
.navbar .sub-menu:after {
border-top: 6px solid transparent;
border-left: none;
border-right: 6px solid #fff;
border-bottom: 6px solid transparent;
left: 10px;
top: 11px;
left: -6px;
}

答案 1 :(得分:0)

尝试使用不同的bootstrap版本或检查是否正确链接了bootstrap js和css。

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">

<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js'></script> 

我尝试使用以下css和js版本,它对我来说很好。

工作示例

https://jsfiddle.net/sarathhari/2xqop6wd/

答案 2 :(得分:0)

请在$(document).ready()函数中添加此函数。

错:

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

右:

$(document).ready(function(){
   $('ul.nav li.dropdown').hover(function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(500);
            }, function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(500);
   });
})