<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
                <a href="index.html" class="navbar-brand display-4">NintendoWorlds</a>
                <button class="navbar-toggler" data-toggle="collapse" data-target="#menu">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="menu">
                    <ul class="navbar-nav">
                        <li class="nav-item dropdown">
                                <a href="#" class="nav-link" data-toggle="dropdown">Games</a>
                                <ul class="dropdown-menu" >
                                    <li><a href="#" class="dropdown-item">Latest</a></li>
                                    <li><a href="#" class="dropdown-item">Best Selling</a></li>
                                    <li><a href="#" class="dropdown-item">Our Pick</a></li>
                                </ul>
                        </li>
@media all and (min-width:992px){
    .navbar .nav-item .dropdown-menu{display:none;}
    .navbar .nav-item:hover .dropdown-menu{display:block;}
    .navbar .nav-item .dropdown-menu{margin-top:0;}
}
ul.dropdown-menu a.dropdown-item{
    padding: 14px;
    margin: 8px;
}

Je veux afficher un menu lorsque je passe la souris sur "Notre choix". Pouvez-vous me montrer comment faire?

0
Quang nGUYEN 2 sept. 2020 à 05:18

2 réponses

Meilleure réponse

Vous pouvez ajouter un élément ul imbriqué dans votre choix li et afficher le sous-menu de notre choix sur le right de notre choix.

Nous pouvons le faire en utilisant la pseudo-classe : hover . Le submenu se cachera quand vous déplacerez la souris de Our Pick

Démo de travail en direct

@media all and (min-width:992px) {
  .navbar .nav-item .dropdown-menu {
    display: none;
  }
  .navbar .nav-item:hover .dropdown-menu {
    display: block;
  }
  .navbar .nav-item .dropdown-menu {
    margin-top: 0;
  }
}

ul.dropdown-menu a.dropdown-item {
  padding: 14px;
  margin: 8px;
}

.our-pick {
  display: none;
  list-style-type: none;
}

.sub-menu:hover .our-pick {
  display: block;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a href="index.html" class="navbar-brand display-4">NintendoWorlds</a>
  <button class="navbar-toggler" data-toggle="collapse" data-target="#menu">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="menu">
    <ul class="navbar-nav">
      <li class="nav-item dropdown">
        <a href="#" class="nav-link" data-toggle="dropdown">Games</a>
        <ul class="dropdown-menu">
          <li><a href="#" class="dropdown-item">Latest</a></li>
          <li><a href="#" class="dropdown-item">Best Selling</a></li>
          <li class="sub-menu"><a href="#" class="dropdown-item">Our Pick</a>
            <ul class="our-pick">
              <li><a href="#" class="dropdown-item">Our Pick 1</a></li>
              <li><a href="#" class="dropdown-item">Our Pick 2</a></li>
              <li><a href="#" class="dropdown-item">Our Pick 3</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>
1
Always Helping 2 sept. 2020 à 02:42

Si le menu est directement placé après le lien, en tant que tel:

<ul class="dropdown-menu" >
    <li><a href="#" class="dropdown-item">Latest</a></li>
    <li><a href="#" class="dropdown-item">Best Selling</a></li>
    <li>
        <a href="#" class="dropdown-item">Our Pick</a>
        <ul class = "menu">
         // menu
        </ul>
    </li>
</ul>

Vous pouvez utiliser le sélecteur + en CSS:

.dropdown-item:hover + .menu { display:block; }
0
Bingostew 2 sept. 2020 à 03:02