【问题标题】:Jquery custom function not positioning divs on ajax loaded contentJquery自定义函数不在ajax加载的内容上定位div
【发布时间】:2012-10-16 18:45:26
【问题描述】:

我编写了一个自定义函数,将绝对 div 定位在容器内,计算顶部位置和高度:

$.fn.gridB = function() {
    var o = $(this); //EDITED from var o = $(this[0]);
    o.each(function() {
        var a1top = o.find('.article1').position().top;
        var a1height = o.find('.article1').height();
        var a0top = o.find('.article0').position().top;
        var a0height = o.find('.article0').height();
        var a2top = o.find('.article2').position().top;
        var a2height = o.find('.article2').height();
        var a3height = o.find('.article3').height();
        var a4height = o.find('.article4').height();
        var a5height = o.find('.article5').height();
        if (a0height > a1height + a1top) {
            $('.article3', o).css('top', a0top + a0height + 20);
        } else if (a0height < a1height + a1top) {
            $('.article3', o).css('top', a1top + a1height + 20);
        }
        $('.article4', o).css('top', a2top + a2height + 20);
        $('.article5', o).css('top', a2top + a2height + 20);
        $('.article6', o).css('top', a2top + a2height + a5height + 40);
        $('.article7', o).css('top', a2top + a2height + a5height + 40);
        var lastChildPos = o.find('div:last-child').position().top;
        var lastChildHeight = o.find('div:last-child').height();
        o.height(lastChildPos + lastChildHeight + 20);
    });
};​

容器有一个类“.fours”,并且脚本在窗口加载时完美运行。但是当在 ajax 无限滚动加载后调用它时,它无法将绝对 div 正确定位在下一个“.fours”容器内:

function loadArticle(pageNumber) {
    $('a#inifiniteLoader').fadeIn('fast');
    $.ajax({
        url: "wp-admin/admin-ajax.php",
        type: 'POST',
        data: "action=infinite_scroll&page_no=" + pageNumber + '&loop_file=loop',
        success: function(html) {
            $('a#inifiniteLoader').fadeOut('1000');
            $("#content").append(html); // This will be the div where our content will be loaded
            $('.fours:last').preloader_index();
            $('.fours').gridB(); // EDITED from $('.fours:last').gridB();
            $('.article a').on({
                mouseenter: function() {
                    if (!$('.preloader', $(this).parent('.article')).length > 0) {
                        $(this).stop().css('opacity', '1');
                    }
                },
                mouseleave: function() {
                    if (!$('.preloader', $(this).parent('.article')).length > 0) {
                        $(this).stop().css('opacity', '0');
                        $('.article-info', $(this).parent('.article')).stop().fadeTo(200, 0);
                    }
                }
            });
        }
    });
    return false;
}​

出了点问题,但我已经很接近了。如果我在 ajax 成功后调用函数 gridB() 时放置 .fours:last ,则脚本仅计算 div 的顶部位置,而忽略高度。如果我删除 :last 脚本不会计算顶部位置和高度。

有什么帮助吗?

编辑: 以下是相关 HTML 的示例。另一个带类的 div

 <div id="content">
<div class="fours">
  <div class="article article0">
     <div><img src="url" /></div>
  </div>
  <div class="article article1">
     <div><img src="url" /></div>
  </div>
  <div class="article article2">
     <div><img src="url" /></div>
  </div>
  <div class="article article3">
     <div><img src="url" /></div>
  </div>
  </div>
<!-- Next ".fours" div is loaded here with ajax function -->
</div>

编辑 2:在 gridB 函数中,我更改了行 var o = $(this[0]);到 var o = $(this);并从 $('.fours:last').gridB(); 更改了函数调用到 $('.fours').gridB();.它有效,但现在脚本将所有绝对 div 定位在 ajax 加载的下一组中,并使用第一组文章的值。我认为它更接近解决方案。必须有一种方法来定位由 ajax 加载的容器。 :last 不起作用。

【问题讨论】:

  • 能贴出相关的HTML吗?
  • 嗨,胡安,我发布了 HTML 代码,请检查我的问题的编辑。有什么建议吗?
  • 我觉得还是有很多信息缺失,比如Ajax调用返回的html是什么? preloader_index() 电话在做什么?您还提到这应该与 Ajax 无限循环一起使用,但您似乎为文章 0 到 7 设置了固定的类名。我看不出代码对于 7 之后的文章是如何工作的。
  • 是的,我想它看起来很复杂。我将尝试解释它是否对建议有任何帮助。从 ajax 返回的 html 是另一个具有类 .four 的 div 容器,具有相同数量的 div 和相同的类 (.article0 - .article7),在没有无限滚动调用的情况下进行了测试和工作。 preloader_index() 在文章 div 中包含的图像中逐渐消失,我尝试了没有它的脚本并且没有冲突,div 的位置仍然错误。最后,没有超过 7 条的物品,它们在 '.fours' 容器中以 7 组为一组装入。希望这可以清除一点?
  • 我猜问题是gridB()函数没有针对ajax加载添加的.fours容器?

标签: jquery css ajax callback positioning


【解决方案1】:

关于您上次编辑中的评论,您可以执行以下操作来引用最后添加的&lt;div class="fours" /&gt;:

success: function(html) {
    var $html = $(html);                       // Wrap HTML in jQuery object.
    $('a#inifiniteLoader').fadeOut('1000');
    $("#content").append($html);               // Add created object.

    $html.preloader_index();                   // Use the same object.
    $html.gridB();                             // Call gridB using the same object.

【讨论】:

  • 感谢您的帮助。 JQuery 对象适用于追加和预加载器功能,但不适用于网格之一。它仍然弄乱了加载内容的布局。有更长的路要走,我使用了调整大小插件并将每个“.fours”的网格功能放在#content的调整大小事件中,现在可以正常工作了。我仍然希望网格功能仅适用于加载的内容,而不是重新计算以前的内容,但现在我要把它保留在那里。
猜你喜欢
  • 2012-05-08
  • 1970-01-01
  • 2013-06-11
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多