coldpumpkin coldpumpkin - 2 months ago 9
jQuery Question

"Direct link to any fancybox", but with URL in address bar

I took a look at this post How to create a direct link to any fancybox box and I managed to implement it. However, I noticed that whenever someone clicks an image, the URL doesn't change. So, basically, all the script does is to allow me to give the link of the image to people. What if people want to catch the image links for themselves and share them? Is it possible to also show the different image urls in the address bar as people navigate the site?

Answer

If I understand correctly:

  • When a visitor clicks on an image, the image pops up in fancybox.
  • When a visitor navigates to page.html#image01, the page loads with image01 already shown in fancybox.
  • You want the url to update to page.html#image02 when a visitor clicks on image02.

In that case, you can set the url hash fragment when the image is clicked.

location.hash  = "image02";

Using your supplied example:

var thisHash = window.location.hash;
$(document).ready(function() {
    $('.thumbs').fancybox({
        prevEffect: 'fade',
        nextEffect: 'fade',
        closeBtn: true,
        arrows: true,
        nextClick: true,
        padding: 15,
        helpers: {
            thumbs: {
                width: 80,
                height: 80
            }
        },
        beforeShow: function() {
            var id = this.element.attr("id")
            if (id) {
                window.location.hash = id;
            }
        },
        beforeClose: function() {
            window.location.hash = "";
        }
    });

    if (thisHash) {
        $(thisHash).trigger('click');
    }
});
Comments