我查看了这篇文章如何创建直接链接到任何fancybox box,并成功实现了它。但是,我注意到每当有人点击图像时,URL并没有发生变化。所以,基本上所有的脚本都只是让我可以把图片链接发送给别人。如果有人想要捕捉图片链接并分享它们怎么办?是否可以在人们浏览网站时在地址栏中显示不同的图像URL?
如果我理解正确:
在这种情况下,您可以在单击图像时设置url哈希片段。
location.hash = "image02";
使用您提供的示例:
var thisHash = window.location.hash;
$(document).ready(function() {
$('.thumbs').fancybox({
prevEffect: 'fade',
nextEffect: 'fade',
closeBtn: true,
arrows: true,
nextClick: true,
padding: 15,
helpers: {
thumbs: {
width: 80,
height: 80
}
},
beforeShow: function() {
var id = this.element.attr("id")
if (id) {
window.location.hash = id;
}
},
beforeClose: function() {
window.location.hash = "";
}
});
if (thisHash) {
$(thisHash).trigger('click');
}
});
我使用了Greg的代码,但是我使用了这些选项(并以不同的方式引用id,因为Greg的方式对我不起作用):
onStart: function(selectedArray, selectedIndex, selectedOpts) {
var id = $(selectedArray[ selectedIndex ]).attr('id');
if (id) {
window.location.hash = id;
}
},
onClosed: function() {
window.location.hash = "";
}