砌体柱宽问题

时间:2015-08-10 03:39:35

标签: javascript jquery html css masonry

我有一个很好的砌体布局,尽管使用ImageLoaded(可能不正确),我仍然在初始加载时重叠。它修复了窗口调整大小,所以我想我只是在图像加载后没有正确调用重新排列。有人有任何想法吗?我觉得这应该很容易,但我是一个菜鸟:/

better solution

JS

$( function() {

  var $container = $('#container').masonry({
    itemSelector: '.item',
    columnWidth: '.item'
  });

  $( function() {
    var $items = getItems();
    $container.masonryImagesReveal( $items );
  });

});

$.fn.masonryImagesReveal = function( $items ) {
  var msnry = this.data('masonry');
  var itemSelector = msnry.options.itemSelector;
  // hide by default
  $items.hide();
  // append to container
  this.append( $items );
  $items.imagesLoaded().progress( function( imgLoad, image ) {
    // get item
    // image is imagesLoaded class, not <img>, <img> is image.img
    var $item = $( image.img ).parents( itemSelector );
    // un-hide item
    $item.show();
    // masonry does its thing
    msnry.appended( $item );
  });

  return this;
};


var imgcount=0

function getItem() {
  imgcount += 1;
  var item = '<div class="item">'+
    '<img src="http://engraminationindustries.com/clients/huron/images/work/Huron_' + 
      imgcount + '.jpg" /></div>';
  return item;
}

function getItems() {
  var items = '';
  for ( var i=0; i < 88; i++ ) {
    items += getItem();
  }
  // return jQuery object
  return $( items );

}

CSS

body { font-family: sans-serif; }

#container {
  background: #DDD;
}

.item {
  width: 33.333%;
  float: left;
}

.item-sizer{
  width: 33.333;
}

.item img {
  display: block;
  width: 100%;
}

button { font-size: 18px; } 

0 个答案:

没有答案