首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果鼠标悬停2秒,则返回jQuery。开始动画或根本不动画

问如果鼠标悬停2秒,则返回jQuery。开始动画或根本不动画
EN

Stack Overflow用户
提问于 2011-10-24 04:38:59
回答 4查看 3.2K关注 0票数 2

情况是这样的:我有一个包含4个产品的产品列表。当您将鼠标悬停在其中一个产品上时,将显示工具提示。我担心的是,如果你将鼠标从左到右快速移动到产品上,你会得到几秒钟内显示的所有工具提示。我想知道,如果鼠标停留在产品上2秒钟,我是否可以告诉jQuery开始播放动画。所以如果你让你的鼠标停留在产品上1秒。然后鼠标移出,动画将根本不会开始。我使用的是jQuery 1.2.6,这是我的工具提示代码:

代码语言:javascript
复制
$(document).ready(function(){
$('.thumb-image').hover(function() {
    $(".thumb-image").mousemove(function(e){
        $(this).find(".t-desc").filter(":not(:animated)").fadeIn(500);
            $(this).find(".t-desc").css({
                top: (e.pageY + 27) + "px",
                left: (e.pageX - 20) + "px"     });
    }); 
    }, function() {
        $(this).find(".t-desc", this).fadeOut(250);             
}); 
});
EN

回答 4

Stack Overflow用户

发布于 2011-10-24 05:11:43

我的朋友,这就是你需要的:jQuery HoverIntent plugin

票数 4
EN

Stack Overflow用户

发布于 2011-10-24 04:50:50

当你悬停的时候设置一个计时器,只有当你的悬停超过这个时间时才会显示提示。如果在计时器触发之前离开当前对象,则会取消未完成的计时器。如果在显示提示后离开当前对象,则会使其淡出。

我还添加了几个.stop(true,true)方法调用,以防之前的动画正在进行,这样就可以停止并加速到结束位置。如果鼠标在fadeIn完成之前离开,则可能发生这种情况。这可能不是fadeOut所必需的,因为它有2秒的延迟,但它不会造成伤害,而且可能会防止某些边缘情况。

你可以这样做:

代码语言:javascript
复制
$(document).ready(function(){
    var tipTimer = null;
    $('.thumb-image').hover(function() {
        var self = this;
        if (tipTimer) {
            clearTimeout(tipTimer);
            tipTimer = null;
        }
        tipTimer = setTimeout(function() {
            tipTimer = null;
            $(self).find(".t-desc").filter(":not(:animated)").stop(true, true).fadeIn(500);
                $(self).find(".t-desc").css({
                    top: (e.pageY + 27) + "px",
                    left: (e.pageX - 20) + "px"});
            }, 2000);
        }, function() {
            if (tipTimer) {
                clearTimeout(tipTimer);
                tipTimer = null;
            }
            $(this).find(".t-desc", this).stop(true, true).fadeOut(250);             
    }); 
});
票数 1
EN

Stack Overflow用户

发布于 2011-10-24 04:44:58

您应该使用mouseenter和mouseleave。

在鼠标键上,启动一个setTimeout,将动画延迟2秒。在mouseleave上,清除超时。

因此,如果鼠标悬停时间小于2秒,则不会显示任何内容。

如果你需要一个实际的例子,请询问。

编辑:所以,我会尝试这样做:

代码语言:javascript
复制
$(document).ready(function(){

    var timer;
    $('.thumb-image').bind('mouseenter',enter).bind('mouseleave',leave);

    function enter(){
        var $this = $(this);
        clearTimeout(timer);
        timer = setTimeout(function()
        {
                $this.find(".t-desc").filter(":not(:animated)").fadeIn(500);
                $this.find(".t-desc").css({
                    top: (e.pageY + 27) + "px",
                    left: (e.pageX - 20) + "px"     });
            },2000);
    }); 

    function leave() {
        var $this = $(this);
        clearTimeout(timer);
        $this.find(".t-desc", this).fadeOut(250);             
    }); 

});

..。但是如果不看你的代码,我就不能确定它是否能工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7869019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档