-1

I'm trying to follow this example for creating a dropdown menu in bootstrap on hover. Right now when you hover over the dropdown menu, nothing happens - (try hovering over services).

What doesn't make sense to me is that it actually works in the 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);
    });

The JavaScript is loaded in the <head> section, inside of opening and closing <script> tags. And again the site is live here. Please let me know if you have any suggestions - thanks!

SpeakInCode43
  • 580
  • 4
  • 11
  • 23
  • 1
    Works fine for me. – Carol Skelly May 24 '17 at 13:41
  • 4
    So, take a look [here](https://stackoverflow.com/questions/8878033/how-to-make-twitter-bootstrap-menu-dropdown-on-hover-rather-than-click). – Dwhitz May 24 '17 at 13:42
  • @ZimSystem Doesn't work for me in Google Chrome, Firefox, or Safari - I'm hovering over services and nothing's happening. – SpeakInCode43 May 24 '17 at 13:43
  • 1
    @Dwhitz yeah that works. Not sure why that article didn't show up when I wrote this question. Thank you! – SpeakInCode43 May 24 '17 at 13:47
  • 1
    You can solve this entirely without JavaScript, since CSS already has the :hover element and you can make keyframes to fade in and fade out an element. Look at this link as well: https://www.w3schools.com/howto/howto_css_dropdown.asp – broodjetom May 24 '17 at 13:47
  • @Sean Google is your friend dude. – Dwhitz May 24 '17 at 13:47
  • @Dwhitz Just saying, when I googled "Bootstrap Dropdown Menu Not Dropping Down On Hover", the first three articles that showed up only have JS solutions - actually the [article that you linked](https://stackoverflow.com/questions/8878033/how-to-make-twitter-bootstrap-menu-dropdown-on-hover-rather-than-click), which has 900 upvotes, doesn't show up at all on the first page of google search. I'm going to create a solution with a link to the article so that more people can see it - thanks. – SpeakInCode43 May 24 '17 at 13:53
  • 1
    @Sean dont' worry. Next time you can search more/better using google keyword with [advanced search](https://www.google.it/advanced_search?q=keyword&hl=it) maybe. – Dwhitz May 24 '17 at 13:56

3 Answers3

1

Please see this article, which describes how to do it with 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;
}
SpeakInCode43
  • 580
  • 4
  • 11
  • 23
0

Try using a different bootstrap version or check whether you linked bootstrap js and css properly.

<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> 

I tried with the following css and js version and it worked fine for me.

Working example

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

Sarath Hari
  • 219
  • 1
  • 11
0

Please add this function in $(document).ready() function.

Wrong:

$('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);
});

Right:

$(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);
   });
})
Hassan Siddiqui
  • 2,799
  • 1
  • 13
  • 22