要将新内容加载到已打开的fancybox 中,您可以执行以下操作。这样,fancybox 将不会在使用新内容更新 fancybox 窗口之间关闭:
// is fancybox already popped up?
fancyboxIsOpen: false,
openFancyBox: function (urlToBeLoaded) {
if (urlToBeLoaded === null) {
this.closeFancyBox();
} else {
if (this.fancyboxIsOpen) {
this.openInAlreadyOpenedFancyBox(urlToBeLoaded);
} else {
this.openFirstFancyBox(urlToBeLoaded);
}
}
},
openFirstFancyBox: function (urlToBeLoaded) {
var that = this;
$.fancybox.open({
type: 'ajax',
href: urlToBeLoaded,
afterClose: function() {
this.openFancyBox(null);
return true;
},
afterShow : function() {
that.fancyboxIsOpen = true;
var ahref = jQuery(".fancybox");
ahref.on("click", function(event){
//stop the click action, we do not want to load the href in the window
event.preventDefault();
var currentTarget = $(event.currentTarget);
var urlToBeLoaded = that.getFancyBoxId(currentTarget);
});
}
});
}, // end openFirstFancyBox
openInAlreadyOpenedFancyBox: function (urlToBeLoaded) {
var that = this;
$.fancybox.showLoading(); // show loading icon
$.ajax({
type: "GET",
url: urlToBeLoaded,
success : function(content, textStatus, jqXHR) {
$.fancybox.hideLoading();
jQuery(".fancybox-inner").html(content);
$.fancybox.update();
},
error : function(xhr, status) {
$.fancybox.hideLoading();
return false;
}
});
},
closeFancyBox: function () {
this.fancyboxIsOpen = false;
$.fancybox.close();
},
getFancyBoxId: function (currentTarget) {
var id = currentTarget.attr("href");
return id;
},
重要的是jQuery(".fancybox-inner").html(content);
用新内容替换当前fancybox 的内容,并且可以选择$.fancybox.update();
根据新内容调整窗口大小。