Reputation: 373
I have tested masonry.js with multiple columns successfully but with my application I have the need to add masonry items dynamically using .ajax()
JSON data. At the moment I have chosen to use .append()
to layout the initial masonry .items - The issue is that all of the masonry items are being output in a single column. The container size changes didn't help, even when increasing the amount of container width to more than enough, it will not style to 3 columns. Even when using the containerWidth function and using return containerWidth / 3
Here is the code:
jQuery(document).ready(function()
{
$.ajax(
{
url: 'http://localhost/project/index.php/controller/function',
dataType:'json',
success: function(data)
{
// limit - used to limit the json data ouput, as the json data contains an array of 50 objects.
var limit = 11;
$.each(data.results, function(index,item)
{
if (index > limit) return false;
// A bunch of var assignments mainly for data_*
$("#content").append('<div class="item ' + index + '"><a href="'
+ data_link + '">'
+ data_title + '</a><p>'
+ data_desc + '</p><img style="height:100; width:100;" src="'
+ data_img + '"><p>'
+ data_date + 'Source: '
+ data_source + '</p></div>');
});
}
});
$('#content').masonry({
itemSelector: '.item',
isAnimated: !Modernizr.csstransitions,
columnWidth: 350
}).imagesLoaded(function() {
$('#content').masonry('reload');
});
});
Here is the HTML in the view:
<header> // FYI I am using CodeIgniter & Twitter Bootstrap
</header>
<div class="container" style="padding-top:145px; width:1300px; margin-left:auto; margin-right:auto;">
<div id="content" class="container-mason clearfix">
</div>
</div>
</div>
Here are the styles I from masonry.js - changed container
to container-mason
in styl.css to avoid any possible conflict with bootstrap
#container-mason {
background: #FFF;
padding: 5px;
margin-bottom: 20px;
border-radius: 5px;
clear: both;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
<style >
#content
{
margin: 0 auto;
width: auto;
}
.item {
height: 300px;
width: 300px;
border: 2px #0099cc solid;
margin-bottom:10px;
left: 10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-transition: left .4s ease-in-out, top .4s ease-in-out .4s;
-moz-transition: left .4s ease-in-out, top .4s ease-in-out .4s;
-ms-transition: left .4s ease-in-out, top .4s ease-in-out .4s;
-o-transition: left .4s ease-in-out, top .4s ease-in-out .4s;
transition: left .4s ease-in-out, top .4s ease-in-out .4s;
}
</style>
Thanks for the help!!
UPDATE
Here is the JsFiddle http://jsfiddle.net/8CjT6/5/
Upvotes: 2
Views: 2599
Reputation: 388406
I think you have missed the style float: left
for the items class.
Check this fiddle.
Upvotes: 2