2011-05-12 127 views
0

http://briancray.com/2009/05/06/twitter-style-alert-jquery-cs-php/中取出一些JQuery,应该给出一个很好的旧twitter风格的通知。Jquery在鼠标悬停上停止动画

如何编辑下面的代码来阻止div隐藏在鼠标悬停上?

更新:我仍然希望div在mouseover完成后滑动。

$(function() { 
    var $alert = $('#alert'); 
    if($alert.length) { 
     var alerttimer = window.setTimeout(function() { 
      $alert.trigger('click'); 
     }, 5000);  
     $alert.animate({height: $alert.css('line-height') || '50px'}, 200).click(function() { 
      window.clearTimeout(alerttimer); 
      $alert.animate({height: '0'}, 200); 
     }); 
    } 
}); 
+2

我是否正确地假设你已经在mouseover事件中的元素上尝试了'.stop()'? – 2011-05-12 18:02:49

+0

什么都没有做,我试过几件事但不停止 – bluedaniel 2011-05-12 18:04:10

+0

'.stop()'停止给定元素上的所有动画,所以即使它是通过隐藏动画的一半(btw为什么你不使用' slideUp()'?)它会停止死亡。那是你要的吗? – 2011-05-12 18:05:36

回答

1

如果我理解正确(这我可能没有),你想是这样的:

var alerttimer, alertBox = $('#alert'); 
function resetTimeout() { 
     if (alerttimer) { 
       clearTimeout(alerttimer); 
     } 
     alerttimer = setTimeout(function() { 
       alertBox.trigger('click'); 
     }, 5000); 
} 
$(function() { 
     if(alertBox.length) { 
       resetTimeout(); 
       alertBox.animate({ height: alertBox.css('line-height') || '50px' }, 200).click(function() { 
         window.clearTimeout(alerttimer); 
         alertBox.animate({ height: '0px' }, 200); 
       }).mouseover(function() { 
         clearTimeout(alerttimer); 
       }).mouseout(function() { 
         resetTimeout(); 
       }); 
     } 
}); 

需要注意的是上面是非常未经测试是非常重要的。

+0

它不适合我,我不确定如何调试此代码。 – bluedaniel 2011-05-12 18:33:56

+0

对不起,你有错误?这可能是一件非常明显的事情,现在很累。 – 2011-05-12 18:34:45

+0

没有错误只是不阻止动画,如果你把它悬停在它上面 – bluedaniel 2011-05-12 18:37:05