Lazy Images: Layouts using imagesLoaded.js and Masonry.js not working as expected
Themes using imageLoaded.js with masonry.js for layouts can end up with gaps and overlaps when Lazy Images are enabled.
Steps to reproduce the issue
- Activate a theme using Masonry and imagesLoaded JavaScript libraries for layout. From themes on WP.com, one good option is Baskerville 2.
- Make sure Lazy load images are activated.
- Make sure you have a number of posts with featured images.
- Load the blog posts page.
What I expected
The posts would be displayed evenly spaced, like here: https://baskerville2demo.wordpress.com/
What happened instead
The posts are often further apart, or slightly overlapped. Resizing the browser window will correct the issue.
On page load (note the gaps, and the overlap in the bottom-right corner):

After resizing the browser:

Originally reported for the Opti theme here:
- 2365-wpcom-premium-themes
- Follow-up ticket: 1143441-zen.
Similar issue on 4582699-hc. User has a theme using masonry.js. When lazy loading images is enabled, no featured images appear at all:

Switching themes fixes the issue.
follow up at 1242521-zen
Hi @sdixon194! I'm not sure that's the same issue -- the original report was that the masonry layout didn't lay itself out correctly. This sounds a bit different, because the images aren't showing at all.
We ran into something like this with one of our premium themes, where JavaScript in the theme was clashing with lazy loading. Looking at their theme's JS, it already includes its own lazy loading image code -- that could be the culprit!
Similar issue is happening with Rebalance, which uses Masonry & imagesLoaded.
- User report: https://en.forums.wordpress.com/topic/portfolio-thumbnails-overlapping/
- User's site: https://climbmountains.blog/copywriter-portfolio/
- Discussion: p1531916072000181-slack-themes
Originally reported here: https://github.com/Automattic/themes/issues/207
User reported similar issue here - 5218710-hc
However, I wasn't able to replicate issue on my end but user shared these screenshots - https://sandeepa953175044.files.wordpress.com/2018/07/homepage1.png https://sandeepa953175044.files.wordpress.com/2018/07/homepage2.png https://sandeepa953175044.files.wordpress.com/2018/07/homepage3.png
Hi @krutidugade! Only business & self-hosted sites can enable Lazy Load images separately; looking at that user's site, I think they may be experiencing this theme issue, here: https://github.com/Automattic/themes/issues/208
I've moved the above report over there!
@laurelfulford Thank you, I missed it completely!
Also affecting Spatial: https://wordpress.com/themes/spatial
Original trac ticket: https://wpcom-premium-themes.trac.automattic.com/ticket/2464 User-report: 1369452-zen
Reported again in 1409732-zen, using Baskerville2
I disabled Lazy Load in Jetpack, and the grids seem to have returned to normal.
reported in 1424789-zen in Garfunkel, Penguin GeneratePress with the upgrade and Cenote. Tested and verified to confirm all that I could.
@eliorivero I see you touched this issue in the past, do you know if there are any plans to include a fix in future Jetpack releases?
Right now there's nothing planned. I'll try to look into this as soon as I have some time.
Any updates? :)
Also reported in 1625283-zen with smartblog theme. cc: @isabellachen
Another repport here: 9661791-hc with the Ippo theme . Disabling lazy load worked.
Similar issue here HC-9796665
Is there an event triggered in Jetpack's Lazy Load? If so imagesLoaded and/or Masonry can rebuilt itself.
Update: listen for the jetpack-lazy-loaded-image Lazy Images event and rebuild your Masonry.
$(document.body).on('jetpack-lazy-loaded-image', function () {
// Example of my $blocks variable (you'll have to update the classes to match your theme/html)
// var $blocks = $('.two-column .gallery-wrapper,.three-column .gallery-wrapper');
$blocks.imagesLoaded(function () {
$blocks.masonry('reloadItems').masonry('layout');
});
});
@thefrosty where does this code go?
@isabellachen you can put it anywhere you like. If you're theme allows custom JS in the settings page, or if you have access to a custom JS file in a functionality plugin etc.
Actual for the external theme: https://wordpress.org/themes/minimal-grid/
1988452-zen