Michal
Michal

Reputation: 3368

keep DIV displayed if mouse hover over it

I have the following jQuery code (take from another question somewhere here):

$(document).ready(function(){
    $("#profile_dropdown").mouseenter(function(){
        clearTimeout($(this).data('timeoutId'));
        $("#profile_dropdown_content").fadeIn("slow");
    }).mouseleave(function(){
        var someElement = $("#profile_dropdown");
        var timeoutId = setTimeout(function(){
            $("#profile_dropdown_content").fadeOut("slow");
        }, 650);
        //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
        someElement.data('timeoutId', timeoutId); 
    });
});

It works as expected, however - I can't figure out how to keep the #profile_dropdown_content shown if the mouse is moved over it. And how to make it fadeOut if the mouse moves out of course...

Any ideas?

Upvotes: 2

Views: 1519

Answers (2)

brenjt
brenjt

Reputation: 16297

Nest the #profile_dropdown_content inside the #profile_dropdown container and use the hover event.

jsfiddle: http://jsfiddle.net/UsWYq/1/

JS

$(document).ready(function(){
    $("#profile_dropdown").hover(function(){
        clearTimeout($(this).data('timeoutId'));
        $("#profile_dropdown_content").fadeIn("slow");
    }, function(){
        var someElement = $("#profile_dropdown");
        var timeoutId = setTimeout(function(){
            $("#profile_dropdown_content").fadeOut("slow");
        }, 650);
        //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
        someElement.data('timeoutId', timeoutId); 
    });
});

HTML

<div id="profile_dropdown">
    <div class="inner">Hover Me</div>
    <div id="profile_dropdown_content">Display Me</div>
</div>
<div id="profile_dropdown"></div>

CSS

#profile_dropdown {
    background:whitesmoke;
    float:left;
}
#profile_dropdown .inner {
    height:100px;
    width:100px;
}
#profile_dropdown_content {
    display:none;
    background:red;
    height:100px;
    width:100px;
}

Option 2

Another thing you could do is CSS3 transitions: http://jsfiddle.net/Ezxm5/

#profile_dropdown {
    background:whitesmoke;
    float:left;
}
#profile_dropdown:hover #profile_dropdown_content {
    display:block;
    opacity:1;
    height:100px;
}
#profile_dropdown .inner {
    height:100px;
    width:100px;
}
#profile_dropdown_content {
    opacity:0;
    background:red;
    height:0;
    width:100px;
    overflow:hidden;
    -webkit-transition: opacity 0.4s ease-in, height 0.4s ease-out;
    -moz-transition: opacity 0.4s ease-in, height 0.4s ease-out;
    -ms-transition: opacity 0.4s ease-in, height 0.4s ease-out;
    -o-transition: opacity 0.4s ease-in, height 0.4s ease-out;
    transition: opacity 0.4s ease-in, height 0.4s ease-out;
}​
​

Upvotes: 2

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Wrap the elements in another div then bind the mouseenter and mouseleave functions to the wrapper.

Mock HTML

<div id="profile_wrapper">
    <div id="profile_dropdown">Profile Dropdown</div>
    <div id="profile_dropdown_content">This is some information about me.  I write code all day long.</div>
</div>

Javascript

$(document).ready(function(){
    $("#profile_wrapper").mouseenter(function(){
        clearTimeout($(this).data('timeoutId'));
        $("#profile_dropdown_content").fadeIn("slow");
    }).mouseleave(function(){
        var someElement = $("#profile_dropdown");
        var timeoutId = setTimeout(function(){
            $("#profile_dropdown_content").fadeOut("slow");
        }, 650);
        //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
        someElement.data('timeoutId', timeoutId);
    });
});

http://jsfiddle.net/H7Hvf/1/

Upvotes: 2

Related Questions