我已经成功测试了多列的masonry.js,但是在我的应用程序中,我需要使用.ajax()
JSON数据动态添加砌体项。目前我选择使用.append()
来布局初始砌体.items - 问题是所有砌体项目都在一列中输出。容器大小的变化没有帮助,即使将容器宽度增加到足够多,也不会将样式设置为3列。即使使用containerWidth函数并使用return containerWidth / 3
以下是代码:
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');
});
});
以下是视图中的HTML:
<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>
以下是来自masonry.js的样式 - 在styl.css中将container
更改为container-mason
,以避免与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>
感谢您的帮助!!
更新
这是JsFiddle http://jsfiddle.net/8CjT6/5/