eozzy
eozzy

Reputation: 68710

Simple jQuery Hover Menu

This looks really bloated, can it be rewritten any better/more compact:

$("#cart-summary").hover(
      function () {
        $('.flycart').slideDown('fast');
      }
   );
   $(".flycart").hover(
      function () {}, // mousein function not required
      function () { // hide menu on mouseout
        $('.flycart').slideUp('fast');
      }
   );
    $('.flycart a.close').click(function(){
        $(this).parents('.flycart').hide();
    });

Thanks!

Upvotes: 0

Views: 2844

Answers (2)

Alex Gyoshev
Alex Gyoshev

Reputation: 11977

$("#cart-summary").mouseenter(function () {
    $('.flycart').slideDown('fast');
});

$(".flycart")
    .mouseleave(function () {
        $(this).slideUp('fast');
    })
    .find('a.close')
        .click(function(){
            $(this).parents('.flycart').hide();
        });

It's a small improvement, though. I couldn't guess the relationship between the #cart-summary and the .flycarts.

Upvotes: 2

Eric
Eric

Reputation: 97631

In short, no. However, you can do without the empty hover function: just use mouseenter() and mouseleave(). mouseover and mouseout have subtle differences to mouseenter and mouseleave. Look at the jQuery API for more info.

$("#cart-summary").mouseenter(function()
{
    $('.flycart').slideDown('fast');
});

$(".flycart").mouseleave(function()
{
    $(this).slideUp('fast');
});

$('.flycart a.close').click(function()
{
    $(this).parents('.flycart').hide();
});

Upvotes: 1

Related Questions