2

WordPress と Zurb Foundation を利用するカスタム ギャラリーを構築しています。

  • テンプレートには、カスタム投稿のページ アーカイブが表示されます。カスタム クエリは、リンクされたサムネイルのグリッドをページに取り込みます。
  • Foundation 4 の Reveal モーダル プラグインと WordPress の AJAX を使用して、コンテンツを Reveal-modal div に追加します。
  • .previous-sketchモーダル ウィンドウの,リンクをクリックすると.next-sketch、対応する隣接する投稿が表示されます。

上記の作業を行うことができましたが、1 つのモーダル ウィンドウを開いて投稿を循環すると、同期がずれ始めることがわかりました。モーダルウィンドウが表示されているときに暗い背景.reveal-modal-bgが表示されず、動作がやや奇妙です。

この問題について何か特別な洞察があれば、事前に感謝します。

更新:私はこれにもう少し取り組むことができました.ほとんどの場合、うまくいくようです. 私のソリューションは最もエレガントではないと感じていますが。

私のjQueryコードは以下です。

(function($) {

 $.fn.displayPost = function() {
    event.preventDefault();
    var post_id = $(this).data("id");
    var id = "#" + post_id;

    // Check if the reveal modal for the specific post id doesn't already exist by checking for it's length
    if($(id).length == 0 ) {
        // We'll add an ID to the new reveal modal; we'll use that same ID to check if it exists in the future.
        var modal = $('<div>').attr('id', post_id ).addClass('reveal-modal').appendTo('body');
        var ajaxURL = MyAjax.ajaxurl;
         $.ajax({
            type: 'POST',
            url: ajaxURL,
            data: {"action": "load-content", post_id: post_id },
            success: function(response) {
                modal.empty().html(response).append('<a class="close-reveal-modal">&#215;</a>').foundation('reveal', 'open');

                modal.bind('opened', function() {
                    // Trigger window resize to reset the left margin.  
                    $(window).trigger('resize');
                    var left;
                    left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
                    $(id).css({
                        left:left + $(window).scrollLeft()
                    });

                return false;
                });
            }
        });
    }
     //If the div with the ID already exists we'll just open it.
     else {
         $(id).foundation('reveal', 'open');
     }

     // Recalculate left margin on window resize
     $(window).resize(function(){
        var left;
        left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
        $(id).css({
            left:left + $(window).scrollLeft()
        });
     });
}

})(jQuery);

// Apply the function when we click on the .reveal link
jQuery(document).on("click", ".reveal,.secondary", function() {
    jQuery(this).displayPost();
});

// Use Reveal's built in function to close the div when we click our paging links
jQuery(document).on("click", ".secondary", function() {
    var id = jQuery(this).closest("div").attr("id");
    jQuery(id).foundation('reveal', 'close');

});
4

1 に答える 1

1

ここの誰かがおそらくこれを行うためのより良い方法を知っていると思いますが、これを理解しようとしている他の誰かのために、私はなんとか解決策を見つけました。完全にテストしていませんが、動作します。

この関数は、Foundation 4 Reveal の機能をいくつかの方法で拡張します。

  • WordPress AJAX を使用してコンテンツを動的に取り込み、モーダル div を設定します。
  • モーダル div を中央に配置し、可変幅にすることができます。
  • モーダル ウィンドウからページング ナビゲーションを追加します。トリガーされると、開いているモーダル ウィンドウが閉じられ、前/次のモーダル コンテンツが開きます。

コードは次のとおりです。

(function($) {

    $.fn.displayPost = function() {

    event.preventDefault();

    var post_id = $(this).data("id");
    var id = "#" + post_id;

    // Check if the reveal modal for the specific post id doesn't already exist by checking for it's length
    if($(id).length == 0 ) {
        // We'll add an ID to the new reveal modal; we'll use that same ID to check if it exists in the future.
        var modal = $('<div>').attr('id', post_id ).addClass('reveal-modal').appendTo('body');
        var ajaxURL = MyAjax.ajaxurl;
         $.ajax({
            type: 'POST',
            url: ajaxURL,
            data: {"action": "load-content", post_id: post_id },
            success: function(response) {
                modal.empty().html(response).append('<a class="close-reveal-modal">&#215;</a>').foundation('reveal', 'open');
                modal.bind('opened', function() {
                    // Reset visibility to hidden and set display: none on closed reveal-modal divs, for some reason not working by default when reveal close is triggered on .secondary links  
                    $(".reveal-modal:not('.reveal-modal.open')").css({'visibility': 'hidden', 'display' : 'none'})
                    // Trigger resize 
                    $(window).trigger('resize');
                return false;
                });
            }
        });
    }
     //If the div with the ID already exists just open it.
     else {
         $(id).foundation('reveal', 'open');
     }

     // Recalculate left margin on window resize to allow for absolute centering of variable width elements
     $(window).resize(function(){
         var left;
            left = Math.max($(window).width() - $(id).outerWidth(), 0) / 2;
            $(id).css({
                left:left + $(window).scrollLeft()
            });
     });
    }

     })(jQuery);

     // Apply the function when we click on the .reveal link
     // (document).ready won't work on any reveal-modal divs added subsequently
     // after page load via AJAX so use .on instead.
     jQuery(document).on("click", ".reveal,.secondary", function() {
      jQuery(this).displayPost();

     });

      // Close open modal via secondary paging links; target parent div id.
      jQuery(document).on("click", ".secondary", function() {
        var id = jQuery(this).closest("div").attr("id");
      jQuery(id).foundation('reveal', 'close');
      });

更新: AJAX コンテンツをロードするための php 関数を追加

  public function __construct() {
        add_action( 'wp_ajax_load-content', array($this, 'load_ajax_content' ));
        add_action( 'wp_ajax_nopriv_load-content', array($this, 'load_ajax_content' ));
    }



        /**
         * Function to call the content loaded for logged-in and anonymous users
        */
        public function load_ajax_content ( $post_id ) {

            $post_id = $_POST[ 'post_id' ];

            if (has_post_thumbnail($post_id)) {
                $sketch_id = get_post_thumbnail_id($post_id);  
                $attachment = get_post( $sketch_id );
                $caption = $attachment->post_excerpt;
                $response = '<figure>'. get_the_post_thumbnail($post_id, 'large-sketch') .'<figcaption><p>'. $caption .'</p></figcaption></figure>' . $this->paging_link_nav( $post_id );
                echo $response;
            }

            die(1);
         }

うまくいけば、他の誰かがこれが役に立つと思う...

于 2013-06-27T07:25:16.463 に答える