基于jQuery masonry,我得到以下内容:
jQuery:
$('#container').masonry({
itemSelector : '.item',
columnWidth: 240
});
jQuery ajax:(jquery-masonrysuccess
在页面加载后第一次工作。如果在没有重新加载页面的情况下再次发生,jquery-masonry 将不起作用success
。)
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;
}
结果:
预期的:
当div 在水平和垂直方向上相互对抗时,它应该看起来像这样。
问题:
我究竟做错了什么?