Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
anthony-roux opened this issue Nov 23, 2022 · 1 comment
Open

Comments

@anthony-roux
Copy link

anthony-roux commented Nov 23, 2022

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

`









{% for item in item.images %}
{% capture element %}{% cycle 'odd', 'even' %}{% endcapture %}
{% assign imgSize = item.img_size | default: 'portrait' %}

							{% assign img = item.image %}
							{% if img != blank %}
								{% assign heightImg = img.height %}
								{% assign widthImg =  img.width %}
								{%- capture viewBox -%}
									{{ widthImg }}x{{ heightImg }}
								{%- endcapture -%}
								{% assign viewBox = viewBox | split: 'x' %}
								{% if imgSize == 'portrait' %}
									{% assign sizes = '433x649|866x1298' | split: '|' %}
									{% assign width = '433w|866w' | split: '|' %}
								{% else %}
									{% assign sizes = '433x289|866x578' | split: '|' %}
									{% assign width = '433w|866w' | split: '|' %}
								{% endif %}
								{% assign heightImg = img.height %}
								{% assign widthImg = img.width %}
								{%- capture viewBox -%}
									{{ widthImg }}x{{ heightImg }}
								{%- endcapture -%}
								{% assign viewBox = viewBox | split: 'x' %}
								{% assign src = img.src %}
								<div class="gallery__img">
									{% assign template_name = 'srcset_img' | prepend : template_prefix %}
									{% include template_name  %}
								</div>
							{% endif %}

							{% if item.caption != blank %}
								<div class="c-gallery__legend u-transition u-pad-sm u-pad-xs--sm u-absolute u-absolute-bottom u-absolute-left {% if item.color_caption == true %} u-white{% endif %}">
									{{ item.caption }}
								</div>
							{% endif %}
						</div>
					{% endfor %}
				</div>
			</div>
		</div>
	</div>
</div>
`

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-gallery__item + .c-gallery__item {
@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 !

@anthony-roux anthony-roux changed the title Certaines briques (une) de mon masonry se positionne mal au load Some bricks (one) of my masonry are not well positioned at the load Nov 23, 2022
@Gabin-traverse
Copy link

Gabin-traverse commented Dec 11, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants