weixin_33709364 2016-01-15 18:55 采纳率: 0%
浏览 34

砌体和重叠div

I've been racking my brain trying to figure this out. I'm using an AJAX call to populate a div with content (divs containing locally stored images and text). The ajax call executes successfully, but the divs on the second row partially overlap the ones on the first row.

Here's the script I've been working with:

var $allF = $('#allItems');

$(document).ready(function () {

    $allF.imagesLoaded(function () {
        $allF.masonry({
            itemSelector: '.box',
            isAnimated: true,
            animationOptions: {
                duration: 500,
                easing: 'linear',
                queue: false
            },
            isFitWidth: true,
            columnWidth: 10
        });
    });

    loadAllItems();

});

function loadAllItems() {
    $.ajax({
        url: '/Ajax/LoadAllItems',
        type: 'POST',
        dataType: 'json',
        data: { skip: (pageIndex * pageSize), take: pageSize },
        success: function (posts) {
            var items = '';
            $.each(posts, function(p, post) {
                items += 'html removed to keep post short';
            });

            var $itemBlock = $(items);
            $allF.append($itemBlock);
            $allF.masonry('appended', $itemBlock);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(xhr.responseText);
            console.log(thrownError);
        }
    });
}

All of the content items have the same width of 130px but the height of each item can vary by as much as 20 to 30 px. Is there something that I've missed? I've tried the Q&A on the masonry site but to no avail.

Any help would be greatly appreciated.

Thanks!

  • 写回答

1条回答 默认 最新

  • 旧行李 2016-01-15 19:08
    关注

    Write your Ajax function like below:

    function loadAllItems() {
        $.ajax({
            url: '/Ajax/LoadAllItems',
            type: 'POST',
            dataType: 'json',
            data: { skip: (pageIndex * pageSize), take: pageSize },
            success: function (posts) {
                var items = '';
                $.each(posts, function(p, post) {
                    items += 'html removed to keep post short';
                });
    
                var $itemBlock = $(items);
                $allF.append($itemBlock);
                $allF.imagesLoaded(function () {
                    $allF.masonry('appended', $itemBlock);
                });
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(xhr.responseText);
                console.log(thrownError);
            }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作