2012-06-27 31 views
5

在我的主頁上,我有一個使用無限滾動的帖子循環。用戶可以使用ajax(如ajax搜索)等將其替換爲其他循環。我的問題是無限滾動僅在第一次使用它時起作用,所以如果它被主循環觸發,那麼當新循環取代主循環時它將不再工作。每當新的循環替換舊的時候,下面的函數被重新加載。那麼,如何在每次調用新循環時進行無限循環重置和工作?如何使用新的ajax內容製作無限滾動重置

var href = 'first'; 
$(document).ready(function() { 
    $('#boxes').infinitescroll({ 
     navSelector: '.infinitescroll', 
     nextSelector: '.infinitescroll a', 
     itemSelector: '#boxes .box', 
     loadingImg: '<?php echo get_bloginfo('stylesheet_directory') ?>/images/loading.gif', 
     loadingText: 'Loading...', 
     donetext: 'No more pages to load.', 
     debug: false 
    }, function(arrayOfNewElems) { 
     $('#boxes').masonry('appended', $(arrayOfNewElems)); 
     if(href != $('.infinitescroll a').attr('href')) { 
      href = $('.infinitescroll a').attr('href'); 
     } 
    }); 
}); 

我使用一個WordPress站點的Pual Irish infinite scroll plugin的最新版本2.0b2.120519。

+1

是否有可能看到你的代碼的一個活生生的例子打破? – rlemon

+0

這是一個測試網站,但我在js聊天中留下了一個鏈接。 –

回答

3

我並不是100%確定您使用的那個插件的版本,但是我檢查了最新的發行版,並且需要執行兩種方法才能完成此工作。

首先,在你的ajax函數中,你需要destroy the session on success

$.ajax({ 
    url: 'path/to/something.ext', 
    success: function(data){ 
    //call the method to destroy the current infinitescroll session. 
    $('#boxes').infinitescroll('destroy'); 

    //insert the new data into the container from the_loop() call 
    $('#boxes').html(data); 

    //reinstantiate the container 
    $('#boxes').infinitescroll({      
     state: {            
     isDestroyed: false, 
     isDone: false       
     } 
    }); 

    //call the methods you need on the container again. 
    $('#boxes').infinitescroll({ 
     navSelector: '.infinitescroll', 
     nextSelector: '.infinitescroll a', 
     itemSelector: '#boxes .box', 
     loadingImg: '<?php echo get_bloginfo('stylesheet_directory') ?>/images/loading.gif', 
     loadingText: 'Loading...', 
     donetext: 'No more pages to load.', 
     debug: false 
    }, function(arrayOfNewElems) { 
     $('#boxes').masonry('appended', $(arrayOfNewElems)); 
     if(href != $('.infinitescroll a').attr('href')) { 
     href = $('.infinitescroll a').attr('href'); 
     } 
    }); 
    } 
}); 

你也可以把它的功能,綁定到它,並解除綁定/重新綁定,而不是重複的代碼了很多,但我已經列出的代碼應該是一個拷貝+粘貼的解決方案。

+0

我曾看過這個解決方案,但無法使其工作。這條路徑假設是什麼,你是不是應該使用'文件就緒'來做這件事? –

+0

嗨@Ohgodwhy我正在嘗試使用您的腳本,但在調試時,我得到了'未捕獲的TypeError:無法調用方法'appendTo'null'。我敢肯定,我打電話的div在那裏。你知道這個代碼是否適用於最新版本?謝謝! – Jaypee

+0

@Jaypee你好。當您收到關於jQuery無法調用「null」方法的錯誤時,這意味着您的選擇器失敗。無論可能是什麼能力。你能簡單地告訴我你的選擇器是否是你的appendTo()? – Ohgodwhy

1

我認爲可以做到這一點。 在我的頁面中,我點擊不同的元素來加載不同的頁面,這些頁面也是infinitescroll。所以我修改以下來源:

 // creation 
     case 'object': 

      this.each(function() { 

       var instance = $.data(this, 'infinitescroll'); 

      /* if (instance) { 

        // update options of current instance 
        instance.update(options); 

       } else { 
      */ 
       $(this).removeData("infinitescroll"); 

       // initialize new instance 
       $.data(this, 'infinitescroll', new $.infinitescroll(options, callback, this)); 

     // } 

      }); 

每次我刪除日期並創建一個新的infinitescroll。所以每一次和每一件事都是好的。

0

呼叫

$('#boxes').infinitescroll({      
       state: {            
        isDuringAjax: false, 
        isInvalidPage: false, 
      isDestroyed: false, 
      isDone: false, // For when it goes all the way through the archive. 
      isPaused: false, 
      currPage: 1 

       } 
      }); 
then 
$('#boxes').infinitescroll({ 
     navSelector: '.infinitescroll', 
     nextSelector: '.infinitescroll a', 
     itemSelector: '#boxes .box', 
     loadingImg: '<?php echo get_bloginfo('stylesheet_directory') ?>/images/loading.gif', 
     loadingText: 'Loading...', 
     donetext: 'No more pages to load.', 
     debug: false 
    }, function(arrayOfNewElems) { 
     $('#boxes').masonry('appended', $(arrayOfNewElems)); 
     if(href != $('.infinitescroll a').attr('href')) { 
     href = $('.infinitescroll a').attr('href'); 
     } 
    }); 

希望工程

+0

如果你能解釋_爲什麼它應該起作用,你的答案會好得多。 – Ben