情况是这样的:我有一个包含4个产品的产品列表。当您将鼠标悬停在其中一个产品上时,将显示工具提示。我担心的是,如果你将鼠标从左到右快速移动到产品上,你会得到几秒钟内显示的所有工具提示。我想知道,如果鼠标停留在产品上2秒钟,我是否可以告诉jQuery开始播放动画。所以如果你让你的鼠标停留在产品上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);
});
});发布于 2011-10-24 05:11:43
我的朋友,这就是你需要的:jQuery HoverIntent plugin
发布于 2011-10-24 04:50:50
当你悬停的时候设置一个计时器,只有当你的悬停超过这个时间时才会显示提示。如果在计时器触发之前离开当前对象,则会取消未完成的计时器。如果在显示提示后离开当前对象,则会使其淡出。
我还添加了几个.stop(true,true)方法调用,以防之前的动画正在进行,这样就可以停止并加速到结束位置。如果鼠标在fadeIn完成之前离开,则可能发生这种情况。这可能不是fadeOut所必需的,因为它有2秒的延迟,但它不会造成伤害,而且可能会防止某些边缘情况。
你可以这样做:
$(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);
});
});发布于 2011-10-24 04:44:58
您应该使用mouseenter和mouseleave。
在鼠标键上,启动一个setTimeout,将动画延迟2秒。在mouseleave上,清除超时。
因此,如果鼠标悬停时间小于2秒,则不会显示任何内容。
如果你需要一个实际的例子,请询问。
编辑:所以,我会尝试这样做:
$(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);
});
});..。但是如果不看你的代码,我就不能确定它是否能工作。
https://stackoverflow.com/questions/7869019
复制相似问题