I am building a tumblr theme from scratch and I'm using masonry
. Although, sometimes it overlaps pictures, it doesn't add any gutter, calculates top wrong, and I'm not sure of what's happening.
I tried adding imagesloaded
library but I don't think it's woking since it's still overlapping pictures, etc, sometimes.
Images width and height are defined on the CSS since they're all the same width but different heights.
HTML
<div class="masonry js-masonry" data-masonry-options='{ "isFitWidth": true, "gutter": 14}' id="content">
{block:Posts}
<div class="container" id="{postID}">
{block:Photo}
<div class="photo inner">
<a href="{permalink}">
<img src="{block:indexpage}{PhotoURL-500}{/block:indexpage}{block:permalinkpage}{PhotoURL-HighRes}{/block:permalinkpage}" alt="{PhotoAlt}">
</a></div>
{/block:Photo}
</div>
{/block:Posts}
</div>
JS
var container = document.querySelector('#content');
var msnry;
imagesLoaded( container, function() {
msnry = new Masonry( container );
});
Please use following code with imagesloaded plugin:
It is the easiest solution when you use masonry as
<div class="masonry js-masonry" data-masonry-options='{ "isFitWidth": true, "gutter": 14}' id="content">....</div>
I had a similar issue with Masonry, with a project that is now unfortunately offline, so I cannot give you a URL. I dug up the code, and my workaround was:
That's jQuery-flavoured; it doesn't look like you're using jQuery, but I hope it helps.
Update:
I just found this section of the masonry docs, which I don't remember from my time with Masonry:
http://masonry.desandro.com/appendix.html#imagesloaded
If I wait for your tumblr page to finish loading, then open the console, and type in:
then the layout works and the overlap vanishes. So if we wait long enough, then calling the layout method fixes your problem. The challenge is to work out what we need to wait for! Some options:
Just use a timeout - a bit hacky!
window.setTimeout(function() { $tumblelog.masonry('layout') }, 1000)
and adjust the value of the timeout until it works.
Second update
Here's a fiddle showing how to redo the layout of Masonry after fonts and images have both loaded:
http://jsfiddle.net/sifriday/5xotLj23/3/
I don't know how tumblr loads content into your template, but I reckon the problem arises due to a mess of complexity associated with inserting content into the DOM, so I have tried to recreate that by adding your images using JS. Within the fiddle, there's a JS line that if you comment it out then re-run the fiddle, seems to recreate the overlap problem.
Quick Tumblr Hack
In your template where you are working with docReady, try this:
After two seconds that will call Masonry to redo the layout. If if works, then you know the problem is definitely something to do with waiting for the right combination of things to load.
Essential code
To integrate my fiddle,
Remove your CSS that loads the Google Font.
Use this JS:
Update the font definitions in this line: