Activating Bootstrap Dropdown Menu On Hover
I have some problem with activating bootstrap dropdown menu on hover - it only works on click. Here is the Bootply version: Bootply version Any suggestions what I'm doing wrong?
Solution 1:
Existing Code Solution
To use your existing code, add the following line to your hover listener:
$($(this).data('target')).collapse('show');
Working fork of your bootply: http://www.bootply.com/FRv5lVuiJk
Refactored Code Solution
That being said, there is a more effecient way of doing this using tabs. See http://www.bootply.com/TjqIiOM7Hi for a working example, and the code is below.
HTML
<div class="container">
<nav class="navbar navbar-default" role="navigation" id="topmenu">
<ul class="nav navbar-nav">
<li class="dropdown active">
<a href="#one" data-toggle="tab">One</a>
</li>
<li class="dropdown">
<a href="#two" data-toggle="tab">Two</a>
</li>
<li class="dropdown">
<a href="#three" data-toggle="tab">Three</a>
</li>
</ul>
</nav>
<nav class="navbar navbar-default right tab-content" role="navigation" id="submenu">
<ul class="nav navbar-nav tab-pane active" id="one">
<li><a href="#" id="">One sub 1</a></li>
<li><a href="#" id="">One sub 2</a></li>
<li><a href="#" id="">One sub 3</a></li>
<li><a href="#" id="">One sub 4</a></li>
</ul>
<ul class="nav navbar-nav tab-pane" id="two">
<li><a href="#" id="">Two sub 1</a></li>
<li><a href="#" id="">Two sub 2</a></li>
<li><a href="#" id="">Two sub 3</a></li>
</ul>
<ul class="nav navbar-nav tab-pane" id="three">
<li><a href="#" id="">Three sub 1</a></li>
<li><a href="#" id="">Three sub 2</a></li>
</ul>
</nav>
</div>
Javascript
$('[data-toggle=tab]').hover(function (e) {
$(this).click();
});
Solution 2:
You can do this. No JavaScript needed
HTML:
<div class="dropdown">
<button class="">
<a class="">Dropdown</a>
</button>
<div class="dropdown-content">
<a class="dropdown-item">Item 1</a>
<a class="dropdown-item">Item 2</a>
<a class="dropdown-item">Item 3</a>
</div>
</div>
you can obviously change the style to your preference but the code relating to the display is important.
CSS:
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color:#6c757d;
}
Just slot in the menu in the position you want along with your bootstrap classes.
Post a Comment for "Activating Bootstrap Dropdown Menu On Hover"