2012-09-10 356 views
2

我不知道如何解决看似简单的问题。如果将鼠标悬停在专辑封面上,则会淡入(i)图标,如果您将鼠标悬停在(i)图标上,会出现一个工具提示,但它在1,2秒后不会停留在淡出状态。我如何解决这个问题,当鼠标悬停在(i)图标上并且当鼠标离开图标时fadesOut停留在工具提示上。Jquery鼠标事件

这里举例:http://www.midnightlisteners.com

我的代码:

//  (i) Icon 

    $(".play, .more-info").mouseenter(function(){ 
     clearTimeout($(this).data('timeoutIds')); 
     $(this).next(".more-info").fadeIn(600); 
    }).mouseleave(function(){ 
     var someElement = $(this); 
     var timeoutIds = setTimeout(function(){ 
      someElement.next(".more-info").fadeOut('150'); 
     }, 1200); // giving a shorter time will reduce the fadeout effect 
     //set the timeoutId, allowing us to clear this trigger if the mouse comes back over 
     someElement.data('timeoutIds', timeoutIds); 
    }); 

//工具提示

$(".more-info").mouseenter(function(){ 
     clearTimeout($(this).data('timeoutId')); 
     $(this).find(".the-tooltip").fadeIn('150'); 
    }).mouseleave(function(){ 
     var someElement = $(this); 
     var timeoutId = setTimeout(function(){ 
      someElement.find(".the-tooltip").fadeOut('150'); 
     }, 1200); 
     //set the timeoutId, allowing us to clear this trigger if the mouse comes back over 
     someElement.data('timeoutId', timeoutId); 
    }); 
+0

这可能更适合于Code Review:http://codereview.stackexchange.com/ – calvinf

+1

@calvinf:Code Review SE仅适用于工作代码。这似乎是一个调试/帮助问题。 – palacsint

+1

@palacsint:好的,很好的信息,我会记住这一点。 – calvinf

回答

1

试试这个

$(function(){ 
    var timeoutIds=0; 
    $(".play").on('mouseenter', function(){ 
     $(this).next(".more-info").fadeIn(600); 
    }).on('mouseleave', function(){ 
     var someElement = $(this); 
     timeoutIds = setTimeout(function(){ 
      someElement.next(".more-info").fadeOut('150'); 
     }, 1200); 
    }); 

    $(".more-info").mouseenter(function(){ 
     clearTimeout(timeoutIds); 
     $(this).find(".the-tooltip").fadeIn('150'); 
    }).mouseleave(function(){ 
     var someElement = $(this); 
     timeoutId = setTimeout(function(){ 
      someElement.find(".the-tooltip").fadeOut('150'); 
     }, 300); 
    }); 
});​ 

DEMO(由于茶演示版的源代码不再工作)。

+0

嗨谢赫Heera,我没有尝试过的代码,但现在他们不会褪色了 – Pullapooh

+0

我希望(i)也会淡出。 – Pullapooh

+0

@Pullapooh,用演示检查更新的答案。 –