【问题标题】:How to create a direct link to any fancybox box如何创建指向任何精美盒子的直接链接
【发布时间】:2012-02-20 03:33:09
【问题描述】:

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

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

谢谢!

(就像facebook照片,当你点击任何照片时,照片会在一个盒子里打开,但地址栏会变成图片链接)

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

我按照 JFK 的建议做了,但经过一小时的尝试,我仍然不知道为什么这些盒子不一样。

看看两者的区别:

代码:

<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',
    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>';
            }

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

那个脚本有什么问题?

【问题讨论】:

  • 在触发它的链接上调用click()
  • 我是新手,我该怎么做?

标签: jquery hyperlink fancybox


【解决方案1】:

首先,您仍然需要在打开 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>

...等等

注意,我正在向每个锚点添加 IDclass,因为我必须通过 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:如果您想要花哨的选项,您需要在两个选择器 IDclass 中添加它们,例如:

<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 使用正确的选项

【讨论】:

  • 实际上我使用的是 fancybox v2.0.4 fancyapps.com/fancybox 只是参考了 fancybox.net 因为这是一个简单的例子,但它在 v2.0.4 上有点工作,唯一的问题是当我设置一个 ID 它会取消课程。所以我得到了链接,但该框未配置。例如:我有拇指和辅助按钮:当我通过框的直接链接进入时它们都消失了。还有标题:我已经配置了字体和大小,但它自己改变了。
  • 那么您需要将与类选择器中相同的 API 选项添加到从 URL 触发 fancybox 的脚本中,例如 $(thisHash).fancybox({padding:0 //etc }).trigger( '点击');
  • 如何使用 onhashchange 事件? developer.mozilla.org/en-US/docs/DOM/window.onhashchange
  • 我想问这个答案的后续问题。我得到这个为自己工作,它很棒,但我注意到它只链接到特定图像,即使它是更大图像组的一部分。如何将其设置为直接链接到特定图像,但仍然能够查看同一组中的其他图像?我正在使用最新的 fancybox 版本(2.1.5)。我所有的链接都是这样的:&lt;a id="id01" class="fancybox" data-fancybox-group="folder01" href="./photos/image01.jpg" title="folder01"&gt;image01&lt;/a&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 2013-08-18
  • 2016-04-26
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多