2012-04-04 63 views
6

如何在某个元素完成动画后运行一个函数?这是一种像动画一样的下滑,元素被隐藏,当点击某个元素时,通过滑动其内容(+高度)可以看到元素。div完成动画后的事件?

我有超过该元素的动画功能无法控制,所以我不能使用来自$ .animate函数的回调选项...

现在,我有这样的事情

$('.trigger').click(function(){ // <-- "trigger" will make the element animate 
    setTimeout(myfunction, 500); 
}); 

其作品,但我不喜欢它,感觉哈克

+0

显示的其他代码... – mplungjan 2012-04-04 11:20:36

回答

3

,如果你使用jQuery 1.5或更高版本,你可以使用jQuery的promise()方法:http://jsfiddle.net/rGBk7/

例如

/* do animation */ 
$("div").animate({ 'marginLeft' : '300px' }, 1000); 

/* attach a promise to animated element/s */  
$("div").promise().done(function() { 
    alert("animation end"); 
}); 

http://api.jquery.com/promise/

+0

这一声运行作为为页面加载。问题是,我的动画运行在用户干预(点击不同的元素) – Alex 2012-04-04 11:28:05

+0

像这样? http://jsfiddle.net/fcalderan/rGBk7/5/ – fcalderan 2012-04-04 11:32:15

+0

类似的东西,但就像我说的,我无法控制动画功能:(所以它应该是:http://jsfiddle.net/rGBk7/20/ – Alex 2012-04-04 11:40:32

1

你的意思是:


$("div").queue(function(){ 
    your_function(); 
}); 

它将动画队列后才能执行完毕