jQuery 如果鼠标悬停 2 秒。开始动画或根本不动画
情况是这样的: 我有一个产品列表,其中连续有 4 个产品。 当您将鼠标悬停在其中一种产品时,会显示工具提示。我担心的是,如果您在产品上从左到右快速移动鼠标,您会看到所有显示的工具提示几秒钟。我想知道我是否可以对 jQuery 说,只有当鼠标悬停在产品上 2 秒时才开始动画。因此,如果您将鼠标悬停在产品上 1 秒钟。然后鼠标移开,动画根本不会开始。 我正在使用 jQuery 1.2.6,这是我的工具提示代码:
$(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);
});
});
This is the situation:
I have a product list with 4 product in a row.
When you hover one of the products, a tooltip is shown. My concern is that if you move your mouse quickly from left to right or whatever over the products, you get all of the tooltips shown for a few seconds. I wonder if I can say to jQuery to start the animation only if the mouse is over the product for 2 seconds. So if you let your mouse over the product for 1 sec. and then mouse out, the animation won't start at all.
I'm using jQuery 1.2.6 and this is my code for the tooltips:
$(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);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的朋友这就是您所需要的: jQuery HoverIntent 插件
This my friend is what you need: jQuery HoverIntent plugin
当您悬停时设置一个计时器,并且仅当悬停持续时间超过该时间时才显示提示。如果在计时器触发之前离开当前对象,则会取消未完成的计时器。如果在提示显示后离开当前对象,则会将其淡出。
我还添加了几个 .stop(true, true) 方法调用,以防任何先前的动画正在进行,以便可以停止并加速到结束位置。如果鼠标在淡入完成之前离开,则可能会发生这种情况。由于 2 秒的延迟,淡出可能不需要它,但它不会造成伤害,并且可以防止某些边缘情况。
你可以这样做:
Set a timer when you hover and only show the tip if the hover lasts for more than that time. If you leave the current object before the timer fires, you cancel the outstanding timer. If you leave the current object after the tip is showing, you fade it out.
I also added a couple .stop(true, true) method calls in case any previous animation is underway so that can be stopped and accelerated to the end position. This could happen if the mouse leaves before the fadeIn completes. It probably isn't required for the fadeOut because of the 2 second delay, but it doesn't hurt and might protect against some edge case.
You can do that like this:
您应该使用 mouseenter 和 mouseleave。
在 mouseenter 上,启动 setTimeout 将动画延迟 2 秒。
在 mouseleave 上,清除超时。
因此,如果鼠标悬停时间少于 2 秒,则不会显示任何内容。
请询问您是否需要一个实际示例。
编辑 :
所以,我会尝试这个:
...但我不能确定它是否会在不查看你的代码的情况下工作。
You should use mouseenter and mouseleave.
On mouseenter, start a setTimeout delaying your animation for 2sec.
On mouseleave, clear your timeout.
So if the mouse hover less than 2s, nothing will show.
Please ask if you need a practical example.
Edit :
So, I would try this :
... but I can't be sure it'll work without looking at your code.
好的,我的一个朋友帮助了我,这是完美运行的最终代码:
});
Ok so a friend of mine helped me on this and here is the final code which is working perfectly:
});