Well, here is the explanation of what is going on :
When you close fancybox, the overlay changes its CSS settings from display:block to display:none. This is triggered within the fancybox script by overlay.hide() when fancybox is closed.
NOTE: overlay.hide() is always triggered if you use an onClosed callback.
When you open a new fancybox within the onClosed callback, overlay.hide() is triggered but it doesn't seem to have time to change back to display:block with the new box, and this why you have to use the setTimeout() workaround.
However, bear in mind that if you open a new fancybox from a button (bound to fancybox) inside an opened fancybox (or navigating from one item to another in a fancybox gallery), fancybox will consider this as a transition and the overlay.hide() method will never be triggered. This is why the overlay remains visible when you click the href="#modal2" button inside of it.
If you don't want to use the setTimeout() workaround, mainly because the overlay flicks (closes/opens), you may .off() the fancybox close button and trigger a click on the href="#modal1" button to open the first fancybox back as a transition instead of onClosed. This way, the overlay.hide() method won't be triggered and the overlay will remain visible with smoother transition.
NOTE : this method will require to bind the fancybox close button back to its original functionality within the onComplete callback of the href="#modal1" initialization script like :
$(document).ready(function () {
$('input[href=#modal1]').fancybox({
onComplete: function () {
$("#fancybox-close").off().on("click", function (event) {
$.fancybox.close();
})
}
});
$('input[href=#modal2]').fancybox({
onComplete: function () {
$("#fancybox-close").off().on("click", function (event) {
event.preventDefault();
$('input[href=#modal1]').trigger("click");
})
}
});
});
See your tweaked JSFIDDLE
NOTES :
.on() and .off() methods require jQuery v1.7+.
- This solution is for fancybox v1.3.4.
- If using
inline content in fancybox v1.3.4, beware of this BUG and workaround