• Home
  • Hover over dropdown with alpine js – JavaScript

Hover over dropdown with alpine js – JavaScript

To create a hover effect for a dropdown using Alpine.js, you can use the @mouseover and @mouseout directives to toggle the dropdown menu’s visibility.

Here’s an example of how you can implement this:

<div x-data="{ showDropdown: false }">
<button @mouseover="showDropdown = true" @mouseout="showDropdown = false">
Show dropdown
</button>
<ul x-show="showDropdown" @mouseover="showDropdown = true" @mouseout="showDropdown = false">
<li>Menu item 1</li>
<li>Menu item 2</li>
<li>Menu item 3</li>
</ul>
</div>

In this example, the dropdown menu will be shown when the mouse is hovering over the button, and hidden when the mouse is no longer hovering over the button or the dropdown menu.

You can also use other events, such as @focus and @blur, to toggle the dropdown menu’s visibility based on the focus state of the button.