desandro / masonry

:love_hotel: Cascading grid layout plugin
https://masonry.desandro.com
16.27k stars 2.11k forks source link

Some bricks (one) of my masonry are not well positioned at the load #1178

Open anthony-roux opened 1 year ago

anthony-roux commented 1 year ago

Hello,

I use Masonry on shopify x storyblok (headless CMS) and the worry I encounter that at the 1st load a certain image positions badly. Always the same place, it is not related to the image. If I resize my screen manually, the Masonry wall is perfectly repositioned.

image

MY JS : `module.exports = class GalleryV2 extends Component { onInit(el) { this.el = el; }

onLoad() { if (window.matchMedia("(min-width: 768px)").matches) { this.galleryMasonryV2(); } }

galleryMasonryV2() { var msnry2 = new Masonry(this.el, { itemSelector: ".js-grid-item", columnWidth: ".grid-sizer", gutter: ".gutter-sizer", percentPosition: true, initLayout: false, });

// add event listener for initial layout
msnry2.on("layoutComplete", function() {
  document
    .querySelector(".js-gallery-v2")
    .classList.add("masonry-is-complete");
});

// trigger initial layout
// imagesLoaded( this.el, function() {
//   msnry2.layout();
// });

imagesLoaded(this.el).on("progress", function() {
  // layout Masonry after each image loads
  msnry2.layout();
});

} }; `

MY HTML/LIQUID

`

`

MY SCSS :

`.c-gallery { margin-bottom: -10px; margin-right: -10px;

@include mq($until:tablet){ margin-bottom: 0; margin-right: 0; }

@include mq($from:mobile){ height: 0; overflow: hidden; opacity: 0; }

&.masonry-is-complete { @include mq($from:mobile){ opacity: 1; } }

&:after { content: ''; display: block; clear: both; }

.grid-sizer, .js-grid-item { width: calc(25% - 10px);

@include mq($until:mobile){
  width: 100%;
}

}

.gutter-sizer { width: 10px; }

.js-grid-item--width2 { width: calc(75% - 10px);

@include mq($until: mobile){
  width: 100%;
}

}

&__item { margin-bottom: 10px; float: left;

@include mq($until: mobile){
  margin-bottom: 0;
} 

&:hover {
  .c-gallery__legend {
    opacity: 1;
  }
}

}

.c-galleryitem + .c-galleryitem { @include mq($until:mobile){ margin-top: 20px; } }

&__legend { opacity: 0;

 @include mq($until:tablet){
  opacity: 1;
 }  

} }

.c-gallery--v2 { .grid-sizer, .js-grid-item { width: calc(33.33% - 10px);

@include mq($until:mobile){
  width: 100%;
}

} }`

Thanks for your help !

Gabin-traverse commented 1 year ago

Hello, I have the same probleme. First i just whant to thank the fact that is open source and updated.

For me it's on many brick and only on Chrome. Sometimes i have no height on the div. https://www.gabintraverse.art/

But when i resize the window it comme back at is place.

I try many different option to launch the masonry grid, before or after the images are loads, but that didn't fix the bug.

Is it possible to just stop making the script change the height of my div ? Because im ussing a VH responsive height so i dont need resize in pxl.

Have a nice day Gabin.

Capture d’écran 2022-12-12 104219 Capture d’écran 2022-12-12 104246 Capture d’écran 2022-12-12 104311 Capture d’écran 2022-12-12 104323 Capture d’écran 2022-12-12 105005 Capture d’écran 2022-12-12 104355 Capture d’écran 2022-12-12 104412 Capture d’écran 2022-12-12 104435