[英]Infinite Ajax Scroll: Hide parent container of load more button if last
I'm using Infinite Ajax Scroll for pagination on my blog.我在我的博客上使用Infinite Ajax Scroll进行分页。 When there are no more posts left to load, I want to hide the trigger
(the buttons) parent container (currently, it just has opacity: 0
, but this leaves unwanted whitespace).当没有更多帖子要加载时,我想隐藏trigger
(按钮)父容器(目前,它只有opacity: 0
,但这会留下不需要的空白)。
In the console, I can see that IAS
leaves a message saying "No more pages left to load", so I know there is a method in place to check if more posts exist.在控制台中,我可以看到IAS
留下一条消息说“没有更多的页面要加载”,所以我知道有一种方法可以检查是否存在更多帖子。 But, having applied two methods, I cannot get it to work.但是,应用了两种方法后,我无法让它发挥作用。
Method 1: Using last:function()
Method 2: Checking if the trigger
has opacity: 0
, then hiding parent container if so方法一:使用last:function()
方法二:检查trigger
是否有opacity: 0
,如果有则隐藏父容器
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@webcreate/infinite-ajax-scroll@3.0.0-beta.6/dist/infinite-ajax-scroll.min.js"></script> <div class="container pagination-hide"> <div class="row"> <div class="col-12"> <div class="pagination"> <a class="loadmore">Load more</a> </div> </div> </div> </div> <script> $(function() { var ias = new InfiniteAjaxScroll('.insertPosts', { item: '.insertCard', pagination: '.blog-pagination', next: '.next-posts-link', trigger: '.loadmore', loadOnScroll: false, last:function(){ $(".pagination-hide").addClass("d-none"); } }); // $('.loadmore').click(function(){ // if ( $(this).css('opacity') == '0' ) { // console.log("true"); // $(this).parent(".pagination-hide").addClass("d-none"); // } // }); }); </script>
Have also tried the following, as per IAS docs , however, this just adds the d-none
class to pagination-hide
on page load, rather than adding the class (as it should) if there are no more posts to load.根据IAS 文档,还尝试了以下操作,但是,这只是将d-none
class 添加到页面加载时的pagination-hide
中,而不是在没有更多帖子要加载的情况下添加 class (应该如此)。
trigger: { element: '.loadmore', // this function is called when the button has to be hidden hide: function(element) { $(element).closest(".pagination-hide").addClass("d-none"); element.style.opacity = '0'; // default behaviour } },
Sorry, but you went on a bad road.对不起,你走错了路。 You shoud show button <a class="loadmore">Load more</a>
if you have some other conten unstead of hiding unnecessary.如果您有其他内容而不是隐藏不必要的内容,则应该显示按钮<a class="loadmore">Load more</a>
。 It's wrong in root.根是错误的。 You are calling unnecessary operations in the DOM render.您在 DOM 渲染中调用了不必要的操作。 You better rewrite your code.你最好重写你的代码。
Seems like you would also need to add a show() method that un-does the changes in hide(), in order to get the correct behavior:似乎您还需要添加一个 show() 方法来取消 hide() 中的更改,以获得正确的行为:
trigger: { element: '.loadmore', // this function is called when the button has to be hidden hide: function(element) { $(element).closest(".pagination-hide").addClass("d-none"); element.style.opacity = '0'; // default behaviour }, show: function(element) { $(element).closest(".pagination-hide").removeClass("d-none"); element.style.opacity = '1'; } },
AFAICT you were on the right track with your first attempt - the last
event seems like exaclty the right way to do what you need. AFAICT,您第一次尝试就走在了正确的轨道上-last
一次似乎是做您需要做的事情的正确方法。 But the docs also show that you need to bind event handlers with .on()
, not pass them as options, which is what you tried.但是文档还表明您需要使用.on()
绑定事件处理程序,而不是将它们作为选项传递,这是您尝试过的。
For example, something like this:例如,像这样:
var ias = new InfiniteAjaxScroll('.insertPosts', {
// ... your options
// last:function(){} // <-- won't work, not the way to bind event handlers
});
// What we want to happen when we've hit the last page
function noMorePages() {
$(".pagination-hide").addClass("d-none");
}
// Handle the "last" event with our function
ias.on('last', noMorePages);
There is a similar example in the docs . 文档中有一个类似的例子。
You simply need to provide show
and hide
functions for the trigger ref .您只需要为触发器ref提供show
和hide
功能。 The library calls the functions automatically on last event.库在最后一个事件上自动调用这些函数。
Demo website https://zyf5k.csb.app/演示网站https://zyf5k.csb.app/
Codesandbox https://codesandbox.io/s/zen-volhard-zyf5k?file=/index.js Codesandbox https://codesandbox.io/s/zen-volhard-zyf5k?file=/index.js
Code for ready reference:代码供参考:
window.ias = new InfiniteAjaxScroll(".container", {
item: ".item",
next: nextHandler,
trigger: {
element: ".loadmore",
show: function (element) {
element.parentElement.style.display = "block";
},
hide: function (element) {
//hide desired element`
element.parentElement.style.display = "none";
}
}
});
Note: Didn't realize @Kevin had already answered this.注意:没有意识到@Kevin 已经回答了这个问题。 Posting the answer for extra info.发布答案以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.