0

このコードは、各 Colorbox 画像に URL (domain.com/#image) を与えます。これをコピーして貼り付けると、その URL でサイトに入ると、必要な写真が自動的に開きます。

すべてが意図したとおりに機能しますが、URL の末尾に #image がない場合でも、サイトの最初の画像が自動的に開きます。URL に #image がある場合にのみ画像を自動的に開くようにするには、このコードをどのように変更すればよいですか?

ありがとう!

コード:

jQuery(function (){
            var id, group;

            group = jQuery("a[rel='lightbox[63]']").colorbox({onComplete:function(){
                    // Get the image URL
                    with_ext = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
                    // Get the image url without the extension
                    without_ext =  with_ext.substring(0, with_ext.lastIndexOf("."));
                    // Redirect
                    window.location.hash = without_ext;
            }, onClosed: function(){
                    location.hash = '';
            }});

            id = location.hash.replace(/^\#/, '')+".jpg";

            group.filter('[href$="'+id+'"]').eq(0).click();
    });
4

1 に答える 1

0

わかりました。他の誰かがこの種の機能を必要とする場合は、次のとおりです。

jQuery(function (){
            var id, group;

            group = jQuery("a[rel='lightbox[63]']").colorbox({onComplete:function(){
                    // Get the image URL
                    with_ext = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
                    // Get the image url without the extension
                    without_ext =  with_ext.substring(0, with_ext.lastIndexOf("."));
                    // Redirect
                    window.location.hash = without_ext;
            }, onClosed: function(){
                    location.hash = '';
            }});

            if(window.location.hash) {

                    id = location.hash.replace(/^\#/, '')+".jpg";
                    group.filter('[href$="'+id+'"]').eq(0).click();

            } else {
                    return;
            }


    });
于 2013-01-28T10:16:42.543 に答える