正如我在评论中提到的,您需要在每个“详细信息”页面中包含一个脚本来嗅探 URL 并检测页面是否在新窗口(首页)中打开,如果是则重定向到主页使用修改后的 URL(例如使用 hash),允许从主页打开fancybox 中的“详细信息”页面。
因此您可以在每个“详细信息”页面中包含此脚本:
<script type="text/javascript">
var isWindow = self == top; // returns true if opened in a new window, otherwise it migh be inside an iframe
if(isWindow){
// alert("this page was opened in a new browser window");
// then redirect to main page and add hash "detailedXX"
window.location = "{URL}/mainpage.html#detailed01"
}
</script>
将detailed01 更改为每个“详细信息”页面的不同值。
然后在主页中,您可能会有指向每个详细页面的链接,例如
<a id="detailed01" class="fancybox" href="detailed01.html">Open project detail page 01 in fancybox</a>
<a id="detailed02" class="fancybox" href="detailed02.html">Open project detail page 02 in fancybox</a>
注意,我在每个锚点中添加了一个 ID,该锚点与我们在重定向到主页时将使用的 hash 匹配。
那么你的fancybox脚本可以是这样的:
<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
if(window.location.hash) {
// get the URL without hash so we can restore it if needed
var thisWindowLocation = window.location;
var thisWindowLocationSplit = String(thisWindowLocation).split("#")[0];
$(thisHash).fancybox({
width: 800,
height: 320,
fitToView: false,
autoSize : false,
type: 'iframe',
afterClose : function(){
// returns URL to main page with no hash
window.location = thisWindowLocationSplit
}
}).trigger('click');
}
// fancybox for normal main page operation
$(".fancybox").fancybox({
width: 800,
height: 320,
fitToView: false,
autoSize : false,
type: 'iframe'
});
}); // ready
</script>
我设置了DEMO here
这个演示打开两个“详细”页面:
http://www.picssel.com/playground/jquery/detailed01.html
和
http://www.picssel.com/playground/jquery/detailed02.html
如果你尝试直接打开它们,会重定向到calling page并在fancybox中打开