2017-08-15 1 views
0

Die letzte Frage ist hier: Wrap some divs with Two different columnswarum die wrapAll in infinite-scroll, viele divs haben?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script src="https://unpkg.com/[email protected]/dist/infinite-scroll.pkgd.min.js"></script> 

    <script type="text/javascript"> 
    $('.wrapper').each(function() { 
     $(this).find(".lpost").wrapAll('<div class="left_columns"></div>') 
     $(this).find(".rpost").wrapAll('<div class="right_columns"></div>') 
    }) 
    </script> 

    <div class="wrapper"> 
     <div class="rpost">-115</div> 
     <div class="lpost">-91</div> 
     <div class="lpost">-99</div> 
     <div class="rpost">-181</div> 
     <div class="lpost">-19</div> 
     <div class="rpost">-135</div> 
     <div class="rpost">-85</div> 
     <div class="lpost">-39</div> 
    </div> 

     <script type="text/javascript"> 
     var $container = $('.wrapper').infiniteScroll({ 
      path: '.next-post', 
      append: '.post', 
      hideNav: '.pagination', 
      status: '.page-load-status', 
     }); 
     $container.on('append.infiniteScroll', 
     function(event, response, path, items) { 
      $(function() { 
       $('.wrapper').each(function() { 
        $(this).find(".lpost").wrapAll('<div class="left_columns"></div>') 
        $(this).find(".rpost").wrapAll('<div class="right_columns"></div>') 
       }) 
      }); 
     }); 
     </script> 

Wenn scroll down drei Seiten:

<div class="wrapper"> 
    <div class="left_columns"> 
    <div class="left_columns"> 
     <div class="left_columns"> 
      <div class="lpost">-91</div> 
      <div class="lpost">-99</div> 
      <div class="lpost">-19</div> 
      <div class="lpost">-39</div> 
     </div> 
    </div> 
    </div> 
</div> 

haben zu viele left_columns und right_columns divs , wie dieses Problem beheben?

+0

try '$ (this) .find ("> .lpost") wrapAll (...)' – talkhabi

+0

@Damon. s okay, ich versuche es – metalbug

Antwort

0

Sie sollten überprüfen, ob die .right_columns oder .left_columns existiert oder nicht.

$('.wrapper').each(function() { 
    var lpost = $(this).find(".lpost"), 
     rpost = $(this).find(".rpost"), 
     lcol = $(this).find('.left_columns'); 

    if(lcol.length){ 
     var rcol = $(this).find('.right_columns'); 
     lpost.appendTo(lcol); 
     rpost.appendTo(rcol); 
    }else{ 
     lpost.wrapAll('<div class="left_columns"></div>'); 
     rpost.wrapAll('<div class="right_columns"></div>'); 
    } 
}) 
+0

danke Mann, gute Arbeit. – metalbug

Verwandte Themen