3

Masonry 不适用于我的动态内容,我不知道为什么。我不认为这是我这边的错误,至少我已经看了几个小时的代码,但我找不到任何不起作用的东西。

//reads listbox.php and cycles through the array calling createbox
function listboxs() {
    $.ajax({
        url: '_php/listbox.php',
        success: function (output) {

            var jsonArray = $.parseJSON(output);

            $.each(jsonArray, function (i, box) {
                createbox(box.id, box.name, box.link, box.description, box.tags);
            });
        }
    });
}

//create the code for 1 box
function createbox(id, name, link, description, tags) {

    var boxHtml = "",
        tagsHtml = "",
        descriptionHtml = "";

    boxHtml = '' + '<div class="box" id="' + id + '">' + '<div class="boxinfo">' + '<label class="boxname"><a href="' + link + '" class="boxlink" target="_blank">' + name + '</a></label>';

    $.each(tags, function (i, tag) {
        tagsHtml += '<label class="boxtag">' + ((!tag.name) ? tags[i] : tag.name) + '</label>';
    });

    //if(description.trim().length > 0){
    descriptionHtml = '<textarea class="boxdescription" readonly rows="1">' + description + '</textarea>';
    //}

    boxHtml += tagsHtml + '</div>' + descriptionHtml + '</div>';

    $content.html($content.html() + boxHtml);
}

以下是简化的 HTML:

<!DOCTYPE html>
<html>

    <head>
        <link rel="stylesheet" type="text/css" href="_css/index.css" />
        <link href='http://fonts.googleapis.com/css?family=Marck+Script' rel='stylesheet'
        type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=Rosario' rel='stylesheet'
        type='text/css'>
        <script src="_resources/jquery-2.0.3.min.js" type="text/javascript" language="javascript"></script>
        <script src="_resources/masonry.pkgd.min.js"></script>
        <script type="text/javascript" language="javascript">
            $('#content').masonry();
        </script>
    </head>

    <body>
        <div id="content" class="js-masonry"></div>
    </body>

</html>

我知道我不需要内联javascript调用内容的砌体,但这是我的众多测试之一......

下面是 CSS 的一部分:

#content{
padding: 15px;
min-height: 400px;
}

/*
################################
box
*/

.box{
border: 1px solid black;
float: left;
padding: 5px;
background: #F0F0F0;
margin-left: 5px;
margin-bottom: 5px;
}

.boxinfo{
border-bottom: 1px solid black;
}

.boxname{
font-weight: bold;
}

.boxdescription{
border: none;
outline: none;
background: white;
overflow: hidden;
}

.boxtag{
margin-left: 5px;
}

#boxdecoy{
height: 45px;
}

.boxname, .boxtag, .boxdescription{
font-family: 'Rosario', sans-serif;
font-size: 12px;
}

.boxlink{
text-decoration: none;
color: black;
}

.boxlink:hover{
text-decoration: underline;
}

我真的对这一切感到疯狂,因为我测试了在内容中手动创建框(这意味着在 html 中写入),如果我这样做,砌体效果很好。如果我通过您在那里看到的函数创建它们,它不起作用......我在声明所有变量后在 javascript 文件的开头调用列表框......

希望我很清楚,你可以帮助我。

4

2 回答 2

5

你应该使用appended方法。来自文档

添加和布置新附加的项目元素。

看看这个jsfiddle

尝试将您的代码更改为

boxHtml += tagsHtml +
        '</div>' +
    descriptionHtml +
'</div>';

var $boxHtml = $(boxHtml);

$content.append($boxHtml).masonry('appended', $boxHtml);
于 2013-08-28T08:18:53.640 回答
1

加上 Grin 的回答:

您还应该将 data-masonry-options='{ "columnWidth": 200, "itemSelector": ".item" }' 应用于您的#container。

<div id="content" class="js-masonry" data-masonry-options='{ "columnWidth": 200, "itemSelector": ".item" }'></div>

像这样。它可能有助于您的评论回复。我没有代表作为评论来回答。

于 2013-08-28T10:03:48.193 回答