Will Tuttle
Will Tuttle

Reputation: 450

jQuery slideUp/Down keeps repeating?

I have a list that popups on the right when a div is hovered over. I wanted to use jqueries slideUp/Down to reveal and hide the list, which work most of the time. but sometimes the function will trigger repeatedly. Constantly opening and closing. I'm not sure why it does that. Any ideas?

Heres my code:

$(myDiv).hover(

  function () {

    $('ul.list_menu').slideDown('medium');

     }, 
     function () {

        $('ul.list_menu').slideUp('medium');

     }

);

Upvotes: 2

Views: 1307

Answers (2)

jfriend00
jfriend00

Reputation: 708106

You'd have to show us your HTML to know for sure what is going on, but if the act of sliding down the menu causes the mouse to no longer be over the div, then it will "un-hover" automatically and trigger the menu close and then when it closes, it will hover again and will cycle back and forth for some mouse positions.

The way out of this would be to change the HTML so dropping the menu down doesn't overlap with the mouse position that makes it come down or to use a different mechanism for closing the menu other than de-hover on your div.

To solve an issue with quickly hovering and dehovering your mouse (though it doesn't sound this is the issue you describe), then you also should use .stop(true) to cancel previous animations like this:

$('ul.list_menu').stop(true).slideDown('medium');

Upvotes: 2

adeneo
adeneo

Reputation: 318352

Try a few stops

$(myDiv).hover(function () {
    $('ul.list_menu').stop(true, true).slideDown('medium');
},function () {
    $('ul.list_menu').stop(true, true).slideUp('medium');
});

Upvotes: 3

Related Questions