简体   繁体   English

方形空间上的无限滚动获取类别过滤器

[英]infinite scroll on squarespace get category filter

I am using this code to infinite load a page on squarespace. 我正在使用此代码无限加载方形空间上的页面。 My problem is the reloading doesn't capture the filtering that I have set up in my url. 我的问题是重新加载不捕获我在我的网址中设置的过滤。 It cannot seem to 'see' the variables or even the url or categoryFilter in my collection. 它似乎无法“看到”我的集合中的变量甚至url或categoryFilter。 I've tried to use a .var directive but the lazy loaded items cannot see the scope of things defined before it. 我曾尝试使用.var指令,但延迟加载的项目无法查看之前定义的事物的范围。 I'm running out of ideas here please help! 我这里的想法用完了,请帮忙!

edit: I've since found the answer but gained another question. 编辑:我已经找到答案,但又得到了另一个问题。

I was able to use window.location.href instead of window.location.pathname to eventually get the parameters that way. 我能够使用window.location.href而不是window.location.pathname来最终得到参数。 Except this doesn't work in IE11 so now I have to search for this. 除了这在IE11中不起作用,所以现在我必须搜索它。

 <script>

 function infiniteScroll(parent, post) {

     // Set some variables. We'll use all these later.
     var postIndex = 1,
         execute = true,
         stuffBottom = Y.one(parent).get('clientHeight') + Y.one(parent).getY(),
         urlQuery = window.location.pathname,
         postNumber = Static.SQUARESPACE_CONTEXT.collection.itemCount,
         presentNumber = Y.all(post).size();

     Y.on('scroll', function() {

         if (presentNumber >= postNumber && execute === true) {
             Y.one(parent).append('<h1>There are no more posts.</h1>')
             execute = false;
         } else {

             // A few more variables.
             var spaceHeight = document.documentElement.clientHeight + window.scrollY,
             next = false;

             /*
                 This if statement measures if the distance from
                 the top of the page to the bottom of the content
                 is less than the scrollY position. If it is,
                 it's sets next to true.
             */
             if (stuffBottom < spaceHeight && execute === true) {
                 next = true;
             }

             if (next === true) {

                 /*
                     Immediately set execute back to false.
                     This prevents the scroll listener from
                     firing too often.
                 */
                 execute = false;

                 // Increment the post index.
                 postIndex++;

                 // Make the Ajax request.
                 Y.io(urlQuery + '?page=' + postIndex, {
                     on: {
                         success: function (x, o) {
                             try {
                                 d = Y.DOM.create(o.responseText);
                             } catch (e) {
                                 console.log("JSON Parse failed!");
                                 return;
                             }

                             // Append the contents of the next page to this page.
                             Y.one(parent).append(Y.Selector.query(parent, d, true).innerHTML);

                             // Reset some variables.
                             stuffBottom = Y.one(parent).get('clientHeight') + Y.one(parent).getY();
                             presentNumber = Y.all(post).size();
                             execute = true;

                         }
                     }
                 });
             }
         }
     });
 }

 // Call the function on domready.
 Y.use('node', function() {
     Y.on('domready', function() {
         infiniteScroll('#content','.lazy-post');
     });
 });


 </script>

I was able to get this script working the way I wanted. 我能够以我想要的方式运行这个脚本。

I thought I could use: 我以为我可以用:

Static.SQUARESPACE_CONTEXT.collection.itemCount

to get {collection.categoryFilter} like with jsont, like this: 像jsont一样得到{collection.categoryFilter},像这样:

Static.SQUARESPACE_CONTEXT.collection.categoryFilter

or this: 或这个:

Static.SQUARESPACE_CONTEXT.categoryFilter

It didn't work so I instead changed 它没有用,所以我改变了

urlQuery = window.location.pathname

to

urlQuery = window.location.href

which gave me the parameters I needed. 这给了我需要的参数。

The IE11 problem I encountered was this script uses 我遇到的IE11问题是这个脚本使用的

window.scrollY

I changed it to the ie11 compatible 我把它改成了ie11兼容

Window.pageYOffset

and we were good to go! 我们很高兴去!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM