0

基于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 在水平和垂直方向上相互对抗时,它应该看起来像这样。

问题:

我究竟做错了什么?

4

2 回答 2

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/

于 2012-11-21T17:09:00.803 回答
0

我怀疑你jquery.masonry.min.js的加载不正确。检查您的萤火虫或您的 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一个名为right 的目录中masonry-master

于 2012-11-21T16:05:39.990 回答