2013-03-04 150 views
2

我认为这很简单,但是错了......尝试使用.delay和其他在线方式找到的组合,但无法让它在没有错误的情况下正常工作。如何在mouseout上添加一秒钟的延迟

我只是想添加一个1秒的延迟,当你鼠标离开.hover区...任何想法??

在此先感谢!

$('.forward').css({ opacity:0, right:-20 }); 
$('.backward').css({ opacity:0, left:-20 }); 

$('.hover-area').hover(function() { 
    var conf_1 = { queue:false, duration:300, easing:'easeOutCubic' }; 
    var conf_2 = { queue:false, duration:400, easing:'easeOutCubic' }; 

    $(this).find('.backward, .forward').each(function() { 
    $(this).stop() 
     .animate($(this).data('animate-on'), conf_1) 
     .animate({ opacity:0.7 }, conf_2); 
    }); 
}, function() { 
    var conf_1 = { queue:false, duration:550, easing:'easeOutSine' }; 
    var conf_2 = { queue:false, duration:300, easing:'easeOutSine' }; 

    $(this).find('.backward, .forward').each(function() { 
    $(this).stop() 
     .animate($(this).data('animate-off'), conf_1) 
     .animate({ opacity:0 }, conf_2); 
    }); 
}); 
+0

你试过.delay jQuery的方法? – 2013-03-04 08:22:30

+0

是的,但不能没有问题地工作。 – Taylor 2013-03-04 08:25:58

回答

0

一切似乎罚款只是做一个.delay().stop()your callback function尝试用此之后:

$(this).find('.backward, .forward').each(function() { 
$(this).stop().delay(1000) 
    .animate($(this).data('animate-off'), conf_1) 
    .animate({ opacity:0 }, conf_2); 
}); 

什么,我在这里做的只是在你mouseout回调function()增加了.delay()

+0

你可以用这段代码添加一些你正在做什么的解释吗? – slugster 2013-03-04 08:50:39

+0

只是给mouseout函数增加了一个延迟。 – Jai 2013-03-04 08:51:46

+1

我的意思是让你编辑你的答案并添加解释......你的答案显示在低质量的审查队列上,因为它主要是一段代码。干杯:) – slugster 2013-03-04 08:56:07

0

我想你可以简单地通过使用setTimeout来做到这一点。将你的整个代码包装在一个你想在mouseout上执行的函数中,并将其与setTimeout放在一起。

以这个样本来看看或尝试这个小提琴http://jsfiddle.net/mvcGN/

HTML

<div id="test"> 
</div> 

CSS

#test{ 
    width:200px; 
    height:200px; 
    background-color:#000; 
} 

JQuery的

jQuery(document).ready(function($){ 
    $('#test').hover(function(){ 
     $(this).css("background-color","#456765"); 
    },function(){ 

//wrap your code in a function like this 
     function do_it_after_delay(){ 
     $('#test').css('background-color','#567324'); 
     } 


//simply use setTimeout to execute is with a delay 
    setTimeout(do_it_after_delay,5000); 

    }); 
}); 

此代码将改变#test的颜色,当你进入耗子的DIV和5再次秒钟后改变颜色你的鼠标左右的DIV