Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I delay The mouse-out function when using Jquery .hover()?

I have a Text Box that is hidden until a div is hovered over. I am Using Jquery's hover function, and it works. But what I would like is to delay the mouse-out function for a few seconds before toggling closed. Here is my code.

// Pops out TxtBox in Left Column When Hovered then collapses after delay
$(".CollapsedLeft .LeftColumn .SearchHoverCatcher").hover(function() {
    $(".CollapsedLeft .LeftColumn .SearchHoverCatcher").addClass("Hovered");
}, function() {
$(".CollapsedLeft .LeftColumn .SearchHoverCatcher").delay(1000).removeClass("Hovered");
});

The above code Hides and shows the text box as desired but the 1000ms delay doesn't occur. Any Thoughts would be greatly appreciated.

Jquery Answers Please.

like image 669
firepig Avatar asked Oct 30 '13 21:10

firepig


People also ask

Does jQuery handle hover?

jQuery hover() MethodThe hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.

Is jQuery hover deprecated?

Is hover deprecated in jQuery? Deprecated in jQuery 1.8, removed in 1.9: The name “hover” used as a shorthand for the string “mouseenter mouseleave” .

What does delay do in jQuery?

Added to jQuery in version 1.4, the . delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .


1 Answers

delay() works with animations, not just arbitrary statements. You can use a setTimeout:

http://jsfiddle.net/p4b7P/

var hoverTimeout;
$('#theDiv').hover(function() {
    clearTimeout(hoverTimeout);
    $(this).addClass('hovered');
}, function() {
    var $self = $(this);
    hoverTimeout = setTimeout(function() {
        $self.removeClass('hovered');
    }, 1000);
});
like image 101
Jason P Avatar answered Oct 16 '22 04:10

Jason P