简体   繁体   English

在网址栏中显示每个灯箱图片的特定锚点

[英]Showing specific anchors for each pictures of a lightbox in URL bar

I have a lightbox where each pictures has an anchor. 我有一个灯箱,每个图片都有一个锚点。
But the anchor doesn't show the specific ID for each pictures, it appears like this in the URL bar : 但是锚点没有显示每张图片的特定ID,它在网址栏中显示为:

  • www.mywebsite.com/gallery.php#example/1 www.mywebsite.com/gallery.php#example/1
  • www.mywebsite.com/gallery.php#example/2 www.mywebsite.com/gallery.php#example/2
  • www.mywebsite.com/gallery.php#example/3 www.mywebsite.com/gallery.php#example/3

So, I wonder if it's possible to show in the URL bar the specific ID : 因此,我想知道是否可以在网址栏中显示特定ID:

  • www.mywebsite.com/gallery.php#bird www.mywebsite.com/gallery.php#bird
  • www.mywebsite.com/gallery.php#dog www.mywebsite.com/gallery.php#dog
  • www.mywebsite.com/gallery.php#cat www.mywebsite.com/gallery.php#cat

Any ideas ? 有任何想法吗 ? Thanks. 谢谢。

Html HTML

 <ul class="thumbnails" id="my-gallery">
        <li class="span2">
          <a href="images/full/example-bird.jpg" class="thumbnail" id="bird">
            <img src="images/thumbs/example-bird.jpg">
          </a>
        </li>
        <li class="span2">
          <a href="images/full/example-dog.jpg" class="thumbnail" id="dog">
            <img src="images/thumbs/example-dog.jpg">
          </a>
        </li>
        <li class="span2">
          <a href="images/full/example-cat.jpg" class="thumbnail" id="cat">
            <img src="images/thumbs/example-cat.jpg">
          </a>
        </li>
 </ul>

Script 脚本

$('#my-gallery li a').iLightBox({
    skin: 'dark',
    path: 'horizontal',
    fullViewPort: 'fill',
    infinite: true,
    linkId: 'example',
    overlay:{
        opacity: 1,
        blur: false
        },
    controls: {
        thumbnail: false
        },
        styles: {
            nextOffsetX: -45,
            prevOffsetX: -45
        }
});

It's difficult to suggest an appropriate solution since I can't test iLightBox. 由于我无法测试iLightBox,因此很难提出合适的解决方案。

Referring to the iLightBox website, you could use the callback provided: 参考iLightBox网站,您可以使用提供的回调:

$('#my-gallery li a').iLightBox({
    skin: 'dark',
    path: 'horizontal',
    fullViewPort: 'fill',
    infinite: true,
    linkId: 'example',


    callback: {
        onShow:function(api) {
            $(location).attr("hash",$(api.currentElement).attr("id"));
        }
    },


    overlay:{
        opacity: 1,
        blur: false
        },
    controls: {
        thumbnail: false
        },
        styles: {
            nextOffsetX: -45,
            prevOffsetX: -45
        }
});

I highlighted the lines I inserted. 我突出显示了插入的行。 My approach is using the callback for the onShow-event of the iLightBox for changing the Hash with jQuerys $(location).attr("hash","value"); 我的方法是使用iLightBox的onShow-event回调,以使用jQuerys $(location).attr("hash","value");更改哈希$(location).attr("hash","value"); .

The problem is that I'm not sure if onShow is the correct event to hook into and if currentElement is in fact the anchor or not. 问题是我不确定onShow是否是挂接到的正确事件,以及currentElement实际上是否是锚点。 This is something you have to find out yourself. 这是您必须了解自己的东西。 I'm sorry. 对不起。

Edit 编辑

I think if you're doing it my way, you have to remove that linkId from your parameters. 我认为,如果按照我的方式进行操作,则必须从参数中删除该linkId

Edit 编辑

I tried something on the test page you linked above. 我在您上面链接的测试页上尝试了一些方法。

This code works fine for me if I paste it into the console: 如果将其粘贴到控制台中,则此代码对我来说效果很好:

$('#deeplinking_looping_gallery li a').iLightBox({
    skin: 'metro-black',
    path: 'horizontal',
    fullViewPort: 'fill',
    infinite: true,
    overlay: {
        opacity: 1,
        blur: false
    },
    callback: {
        onBeforeLoad:function(api) {
            $(location).attr("hash",$("#deeplinking_looping_gallery li").eq(api.currentItem).children("a").data("caption"));
        }
    },
    controls: {
        thumbnail: false
    },
    styles: {
        nextOffsetX: -45,
        prevOffsetX: -45
    }
});

Edit 编辑

Loading an image at page load should be possible with this line of code: 使用以下代码行可以在页面加载时加载图像:

$("a[data-caption='" + $(location).attr("hash").substr(1) + "']").trigger("click");

It triggers the click event for the appropriate anchor (like if you click it with your mouse). 它会触发相应锚点的click事件(就像您用鼠标单击它一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM