基于jQuery masonry,我得到以下内容:
jQuery的:
$('#container').masonry({
itemSelector : '.item',
columnWidth: 240
});
jQuery ajax:(jquery-masonry在页面加载后第一次发生success
。如果success
在没有页面的情况下再次发生,则jquery-masonry不起作用重装。)
success: function( widget_shell )
{
if( widget_shell.d[0] ) {
$( "#container" ).empty();
var i = 0;
// creating the div elements
for ( i = 0; i <= widget_shell.d.length - 1; i++ ) {
var j = Math.floor(Math.random() * 200) + 50
var $widget = $( "<div class='item col1' style='height:" + j + "px'></div>" ).appendTo( $( "#container" ) );
$( "<span>" + widget_shell.d[i].widget_id + " - " + j + "</span>" ).appendTo( $widget );
}
// initialising the jquery masonry plugin
$('#container').masonry({
itemSelector : '.item',
columnWidth: 240
});
}
}
HTML:(通过jQuery-ajax动态创建)
<div id="container" style="position: relative; height: 0px;" class="masonry">
<div class="item col1" style="height:154px"><span>39 - 154</span></div>
<div class="item col1" style="height:100px"><span>52 - 100</span></div>
<div class="item col1" style="height:229px"><span>53 - 229</span></div>
<div class="item col1" style="height:126px"><span>55 - 126</span></div>
<div class="item col1" style="height:245px"><span>56 - 245</span></div>
<div class="item col1" style="height:242px"><span>57 - 242</span></div>
<div class="item col1" style="height:146px"><span>58 - 146</span></div>
<div class="item col1" style="height:63px"><span>59 - 63</span></div>
<div class="item col1" style="height:118px"><span>60 - 118</span></div>
<div class="item col1" style="height:249px"><span>61 - 249</span></div>
<div class="item col1" style="height:136px"><span>62 - 136</span></div>
<div class="item col1" style="height:114px"><span>63 - 114</span></div>
<div class="item col1" style="height:152px"><span>64 - 152</span></div>
<div class="item col1" style="height:52px"><span>65 - 52</span></div>
<div class="item col1" style="height:95px"><span>66 - 95</span></div>
</div>
CSS:
.item {
width: 220px;
margin: 10px;
float: left;
background:white;
}
结果:
预期:
当它看起来像this div的interms水平和垂直相互对立时。
问题:
我做错了什么?
答案 0 :(得分:2)
添加更多元素后,您需要使用.masonry('reload')
函数。例如,可以工作:
success: function( widget_shell )
{
if( widget_shell.d[0] ) {
$( "#container" ).empty();
var i = 0;
// creating the div elements
for ( i = 0; i <= widget_shell.d.length - 1; i++ ) {
var j = Math.floor(Math.random() * 200) + 50
var $widget = $( "<div class='item col1' style='height:" + j + "px'></div>" ).appendTo( $( "#container" ) );
$( "<span>" + widget_shell.d[i].widget_id + " - " + j + "</span>" ).appendTo( $widget );
}
// reload jquery masonry plugin
$('#container').masonry('reload')
}
}
否则,请检查jsFiddle:http://jsfiddle.net/RNA8R/1/
答案 1 :(得分:0)
我怀疑您的jquery.masonry.min.js
未正确加载。检查您的firebug或chrome开发人员工具上的网络选项卡。
这是我的index.html
文件中的确切代码(不是我的dom上呈现的内容;我的html文件中的实际代码): -
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="./masonry-master/jquery.masonry.min.js"></script>
<script>
$(function(){
$('#container').masonry({
// options
itemSelector : '.item'
});
});
</script>
<style>
body {
background: #999;
}
.item {
width: 220px;
margin: 10px;
float: left;
background:white;
}
</style>
</head>
<body>
<div id="container">
<div class="item" style="height:154px"><span>39 - 154</span></div>
<div class="item" style="height:100px"><span>52 - 100</span></div>
<div class="item" style="height:229px"><span>53 - 229</span></div>
<div class="item" style="height:126px"><span>55 - 126</span></div>
<div class="item" style="height:245px"><span>56 - 245</span></div>
<div class="item" style="height:242px"><span>57 - 242</span></div>
<div class="item" style="height:146px"><span>58 - 146</span></div>
<div class="item" style="height:63px"><span>59 - 63</span></div>
<div class="item" style="height:118px"><span>60 - 118</span></div>
<div class="item" style="height:249px"><span>61 - 249</span></div>
<div class="item" style="height:136px"><span>62 - 136</span></div>
<div class="item" style="height:114px"><span>63 - 114</span></div>
<div class="item" style="height:152px"><span>64 - 152</span></div>
<div class="item" style="height:52px"><span>65 - 52</span></div>
<div class="item" style="height:95px"><span>66 - 95</span></div>
</div>
</body>
</html>
目录结构: -
我的index.html
在同一目录级别的jquery.masonry.min.js
目录中加载masonry-master