- Joined
- Sep 3, 2014
- Messages
- 6,244
- Likes
- 13,130
- Degree
- 9
I have a fun riddle for you guys.
The Background
In the past, I created a comparison table. It has 3 columns and 4 rows. The cells in each row load and end up having different heights due to the amount of text and the images having different heights. So I used
All was well and good. Now I'm converting all my cool custom field stuff into Gutenberg Blocks, which means this comparison table is now within
The Mystery
While testing the new custom comparison table block, I noticed on maybe 1 of every 5 loads that the height of rows with images was getting messed up and being very tiny. This let me know that the height was being calculated before the real image was loaded. It was measuring while the 1px by 1px transparent image was still in there.
The scripts were loading in this order:
I started playing around and swapped the order of the scripts to be:
The Questions
This led me to wondering several things. We know that
Think about this. If
It seems like "everything is loaded" would exclude the big images to be swapped in. Does that mean I'm getting lucky on the timing and that someone on a tablet might see the table all messed up?
My timeline could be:
Final Question
If my assumption is right and I'm getting lucky on the timing, what can I do to ensure the height calculation only runs after the images are loaded?
I could run a couple delays. I could run the calculation at
Otherwise I'm kind of at a loss for a simple solution without digging into a ridiculous amount of event detection, which seems like overkill here.
The Background
In the past, I created a comparison table. It has 3 columns and 4 rows. The cells in each row load and end up having different heights due to the amount of text and the images having different heights. So I used
$(window).load
to fire a function that measures the heights of the cells in each row, finds the tallest one, and sets them all to be the height of the tallest one. This made the table pretty. I'd also run it any time the window was resized after 100ms to make sure it was always pretty.All was well and good. Now I'm converting all my cool custom field stuff into Gutenberg Blocks, which means this comparison table is now within
the_content()
. That means it's subject to Lazy Loading (and I can't tell it to skip over certain images with classes or I would. I'm using this one library due to how lightweight it is.The Mystery
While testing the new custom comparison table block, I noticed on maybe 1 of every 5 loads that the height of rows with images was getting messed up and being very tiny. This let me know that the height was being calculated before the real image was loaded. It was measuring while the 1px by 1px transparent image was still in there.
The scripts were loading in this order:
- table.js
- lazy-load.js
$(window).load
.I started playing around and swapped the order of the scripts to be:
- lazy-load.js
- table.js
The Questions
This led me to wondering several things. We know that
$(window).load
waits for all scripts and images and everything to be loaded before firing. So am I correct in thinking that the order of the scripts still mattered? So even though we're waiting for it all to be loaded, it's going to be parsed and fired off in the order it appears in the DOM, right?Think about this. If
$(window).load
is waiting for all images to load, does that include or exclude the big images that are being swapped in by the lazy-loading script, assuming that the lazy-loading script fires before the table.js script?It seems like "everything is loaded" would exclude the big images to be swapped in. Does that mean I'm getting lucky on the timing and that someone on a tablet might see the table all messed up?
My timeline could be:
- Everything is loaded
- lazy-load.js fires
- Because I'm on a fast connection, the new big images are swapped in before step 4
- table.js does the height measurement and makes the table pretty
Final Question
If my assumption is right and I'm getting lucky on the timing, what can I do to ensure the height calculation only runs after the images are loaded?
I could run a couple delays. I could run the calculation at
$(window).load
, then 500ms later, then 3 seconds later (or ever later to accommodate tablet users).Otherwise I'm kind of at a loss for a simple solution without digging into a ridiculous amount of event detection, which seems like overkill here.