スキップしてメイン コンテンツに移動

PhotoSwipeでテキストリンクから特定の画像のPhotoSwipeギャラリーを起動する方法

【PhotoSwipeとは】

スマホサイトで写真ギャラリーを設置したいときに使えるライブラリ
スワイプで画像がめくれて便利!
レスポンシブサイトの画像ギャラリーにオススメ。
(optionに、imageScaleMethod: "fitNoUpscale" とすれば画像が最大100%で表示されるのでPCサイトでも使えます。)
https://github.com/codecomputerlove/PhotoSwipe


【実現したいこと】

通常は画像サムネイルのクリック(or タップ)でPhotoswipeギャラリーを起動します。
それとは別に、新着情報などのテキストリンクから特定の画像のPhotoSwipeギャラリーを起動したい。


【方針】

var instance = $("#Gallery a").photoSwipe(options);
instance.show(0);
で、最初のPhotoSwipeギャラリーを起動できることから、テキストリンクに任意の要素(data-swipeとする)と、クラス(.swipe-targetとする)を設置し、.swipe-targetの上から順番にdata-swipeに番号を振ってゆき、.swipe-targetがクリックされたら、data-swipeから番号xを取得し、instance.show(x)で目的のPhotoSwipeギャラリーを起動する。


【HTML】

ヘッダー(もしくはフッター)部分(jQueryバージョンを使用)

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/vendor/jquery-1.8.2.min.js"><\/script>')</script>
<script src="/js/vendor/klass.min.js"></script>
<script src="/js/vendor/code.photoswipe.jquery-3.0.5.min.js"></script>
・・・

画像のサムネイル部分

<ul id="Gallery">
    <li><a href="A.png"><img src="A_thumb.png" alt=""></a></li>
    <li><a href="B.png"><img src="B_thumb.png" alt=""></a></li>
    <li><a href="C.png"><img src="C_thumb.png" alt=""></a></li>
</ul>
(サムネイルをクリックすると普通にPhotoSwipeギャラリーが起動します。)


新着情報テキスト部分

<ul id="GalleryTxt">
    <li><a href="#" class="swipe-target" data-swipe="">Aをアップしました。</a></li>
    <li><a href="#" class="swipe-target" data-swipe="">Bをアップしました。</a></li>
    <li><a href="#" class="swipe-target" data-swipe="">Cをアップしました。</a></li>
</ul>
(このテキストリンクをクリックして対象の画像をPhotoSwipeギャラリーで起動したい。)


【JavaScript】(jQuery)

(function (window, $, PhotoSwipe) {
    $(document).ready(function () {
        // 通常の設定部分
        var options = {};
        var instance = $("#Gallery a").photoSwipe(options);

        // data-swipeに番号をふる
        var swipe_cnt = 0;
        $("#GalleryTxt .swipe-target").each(function(){
            $(this).attr("data-swipe", swipe_cnt);
            swipe_cnt++;
        });

        // .swipe-targetがクリックされたら当該番号のPhotoSwipeギャラリーを起動する
        $(".swipe-target").click(function () {
            var showNo = $(this).attr("data-swipe");
            instance.show(parseInt(showNo));
            return false;
        });
    });
}(window, window.jQuery, window.Code.PhotoSwipe));

以上です。

コメント

このブログの人気の投稿

iframeに異なるドメイン(クロスドメイン)のコンテンツを読み込んで高さを自動調節する方法

趣旨 iframe(親)に異なるドメインのページ(子)を読み込んで、親フレームにスクロールが出ないよう、iframeの高さを子ページの高さに自動調節する 方法 普通にやると、 「Uncaught SecurityError: Blocked a frame with origin "http://hoge.com" from accessing a frame with origin "http://fuga.com".  Protocols, domains, and ports must match. 」 などと怒られてしまいますのでこちらを参考に http://stackoverflow.com/questions/18456498/how-can-i-change-the-size-of-an-iframe-from-inside http://i556tips.tumblr.com/post/15710917018/%E3%82%AF%E3%83%AD%E3%82%B9%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3%E3%81%A7iframe%E3%81%AE%E9%AB%98%E3%81%95%E3%82%92%E8%87%AA%E5%8B%95%E8%AA%BF%E7%AF%80%E3%81%99%E3%82%8Bjavascript コード hoge.com(親) <html> <head>     <title>親</title>     <script src="js/jquery.js"></script>     <script>         window.addEventListener('message', function(e) {         var iframe = $("#fugapage");         var eventName = e.data[0];         var data = e.data[1];         switc

MovableTypeでクエリーからGET値をPHPで取得して、そのパラメータで記事を動的に出力する方法

【実現したいこと】 MovableTypeで、クエリー(http://◯◯◯.com/?aaa=bbbの?以下)からパラメータをPHPで取得して、MovableTypeの変数に引き渡して、取得したパラメータで特定カテゴリの記事を動的に出力する。 【呼び出し元】(ウェブページ、インデックステンプレートなどから) <a href="<$mt:BlogURL$>list/?category=hogehoge">カテゴリー「ほげほげ」の記事を動的に出力</a> ↓ 【呼び出し先】(インデックステンプレートなどで) <mt:Ignore><!--自分のカテゴリ(ディレクトリ名)取得--></mt:Ignore> <mt:SetVarBlock name="mycategory" strip_linefeeds="1" trim="1"><?php $mycategory = $_GET['category']; echo $mycategory; ?></mt:SetVarBlock> <mt:Ignore><!--自分のカテゴリー(ラベル名)取得--></mt:Ignore> <mt:SetVarBlock name="mycategorylabel" strip_linefeeds="1" trim="1"><?php if ($mycategory == "hogehoge") {     $my_category_label = "ほげほげ"; } else if ($mycategory == "fugafuga") {     $my_category_label = "ふがふが"; } else if () {     ... } echo $my_category_label; ?>&l

HTMLの要素を下揃えする方法

【実現したいこと】 上の図において、要素1と要素2の下面を揃えたい。 【よく見る方法は...】 ・親要素に「position:relative;」 ・子要素に「position:absolute;」、「bottom:0;」 とすると、左右のdivの高ささえ揃えておけば、下面を合わせられますが、BootstrapなどのCSSフレームワークを使用していたりして、既存のスタイルと干渉してしまう場合に、JavaScript(jQuery)で実現する方法を記載します。 【JavaScriptでの実現方法(考え方)】 上の図において、スクリプトを使用し、「A」、「B」、「C」を高さを取得し、計算により「D」の高さを取得します。 「D」=「A」-「B」-「C」 この「D」を要素2の「margin-top」に追加してあげます。 今回は、レスポンシブ(RWD)に対応するため、ウィンドウの幅を変えた時にも「margin-top」を設定し直す仕様とします。 【実装】 jQuery(document).ready(function() {   adjustPosition();     $(window).resize(function () {     adjustPosition();   });   function adjustPosition(){       var heightA = $("要素1").outerHeight();       var heightB = $("テキスト部").outerHeight();       var heightC = $("要素2").outerHeight();       var heightD = heightA - heightB - heightC;       if (heightD > 0) {         $("#要素2").css("margin-top", heightD + "px");       }   } }); 【解説】 特に難しいことはしていませんが、初回とウィンドウリサイズ時に、それぞれの要素の高さ