I've found this template that demonstrates the issue I'm having with jquery masonry and image layout. Take a look at this twitter bootstrap template page:
The very first time the page is loaded all the images are stacked on each other until a page refresh or re-size is done. The images are then displayed correctly.
Here is my HTML and jQuery that has the same problem:
HTML
<div class="gallery-masonry masonry" style="position: relative; min-height:100px; height: auto;">
<div id="loading">Loading ...</div>
</div>
jQuery
$.post("functions/photo_functions.php", { f: 'getallphotos'}, function(data) {
$('#loading').hide();
if(data) {
$.each(data.images, function(i, image) {
var img_block = '<div class="item masonry-brick" style="position: absolute; top: 0px; left: 0px;">' +
'<a href="#" class="thumbnail"><img src="'+image.url+'" alt=""></a>' +
'<div class="actions">' +
'<a title="" href="#" class="tip-top" data-original-title="Edit"><i class="icon-pencil icon-white"></i></a>' +
'<a title="" href="#" class="tip-top" data-original-title="Remove"><i class="icon-remove icon-white"></i></a>' +
'</div>' +
'</div>';
$(".gallery-masonry").append(img_block);
});
$('.gallery-masonry').masonry({
itemSelector: '.item',
isAnimated: true,
isFitWidth: true
});
}
}, "json");
Any idea why this is happening and how might I fix it?
I've found this template that demonstrates the issue I'm having with jquery masonry and image layout. Take a look at this twitter bootstrap template page:
The very first time the page is loaded all the images are stacked on each other until a page refresh or re-size is done. The images are then displayed correctly.
Here is my HTML and jQuery that has the same problem:
HTML
<div class="gallery-masonry masonry" style="position: relative; min-height:100px; height: auto;">
<div id="loading">Loading ...</div>
</div>
jQuery
$.post("functions/photo_functions.php", { f: 'getallphotos'}, function(data) {
$('#loading').hide();
if(data) {
$.each(data.images, function(i, image) {
var img_block = '<div class="item masonry-brick" style="position: absolute; top: 0px; left: 0px;">' +
'<a href="#" class="thumbnail"><img src="'+image.url+'" alt=""></a>' +
'<div class="actions">' +
'<a title="" href="#" class="tip-top" data-original-title="Edit"><i class="icon-pencil icon-white"></i></a>' +
'<a title="" href="#" class="tip-top" data-original-title="Remove"><i class="icon-remove icon-white"></i></a>' +
'</div>' +
'</div>';
$(".gallery-masonry").append(img_block);
});
$('.gallery-masonry').masonry({
itemSelector: '.item',
isAnimated: true,
isFitWidth: true
});
}
}, "json");
Any idea why this is happening and how might I fix it?
Share Improve this question edited Sep 17, 2014 at 2:21 JGallardo 11.4k11 gold badges86 silver badges99 bronze badges asked May 6, 2013 at 2:25 PaulPaul 11.7k32 gold badges92 silver badges145 bronze badges2 Answers
Reset to default 16Use imagesLoaded() to triggered masonry after all images are loaded.
(imagesLoaded()
is provided by the script http://github.com/desandro/imagesloaded.)
$('.gallery-masonry').imagesLoaded( function(){
$('.gallery-masonry').masonry({
itemSelector: '.item',
isAnimated: true,
isFitWidth: true
});
});
The accepted answer has missing steps. So here is a step-by-step.
- Go to the imagesloaded repo and download the minified version at https://github.com/desandro/imagesloaded/blob/master/imagesloaded.pkgd.min.js.
- Add that library to your project.
- At the bottom of your page call the file
```[Adjust path to your javascript files]
<script src="/js/masonry.pkgd.min.js"></script>
<script src="/js/imagesloaded.pkgd.min.js"></script>
<script>
$('.gallery-masonry').imagesLoaded( function(){
$('.gallery-masonry').masonry({
itemSelector: '.item',
isAnimated: true,
isFitWidth: true
});
});
</script>