Skip to content
Home » Forum

Forum

Interactive Dropdow...
 
Notifications
Clear all

Interactive Dropdown Menu (Using Mouse Events)

1 Posts
1 Users
0 Reactions
68 Views
Mark Sikaundi
(@emmanuelmark117)
Member Admin
Joined: 2 years ago
Posts: 101
Topic starter  

Create a dropdown menu that appears when the user hovers over a button and disappears when the mouse leaves the dropdown area.

Use Case: Hover Dropdown Menu

You want a dropdown to show when the user hovers over a button, and hide when the user moves the mouse away.

Example:

<button id="menuButton">Menu</button>
<div id="dropdownMenu" style="display: none; border: 1px solid black; width: 150px;">
  <a href="#">Link 1</a><br>
  <a href="#">Link 2</a><br>
  <a href="#">Link 3</a>
</div>

<script>
  const menuButton = document.getElementById('menuButton');
  const dropdownMenu = document.getElementById('dropdownMenu');

  menuButton.addEventListener('mouseover', function() {
    dropdownMenu.style.display = 'block';
  });

  dropdownMenu.addEventListener('mouseleave', function() {
    dropdownMenu.style.display = 'none';
  });
</script>

   
Quote
Share: