jquerysettimeoutjquery-easingjquery-slide-effects

jQuery slide up/down content on mouse over/out after two seconds - bug


When mouse is over the image, span content slides down after two seconds, also when I move cursor out of image, after two seconds span slides up, that's work fine.

I want to span be displayed if someone move mouse over that span too, and there my problem begins...

I do not know if I was clear enough but I think the code will tell you more than words :)

Link: http://jsfiddle.net/zDd5T/3/

HTML:

<img id="image" src="button_green.png" />
<span id="content">
    <a href="http://www.google.com">Link</a>
    Some content here
</span>

CSS:

#image{
   left:0px;
   position:absolute;
   z-index: 10;
}
#content{
   top:48px;
   left:0px;
   position:absolute;
   height: 100px;
   border: 1px solid #f00;
   display: block;
   z-index: 0;
   width: 100px;
}

JavaScript:

$(document).ready(function() {
    $('#content').hide();
    var over_img = false;
    var over_span = false;
    $('#image').live('mouseover', function() {
        over_img = true;
        setTimeout(function() {
            $('#content').show('slide', {
                direction: 'up'
            }, 1000);
        }, 2000);
    });
    $('#content').live('mouseover', function() {
        over_span = true;
        setTimeout(function() {
            $('#content').show('slide', {
                direction: 'up'
            }, 1000);
        }, 2000);
    });
    $('#image').live('mouseout', function() {
        over_img = false;
        if (!over_img && !over_span) {
            setTimeout(function() {
                $('#content').hide("slide", {
                    direction: "up"
                }, 1000);
            }, 2000);
        }
    });
    $('#content').live('mouseout', function() {
        over_span = false;
        if (!over_img && !over_span) {
            setTimeout(function() {
                $('#content').hide("slide", {
                    direction: "up"
                }, 1000);
            }, 2000);
        }
    });
});

Thanks in advance!


Solution

  • For jQuery 1.7+ use on() instead of .live() wich is deprecated.

    I believe this should solve your problem:

    $(document).ready(function() {
        var T1, T2;
        $('#content').hide();
        $('body').on({
            mouseenter: function() {
                clearTimeout(T2);
                T1 = setTimeout(function() {
                     $('#content').slideDown(1000);
                }, 2000);
            },
            mouseleave: function() {
                clearTimeout(T1);
                T2 = setTimeout(function() {
                     $('#content').slideUp(1000);
                }, 2000);    
            }
        }, '#image, #content');
    });​
    

    Here's a FIDDLE