macy.js icon indicating copy to clipboard operation
macy.js copied to clipboard

Grid dimensions only calculated on window resize

Open richardpbull opened this issue 4 years ago • 9 comments

Hello, I must be doing something wrong as everywhere else I read about this it is working perfectly. I can't get the instance to resize on all images loading. The images remain overlapped until I resize the browser. Video below:

https://user-images.githubusercontent.com/5243046/103400813-9787e380-4aea-11eb-909d-b275469de034.mov

Here's the code being used on this example: https://www.ultratourmonterosa.com/gallery/

Help appreciated! Thank you.

var macy_instance = Macy({
	container: '#photos',
	trueOrder: false,
	waitForImages: true,
	margin: 6,
	columns: 3,
	breakAt: {
			1200: 3,
			940: 3,
			520: 2,
			400: 1
		}
	});
	macy_instance.runOnImageLoad(function () {
		console.log('I only get called when all images are loaded');
		macy_instance.recalculate(true, true);
	});

richardpbull avatar Dec 31 '20 08:12 richardpbull

Confirm the same problem.

let macyInstance = new Macy({
    'columns': 3,
    'container': '#rdba-dashboard-row-normal'
});

ve3 avatar Jan 25 '21 08:01 ve3

I'm having the same issue, any fix?.

image

kuipumu avatar Mar 08 '21 00:03 kuipumu

hey @richardpbull @kuipumu

You could try running the recalculate event on each image load; the issue seems to be if an image has failed to load (or if it does it too quick) then it can cause issues with the event finishing you can try doing it like so here:

macy_instance.runOnImageLoad(function () {
  console.log('Every time an image loads I get fired');
  macy_instance.recalculate(true, true);
}, true);

jrmd avatar Mar 08 '21 09:03 jrmd

Hi @richardpbull adding the recalculate event didn't fixed the issue, the problem was that if no src is present on each image macy.js fails to give a correct height, and unless you resize the windows the grid will be shown has previously captured.

kuipumu avatar Mar 10 '21 17:03 kuipumu

same issue, src is present in all images

c8e4d2a avatar May 05 '21 10:05 c8e4d2a

Dirty Fix:

macyInstance.runOnImageLoad(function () {
            macyInstance.recalculate(true, true);
            var evt = document.createEvent('UIEvents'); 
            evt.initUIEvent('resize', true, false, window, 0); 
            window.dispatchEvent(evt);
    }, true);

c8e4d2a avatar May 05 '21 10:05 c8e4d2a

This dirty code didn't work for me. I fixed it by setTimeout function. Whenever I'm fetching the images and got the response after I setTimeout with 1 sec and define macy instance in setTimeout function. You can try also.

Syed25794 avatar Feb 25 '23 17:02 Syed25794

Setting waitForImages: false seems to solve the issue for me.

joworeiter avatar Jul 03 '23 16:07 joworeiter

I had this issue as well, and in my case, it happened because I was using VUE. Whenever I added new elements to the grid or tried to use the recalculate() or reInit() functions, the changes weren't being reflected in the application due to Vue's reactivity system.

The only solution that worked for me was to create a reference (ref) for the grid element I was using and then use this ref as the key for the grid. Every time I called the recalculate() function, I updated the value of my ref.

I believe if you're using this library with a framework like Vue or React, the same thing might be happening. With React, I think the solution to the problem would be the same.

talissonf avatar Aug 18 '23 13:08 talissonf