2011-09-18 91 views
0

我需要你的帮助。 我想用jquery动画面板。它必须打开点击按钮(函数OPEN_PANEL)并加载不同的PHP页面,然后关闭时点击一个类“关闭”(功能CLOSE_PANEL)的div。 这工作正常,问题是当我想打开一个不同的面板,当一个已经打开。它应该关闭打开的,然后打开我选择的最后一个,但它看起来像它同时执行两个功能。我该如何解决这个问题?jquery:另一个完成时执行一个函数

这是JavaScript代码:

var panel_is_open=0; 
var last_open_panel=""; 

function animation_open_panel(id){ 
    window_height=$(window).height()*0.85; 
    $("#"+id+"_button").css({'background-color':'rgba(255,255,255,0.8)', 'box-shadow':'0px 5px 10px #39C', '-webkit-box-shadow':'0px 5px 10px #39C'}); 
    $("#main_panel").show().animate({ height: window_height+"px" }, 1500) 
    .animate({ width: "90%" },1000); 
    $("#main_panel").queue(function(){ 
     $(".close").show(); 
     $("#page_container").hide().load(id+"/"+id+".php",function(){ 
      $("#page_container").fadeIn(1000); 
     }); 
     $(this).dequeue(); 
    }); 
} 

function animation_close_panel(){ 
    $("#page_container").fadeOut(1000, function(){ 
     $("#main_panel").animate({ width: "637px" }, 1000) 
     .animate({ height:"0px" }, 1500, function(){ 
      $(".close").hide(); 
      $("#"+last_open_panel+"_button").css({'background-color':'', 'box-shadow':'', '-webkit-box-shadow':''}); 
     }); 
    }); 
} 

function close_panel(){ 
    if(panel_is_open==1){ 
     animation_close_panel(); 
     panel_is_open=0; 
    } 
} 

function open_panel(id){ 
    if(panel_is_open==0){ 
     animation_open_panel(id); 
     last_open_panel=id; 
    } 

    else if(panel_is_open==1){ 
     if(id!=last_open_panel){ 
      close_panel(); 
      open_panel(id); 
     } 
    } 

    panel_is_open=1; 
} 

非常感谢你提前为您的帮助。


非常感谢您的建议,但我无法解决这两个解决方案的问题。我错了什么,但我不明白是什么。

这是我的代码:

function close_panel(){ 
    if(panel_is_open==1){ 
     // animations here 
     panel_is_open=0; 
    } 
} 

function close_open_panel(next){ 
    close_panel(); 
    next(); 
} 

function open_panel(id){ 
    if(panel_is_open==0){ 
     // animations here 
     last_open_panel=id; 
     panel_is_open=1; 
    } 

    else if(panel_is_open==1){ 
     if(id!=last_open_panel){ 
      close_open_panel(function(){ 
       open_pannel(id); 
      }); 
     } 
    } 
} 

任何想法,我弄错? 谢谢。

回答

0

您可以在函数中使用回调函数,例如

function open(callback) 
{ 
    // do stuff 
    callback(some_value); 
} 

然后你就可以这样使用它:

open(function(value) 
{ 
    // anything here will be executed 
    // after the function has finished 
}); 

callback()function(value)值是可选的,你可以返回一个简单的函数,而不是传递一个值,该值被称为回但是,它对于需要异步回调的某些功能很有用。

更多关于回调函数的信息:

1

如果你是后jQuery的具体解决方案,然后查找Deferred Object

jQuery.Deferred(),在推出版本1.5,是一个可链接的实用工具对象,可以将多个回调注册到回调队列中,调用回调队列并转发成功或失败任何同步或异步功能的状态。

+0

哇,看起来很强大。 – bzlm

相关问题