微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 如何创建指向任何fancybox框的直接链接

当我点击任何使用fancybox的东西时,我需要它为它生成一个特定的URL,因此当我将此链接发送给某人时,它会打开我想要的特定框.

例如:
fancybox.net/home
当我点击第一张图片时,链接仍然是fancybox.net/home
我希望当我点击图片时,会生成URL并显示在地址栏中,如:fancybox.net/home/imageid=1
因此,当我向某人发送fancybox.net/home/imageid=1时,它已经打开了框中的图像

谢谢!

(就像Facebook照片一样,当您点击任何照片时,照片会在一个方框中打开,但地址栏会更改为图片链接)

//////
更新#1
//////

我做了肯尼迪的建议,但经过一个小时的尝试,我仍然不知道为什么盒子不一样.

看看之间的差异:

代码

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
if(window.location.hash) {
$(thisHash).fancybox({
    prevEffect : 'none',nextEffect : 'none',closeBtn  : false,arrows    : true,nextClick : true,helpers :   { 
            thumbs : {
                width  : 80,height : 80
            },title : {
            type : 'inside'
            },buttons : {}
                },afterLoad : function() {
            this.title = (this.index + 1) + ' de ' + this.group.length + '<div id="curti"><iframe src="http://www.facebook.com/plugins/like.PHP?href=http://www.google.com.br&amp;send=true&amp;layout=standard&amp;width=45&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:56px; height:24px;" allowTransparency="true"></iframe></div>';
            }

                }).trigger('click');
 }
 $('.fancylink').fancybox({
    prevEffect : 'none',afterLoad : function() {
            this.title = (this.index + 1) + ' de ' + this.group.length     + '<div id="curti"><iframe src="http://www.facebook.com/plugins/like.PHP?href=http://www.google.com.br&amp;send=true&amp;layout=standard&amp;width=45&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:56px; height:24px;" allowTransparency="true"></iframe></div>';
            }

                });
}); // ready
</script>

那个剧本有什么问题?

解决方法

首先,您仍然需要在打开fancybox页面链接到您的图像,例如:
<a id="image01" class="fancylink" rel="gallery" href="images/01.jpg" title="image 01">open image 01</a>
<a id="image02" class="fancylink" rel="gallery" href="images/02.jpg" title="image 02">open image 02</a>

…等

请注意,我正在为每个锚添加ID和类,因为我必须通过URL定位它们的唯一方法是通过它们的ID …这个类将用于在fancybox中以常规方式打开这些图像在页面上,所以我不需要为每个ID编写特定的fancybox代码.

然后在引用页面上设置此脚本:

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
 if(window.location.hash) {
  $(thisHash).fancybox().trigger('click');
 }
 $('.fancylink').fancybox();
}); // ready
</script>

然后你可以提供针对每个图像的URL

http://mydomain.com/page.html#image01

要么

http://mydomain.com/page.html#image02

等等

想要工作演示吗?

http://picssel.com/playground/jquery/targetByID_28jan12.html#image01

http://picssel.com/playground/jquery/targetByID_28jan12.html#image02

注意:此代码适用于fancybox v1.3.x,因为您使用fancybox.net作为参考.

更新#1:如果你想要fancybox选项,你需要在选择器ID和类中添加它们,如:

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
 if(window.location.hash) {
  $(thisHash).fancybox({
    padding: 0
    // more API options
  }).trigger('click');
 }
 $('.fancylink').fancybox({
    padding: 0
    // more API options
 });
}); // ready
</script>

当然,对fancybox v1.3.x或2.x使用正确的选项

原文地址:https://www.jb51.cc/jquery/178651.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐