How to open the menu collapse when the mouse hovers?

Viewed 319

I have a site with Drupal 8 and Bootstrap 3.3.7

I have a menu collapse on my site and I want it to open to mouse over on desktop.

Here is my JS code but it does not work:

$(".navbar-toggle-first").mouseenter(function () {
        $(".navbar-collapse-first").fadeIn();
    });
 $(".navbar-toggle-first").mouseleave(function(){
       $(".navbar-collapse-first").fadeOut();
});

Here is the HTML code of my page:

<button type="button" class="navbar-toggle-first collapsed" data-toggle="collapse" data-target="#navbar-collapse-first" aria-expanded="false">

  <div class="icon-navbar-first">
    <span class="fa-layers fa-3x">
      <i class="far fa-circle"></i>
      <span class="navbar-icon-open">
        <i class="fas fa-th-list" data-fa-transform="shrink-8"></i>
      </span>
      <span class="navbar-icon-close">
        <i class="fas fa-times" data-fa-transform="shrink-8"></i>
      </span>
    </span>
  </div>

  <div class="icon-navbar-first-alert icon-navbar-first-alert-disable">
    <span class="fa-layers fa-3x">
      <i class="fas fa-circle"></i>
      <span class="navbar-icon-open">
        <i class="fas fa-th-list fa-inverse" data-fa-transform="shrink-8"></i>
      </span>
      <span class="navbar-icon-close">
        <i class="fas fa-times fa-inverse" data-fa-transform="shrink-8"></i>
      </span>
    </span>
  </div>

</button>

and

{# Navigation (collapsible first) #}
{% if page.navigation_collapsible_first %}
  <div id="navbar-collapse-first" class="navbar-collapse-first collapse width navbar-collapse-first-fixed-top">
    {{ page.navigation_collapsible_first }}
  </div>
{% endif %}
1 Answers

your nav structure would look like:

<ul>
    <li>About</li>
    <li>Products
        <ul>
            <li>Booster</li>
            ....
        </ul>
    </li>
</ul>

Then you can use CSS (instead of javascript) to control the hiding and showing of the sub-nav. This can be done with the :hover pseudo-selector.

The approach is to initially hide the submenu with

ul ul { display: none }

When the user mouse's over the main menu's items, use the :hover selector to display the sub-menu

li:hover ul { display: block }

Related