【问题标题】:Fancybox: link that changes the url in address bar and links to a page with an open FancyboxFancybox:更改地址栏中的 url 并链接到打开 Fancybox 的页面的链接
【发布时间】:2011-10-21 00:33:14
【问题描述】:
【问题讨论】:
标签:
javascript
jquery
fancybox
【解决方案1】:
诀窍是根据一些标签创建一个用于显示您想要的内容的函数。最简单的方法是使主题标签的值也成为页面上您希望在 fancybox 中显示的某些元素的 ID(尽管不是必需的)。
然后,只需为链接href 使用主题标签,不要在这些链接上使用jQuery 的preventDefault() 函数。这样,这些链接将更改 url 栏,附加您设置为它们的 href 值的哈希标记。然后,将显示花式框的函数附加到这些链接。
最后,使用 location.hash 的值作为参数调用该函数。
只是把这个作为一个简单的例子。似乎做你想做的事。
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script src="fb/fb.js" type="text/javascript"></script>
<link rel="stylesheet" href="fb/fb.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
$(function(){
function showfb(id) {
switch(id) {
case '#fb1':
case '#fb2':
// Gotta do this so that fb can measure the content dimensions
$(id).show();
$.fancybox({
href: id,
type:'inline',
// This is so that the content doesn't just pop back on to the
// page when finished. Of course, if you're not using inline content
// then this may not apply
onClosed: function() {
$(id).hide();
}
});
break;
}
}
showfb(location.hash);
$('a.fblink').click(function(e){
showfb($(this).attr('href'));
});
});
</script>
<style type="text/css" media="screen">
/* I'm assuming you want the fancybox content to
ONLY be displayed in the fancybox. Once again, if using
ajax or some other method of getting fancybox content, this might
not be necessary */
.fb { display: none; }
</style>
</head>
<body>
<p>
<a class='fblink' href="#fb1">Show 1</a>
</p>
<p>
<a class='fblink' href="#fb2">Show 2</a>
</p>
<div id="fb1" class="fb">This is a test</div>
<div id="fb2" class="fb">This is another test</div>
</body>
</html>