两秒后,jQuery 在鼠标悬停/移出时向上/向下滑动内容 - bug

标签 jquery settimeout jquery-easing jquery-slide-effects

当鼠标悬停在图像上时,跨度内容在两秒后向下滑动,当我将光标移出图像时,两秒后跨度向上滑动,效果很好。

如果有人也将鼠标移到该范围上,我希望显示该范围,然后我的问题就开始了......

我不知道我是否足够清楚,但我认为代码会告诉你更多的信息:)

链接: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);
        }
    });
});

提前致谢!

最佳答案

对于 jQuery 1.7+,请使用 on() 而不是 .live(),后者已被弃用。

我相信这应该可以解决您的问题:

$(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');
});​

这是一个FIDDLE

关于两秒后,jQuery 在鼠标悬停/移出时向上/向下滑动内容 - bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526459/

相关文章:

javascript - 如何使用 jasmine.js 测试 js 函数的一部分?

javascript - 如何使用setTimeout来延迟循环中的增量计数器?

javascript - 在附加图像的函数中使用 setTimeout

jquery - 寻找一个类似 “swing” 的 easing 可以用 jQuery 和 CSS3 表达

javascript - 当我设置溢出-y :hidden to element causes scroll reveal animation to not fire

jquery - 在 jQuery 中选择类

javascript - 如何清除 jQuery mouseover #id 上的 setTimeout

jquery - 是否可以定义 jQuery 动画的速度而不是持续时间?

javascript - jQuery Ajax 成功函数返回空值?