0

I have the following:

$("#header .navigation li.menu-item-first a").mouseenter(function() {
    $(".sub-nav").css("display", "block");
}).mouseleave(function() {
    $(".sub-nav").css("display", "none");
});

I don't want .sub-nav to disappear as soon as the user moves the mouse away. How would I integrate setTimeout into these?

1
  • 3
    ...by doing it? Have you tried it? Commented May 12, 2013 at 14:43

2 Answers 2

2

there is also way to use delay()

$("#header .navigation li.menu-item-first a").mouseenter(function() {
  $(".sub-nav").stop(true).css("display", "block"); //stop "animation" and clear queue
}).mouseleave(function() {
  $(".sub-nav").delay(1000).fadeOut(1);
});

demo of usage

Sign up to request clarification or add additional context in comments.

Comments

1
$("#header .navigation li.menu-item-first a").mouseenter(function() {
  $(".sub-nav").css("display", "block");
}).mouseleave(function() {
    window.setTimeout(function() {
        $(".sub-nav").css("display", "none");
    }, 1000);
});

Maybe?

1 Comment

You would want the mouseenter handler to cancel that timeout, if it exists, otherwise if the user moves their mouse out (tripping the timeout) and then back in within 1 second (or whatever the setTimeout delay is), the menu would show again with the mouseenter handler (even though it's already shown), but then hide when the timeout resolves even with the mouse on the menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.