0

WordPress サイトの垂直リズムを整理するために、Web サイトに次の jQuery コードがあります。

$(window).bind('load', function(){
        $(".wp-caption").each(function() {
                /* variables */
                var this_img   = $(this);
                var baseline   = 24;
                var img_height = this_img.outerHeight();
            /* do the maths */
            var remainder  = parseFloat(img_height%baseline);

            /* how much do we need to add? */
            var offset     = parseFloat(baseline-remainder);

            var top_offset = Math.round(offset/2);
            var bottom_offset = offset - top_offset;

            /* apply the margin to the image */
            this_img.css("margin-bottom",bottom_offset+"px");
            this_img.css("margin-top",top_offset+"px");
        });
});

これをもっと再利用できるようにしたいので、すべてのコードを に詰め込むのではなく.bind$(".wp-caption").verticalRhythm(24). これがプラグインが必要であることを意味するのかどうか、もしそうなら、それが正確に何を意味するのかはわかりません.

どんなアイデアや助けもいただければ幸いです

4

2 に答える 2

0

次のようなものを試してください:

$.fn.extend({
  verticalRhythm : function(baseline){
    $(window).bind('load', function(){
      $(this).each(function(){
        var this_img = $(this), img_height = this_img.outerHeight();
        var remainder = parseFloat(img_height%baseline);
        var offset = parseFloat(baseline-remainder);
        var top_offset = Math.round(offset/2);
        var bottom_offset = offset - top_offset;
        this_img.css("margin-bottom",bottom_offset+"px");
        this_img.css("margin-top",top_offset+"px");
      });
    });
  }
});
于 2013-08-08T22:31:13.177 に答える