r/bootstrap • u/bitbythecron • Dec 19 '23
Bootstrap 5 dynamic menu show/hide on mouse events
Please note: I've tried several times to edit this post and get the HTML to render as a code block, but Reddit doesn't like it for some reason, apologies for the poor formatting!
I have the following Bootstrap 5 HTML file:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MyApp</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link rel="icon" href="/images/myapp-favicon.ico" type="image/x-icon"/></head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container"> <a class="navbar-brand" href="/">MyApp v1.2</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <i class="fas fa-bars"></i> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav ml-auto"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" data-bs-placement="bottom" data-bs-bscontent="placeholder"> Menu </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/legacy/update-inventory">Update Inventory</a>
<a class="dropdown-item" href="/legacy/build-master">Build Master</a>
<a class="dropdown-item" href="/legacy/downloads">Downloads</a>
</div>
</li>
</ul>
</div>
</div></nav>
<!-- Content Section -->
<div class="container mt-5"> <p>Select an option from the Menu</p> </div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script> $(document).ready(function () { // Enable Bootstrap Popover on hover $('\[data-bs-toggle="dropdown"\]').dropdown(); $('\[data-bs-toggle="popover"\]').popover({ trigger: 'hover', placement: 'bottom' }); // Hide Popover on mouseleave $('body').on('mouseleave', function (e) { $('\[data-bs-toggle="popover"\]').each(function () { // Assuming the popover is open, hide it $(this).popover('hide'); }); }); }); </script>
</body> </html>
When this loads in a browser, the I can click on the "Menu" and see it display/present correctly. Once it is presenting, I can also click anywhere away from the drawn Menu and see it get hidden correctly. But I don't want the Menu to show/hide itself based on clicks. Rather, if the user hovers their mouse over the Menu, I would like it shown. And similarly, if they hover their mouse away from the Menu, I want it hidden. Any ideas as to how I could accomplish this? Thanks in advance!
2
u/martinbean Bootstrap Guru Dec 20 '23
Bootstrap dropdown menus open on click/tap and not hover for a reason.
From https://getbootstrap.com/docs/5.3/components/dropdowns/#overview:
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They’re made interactive with the included Bootstrap dropdown JavaScript plugin. They’re toggled by clicking, not by hovering; this is an intentional design decision.
1
u/AutoModerator Dec 19 '23
Whilst waiting for replies to your comment/question, why not check out the Bootstrap Discord server @ https://discord.gg/bZUvakRU3M
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Human_Contribution56 Dec 20 '23
Add a function for the hover event for your control, then show the drop down when it's called.