Solution 1 :

add e to your scripts function like this:

<script>
    $('.dropdown').hover(function (e) {
        $(this).children('.dm-lv1').toggleClass('show');
        e.preventDefault();
    });

    $('.dropdown-submenu').hover(function (e) {
        $(this).children('.dm-lv2').toggleClass('show');
        e.preventDefault();
    });
</script>

Problem :

Created a Bootstrap multi-level dropdown navbar with some custom jQuery & CSS.

But there is an issue with toggle open & close submenu in Mobile device.

Please take a look:

$('.dropdown').hover(function() {
  $(this).children('.dm-lv1').toggleClass('show');
  e.preventDefault();
});

$('.dropdown-submenu').hover(function() {
  $(this).children('.dm-lv2').toggleClass('show');
  e.preventDefault();
});
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu a::after {
  transform: rotate(-90deg);
  position: absolute;
  right: 6px;
  top: .8em;
}

.dropdown-menu {
  top: 0;
  left: 100%;
  box-shadow: 0 0px 5px rgba(0, 0, 0, 0.4);
}

.dropdown-menu {
  top: 90%;
  padding: 0;
  border: 0;
  border-radius: 0;
  border-top: 2px solid #333;
  background: #eee;
}

.dropdown-item {
  padding: .5rem 1rem;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-light">
  <div class="container">
    <a class="navbar-brand" href="#">Brand</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active"><a class="nav-link" href="#">Home</a></li>

        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Products
          </a>
          <ul class="dropdown-menu dm-lv1 animate slideIn" aria-labelledby="navbarDropdownMenuLink">
            <li><a class="dropdown-item" href="#">Dropdown Menu</a></li>
            <li><a class="dropdown-item" href="#">Dropdown Menu</a></li>
            <li class="dropdown-submenu">
              <a class="dropdown-item dropdown-toggle" href="#">Submenu Items</a>
              <ul class="dropdown-menu dm-lv2 animate slideIn">
                <li><a class="dropdown-item" href="#">Submenu action</a></li>
                <li><a class="dropdown-item" href="#">Submenu action</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li class="nav-item active"><a class="nav-link" href="#">Clients</a></li>
        <li class="nav-item active"><a class="nav-link" href="#">News</a></li>
        <li class="nav-item active"><a class="nav-link" href="#">Contact US</a></li>
      </ul>
    </div>
  </div>
</nav>

How can I toggle (open & close) this dropdown & submenu dropdown on click?

Comments

Comment posted by Loner

add e as a parameter to your event’s anonymous functions – like ‘$(‘.dropdown’).hover(function(e) { ….’

By

Leave a Reply

Your email address will not be published. Required fields are marked *