jQuery Masonry 和 Ajax-fetching 追加项目导致图像重叠

2023-10-20前端开发问题
4

本文介绍了jQuery Masonry 和 Ajax-fetching 追加项目导致图像重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这里使用 Masonry 和 Ajax 在 Wordpress 中附加项目的另一个图像重叠问题.第一次添加更多项目时,图像会重叠.但是,当页面重新加载时,图像不再重叠.经过一些研究,我意识到这与计算图像的高度有关.

Another image overlap issue here using Masonry and Ajax to append items in Wordpress. The first time more items are appended, the images overlap. However, when the page is reloaded, the images no longer overlap. I realize after doing some research this has to do with calculating the height of the images.

在 Masonry 网站的帮助页面中,建议使用 getimagesize 函数来指定图像的宽度和高度.

From the help page on the Masonry website, it is suggested to use the getimagesize function in order to specify the width and height of the images.

但是,这就是我卡住的地方.由于我对 PHP 的了解有限,我不知道如何使用这个函数或将它放在我的代码中的什么位置,所以我在这里寻找一点方向.谁能帮我弄清楚如何将 getimagesize 函数集成到我的代码中?

However, this is where I am stuck. Because of my limited knowledge of PHP, I have no idea how to utilize this function or where to place it in my code, so I'm looking for a little direction here. Can anyone help me figure out how to integrate the getimagesize function into my code?

这是砌体代码:

$(document).ready(function(){

    var $container = $('#loops_wrapper');

    $container.imagesLoaded( function(){
      $container.masonry({
        itemSelector : '.post_box',
        columnWidth: 302

        });
      });
    });

这是 ajax 获取代码:

This is the ajax fetching code:

$('.load_more_cont a').live('click', function(e) {
    e.preventDefault();
    $(this).addClass('loading').text('Loading...');
    $.ajax({
        type: "GET",
        url: $(this).attr('href') + '#loops_wrapper',
        dataType: "html",
        success: function(out) {
            result = $(out).find('.post_box');
            nextlink = $(out).find('.load_more_cont a').attr('href');

            $('#loops_wrapper').append(result).masonry('appended', result);
            $('.load_more_cont a').removeClass('loading').text('Load more posts');

            if (nextlink != undefined) {
                $('.load_more_cont a').attr('href', nextlink);
            } else {
                $('.load_more_cont').remove();
            }
        }
    });
});

推荐答案

您可以尝试在此处实现 David DeSandro 的计时器方法来附加图像...

You could try to implement David DeSandro's timer approach here for appending images...

正如入门所推荐的,处理图像的最佳解决方案是在 img 标签中定义尺寸属性,尤其是在使用无限滚动时.这是下面示例中采用的解决方案.

"As recommended in the primer, the best solution to handle images is to have the size attributes defined in the img tag, especially when using Infinite Scroll. This is the solution employed in the example below.

当然,这在某些 CMS 中不是一个可行的选择,尤其是 Tumblr.在这种情况下,需要在新附加的图像完全加载后调用 Masonry.这是通过延迟 Masonry 回调来完成的."

Of course, this is not a viable option in some CMSs, most notably Tumblr. In this situation, Masonry needs to be called after the newly-appended images are fully loaded. This is done by delaying the Masonry callback."

function( newElements ) {
    setTimeout(function() {
    $wall.masonry({ appendedContent: $(newElements) });
    }, 1000);
}

编辑:如果您无法实现无限滚动附加项目的常见延迟想法,您可以尝试在附加新项目后重新加载,而不是

EDIT: If you can't implement the common delay idea for appending items with infinite scroll, you could try reloading after appending new items so instead of

$('#loops_wrapper').append(result).masonry('appended', result);

这样做

$("#loops_wrapper").append(result).masonry('reload');

这篇关于jQuery Masonry 和 Ajax-fetching 追加项目导致图像重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6

无限滚动和 will_paginate 多次附加项目的“下一页"
Infinite scroll and will_paginate appending the #39;next page#39; of items multiple times(无限滚动和 will_paginate 多次附加项目的“下一页)...
2024-04-20 前端开发问题
8