是的,这可以做到。在这里,我假设您将为您的模态使用内联内容(隐藏)。链接将打开您的颜色框模式,但不是以正常方式将颜色框附加到链接,您只需使用普通链接和定义要打开哪个模式的查询参数:?cb=modal1。然后在您的 docReady 中,您只需在查询字符串中查找颜色框参数并打开相应的颜色框。这样,您的链接在哪里并不重要,也无需将链接声明为彩盒链接。此示例使用this answer 中的getParameterByName 函数,当然您可以使用任何您喜欢的策略来拉取查询参数。
$(document).ready(function() {
var modal = getParameterByName("cb");
if(modal != "") {
$.colorbox({
href: "#" + modal,
inline: true
});
}
});
那么任何到模态的链接都是:
<a href="yourpage?cb=modal1">Open modal 1</a>
在this jsfiddle查看该代码的完整代码。
更新:后退按钮关闭颜色框
阅读您的 cmets 后,我更了解您想要实现的目标。因此,如果您只需要在用户单击后退按钮时关闭颜色框,而不是查询字符串,您可以在链接中使用 url 哈希:
<a href="#colorbox-modal1">Open colorbox</a>
为了观察位置哈希的变化,您可以使用this jQuery onhashchange plugin,或类似的东西。然后在你的 docReady 中:
$(document).ready(function() {
$(window).hashchange(function(){
//gets the colorbox content id from the url hash
var getCb = function() { return location.hash.match(/(#colorbox-.*)/) && RegExp.$1 },
cb = getCb();
if(cb) {
$.colorbox({
href: cb,
inline: true,
//if closing with ESC key or overlay click, we
//need to go back so that hashchange is fired
//if the same colorbox is opened again
onClosed: function() {
if(getCb()) {
history.go(-1);
}
}
});
} else {
$.colorbox.close();
}
})
});
Here's 提琴,但带有免责声明:IE8 和 IE9 在此代码位于提琴内时存在问题。不过,我把它拿出来时似乎还可以。