Rollover menu on click

In this post is an example of a rollover menu that appears on a click event. Timers are used to show the menu while the mouse is over the menu, and to hide the menu when you leave the menu. This can be very simple to modify from a click event to a mouseover/mouseenter event.

Demo

1. The HTML

<a href="#">Show menu</a>
<ul>
  <li>&raquo; <a href="#">First item</a></li>
  <li>&raquo; <a href="#">Second item</a></li>
  <li>&raquo; <a href="#">Third item</a></li>
</ul>

2. The CSS

Here is the entire CSS that I’ve used for the demo.

body { font-family: Arial; }
.menu a, .menu li {
  color: #9F1501;
  text-decoration: none;
}
.menu a:hover { text-decoration: underline; }
.menu {
  display: none; /* this is mandatory */
  background: #f0f0f0;
  padding: 10px;
  list-style: none;
  width: 150px;
  border-radius: 5px;
}

3. The jQuery

$(function() {
  $('.show-menu').click(function() {
    clearTimeout($(this).data('timeoutId'));
    $(".menu").show();
  }).mouseleave(function() {
    var timeoutId = setTimeout(function() {
      $(".menu").hide();
    }, 650);
    $(this).data('timeoutId', timeoutId);
  });
  $('.menu').mouseenter(function() {
    clearTimeout($(".show-menu").data('timeoutId'));
    $(".menu").show();
  }).mouseleave(function() {
    var timeoutId = setTimeout(function() {
      $(".menu").hide();
    }, 650);
    $(".show-menu").data('timeoutId', timeoutId);
  });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s