gpt4 book ai didi

javascript - jQuery 砌体示例对我不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:54 24 4
gpt4 key购买 nike

基于 jQuery masonry ,我有以下内容:

jQuery:

$('#container').masonry({
itemSelector : '.item',
columnWidth: 240
});

jQuery ajax:(jquery-masonry 在页面加载后第一次成功 时工作。如果成功,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;
}

结果:

enter image description here

预期:

什么时候应该看起来像 this div 在水平和垂直方向上相对。

问题:

我做错了什么?

最佳答案

您需要使用 .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/

关于javascript - jQuery 砌体示例对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13496942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com