簡體   English   中英

無盡的滾動導軌

[英]Endless scrolling in rails

我參考了http://railscasts.com/episodes/114-endless-page?autoplay=true,以在項目中使用無限滾動。 我想他在視頻中提到的所有內容都是舊的,因此我嘗試將其修改為Rails4。JavaScript文件是

                     var currentPage = 1;

                        function checkScroll() {
                             if (nearBottomOfPage()) {
                                  currentPage++;
                                  new Ajax.Request('/shirts/first?page=' + currentPage, {asynchronous:true, evalScripts:true, method:'get'});
                                  } else {
                                  setTimeout("checkScroll()", 250);
                             }
                          }

                        function nearBottomOfPage() {
                           return scrollDistanceFromBottom() < 150;
                        }

                        function scrollDistanceFromBottom(argument) {
                           return pageHeight() - (window.pageYOffset + self.innerHeight);
                        }

                        function pageHeight() {
                           return Math.max(document.body.scrollHeight, document.body.offsetHeight);
                        }

                        document.observe('dom:loaded', checkScroll);

而且我沒有使用rjs文件,而是在.js.erb中編寫了以下代碼

                    if @first.total_pages > @first.current_page
                         page.call 'checkScroll'
                    else
                         page['#loading'].hide
                    end

我做的事正確嗎? 當我用螢火蟲檢查瀏覽器時,它說:

                      TypeError: document.observe is not a function
                      document.observe('dom:loaded', checkScroll);

我的代碼有什么問題? 有人可以幫我這個忙嗎?

我認為該railscast示例使用prototype.js而不是jQuery,並且我猜您在Rails 4應用程序中使用了jQuery,因為我認為document.observe原型中方法 ,而不是jQuery中的方法。

您可能需要查看修訂后的情節以進行無窮的滾動 ,但是它不是免費的,但是Railscast的Ryan Bates還在github上分享了他的代碼,因此您可以在github上看到修訂后的情節的代碼 ,這是Rails 3.1並使用jQuery,因此可能更容易連接到Rails 4。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM