0

I'm using a simply Javascript that makes a menu slide out upon mouseenter then slide in upon mouseleave. However, when the mouseleaves the area, I would like the menu to remain in place for a few seconds and then slide in.

This is the code im using.

$('#nav').mouseleave(function()
    {
            $("#nav").animate({"left": "0"}, "1000");
            $("#nav li a").css({ opacity: 0.7 });
            $("#nav li.current a").css({ opacity: 1 });
});

I've search stack overflow and google, but haven't been able to get any of the solutions to work. I'm only new to JS

Any ideas?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390

3 Answers3

1

Slightly better, if you hover again, before the animation is executed:

$('#nav').hover(function () {
    clearTimeout(this.timer);
}, function () {
    this.timer = setTimeout(function () {
        $("#nav").animate({"left": "100"}, "1000");
        $("#nav li a").css({ opacity: 0.7 });
        $("#nav li.current a").css({ opacity: 1 });
    }, 1000);
});
jasssonpet
  • 2,079
  • 15
  • 18
0

Use the .delay() function from the JQuery API: http://api.jquery.com/delay/

$("#nav").delay(3000).animate({"left": "0"}, "1000");

This waits 3000 milliseconds(3 seconds) before calling animate()

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

I'm a pretty big fan of the setTimeout() method.

http://www.w3schools.com/jsref/met_win_settimeout.asp

So I would do

$('#nav').mouseleave(function()
{
        setTimeout( function(){
        $("#nav").animate({"left": "0"}, "1000");
        $("#nav li a").css({ opacity: 0.7 });
        $("#nav li.current a").css({ opacity: 1 });},1000)
});
Aaron Miler
  • 869
  • 11
  • 22