简体   繁体   English

将“单击以加载更多”替换为滚动以加载更多

[英]Replace a “Click to load more” into a Scroll to load more

Currently, my client's website has a "Load more" button, linked to the Shutterstock API to load more photos everytime you click on that button. 当前,我客户的网站上有一个“加载更多”按钮,链接到Shutterstock API,以便每次单击该按钮时可以加载更多照片。

My client asked to change this into "When the user scrolls down, it loads automatically more images". 我的客户要求将其更改为“当用户向下滚动时,它将自动加载更多图像”。

So what I thought, since I'm not an experienced coder, is to add a function linked with window.scroll that would trigger a click on that button once you reach the top of that button, using the following code: 所以我想,因为我不是一个有经验的编码人员,所以要使用以下代码添加一个与window.scroll链接的函数,该函数将在您到达该按钮的顶部时触发该按钮的单击:

    $(window).scroll(function() {
var top_of_element = $("#load_more_images").offset().top;
var bottom_of_element = $("#load_more_images").offset().top + $("#load_more_images").outerHeight();
var bottom_of_screen = $(window).scrollTop() + window.innerHeight;
var top_of_screen = $(window).scrollTop();

if((bottom_of_screen > top_of_element) && (top_of_screen < bottom_of_element)){
    $("#load_more_images").trigger("click");
}
else {
    // The element is not visible, do something else
}

}); });

The issue, is that once the button is in view, it triggers the click multiple time, and it loads the next 6 images multiple times back to back. 问题在于,一旦按钮出现在视图中,它将触发多次单击,并且会连续地多次加载接下来的6张图像。 I guess it's clicking multiple time since the button stays in view, not sure how to handle this. 我猜这是多次单击,因为该按钮一直处于可见状态,不确定如何处理。

The code for the "load more" function that works linked to that "load_more_images" button is in a "func.php" page (it's for a Wordpress site, and it's in a plugin) : 链接到“ load_more_images”按钮的“更多加载”功能的代码在“ func.php”页面中(用于Wordpress网站,并且在插件中):

jQuery("#load_more_images").click(function() {
            jQuery(this).hide();
            jQuery(".load_more_wrapper .loader").show();
            var ajax_url = "'.admin_url('admin-ajax.php').'";
            jQuery.post(
                        ajax_url,
                        {
                            "action": "pd_load_more_img",
                            "data": {
                                "type": search_type,
                                "page":page+1,
                                "image_type": "'.(isset($_GET["image_type"]) ? $_GET["image_type"] : "all").'"';

                            if (isset($_GET['category'])){
                                $js.=',
                                "category":'.$_GET['category'];
                            }
                            if (isset($_GET['search'])){
                                $js.=',
                                "search":"'.$_GET['search'].'"';
                            }
                $js.= '}
                        },
                        function(data){
                            page++;
                            jQuery("#images_container").append(data);
                            jQuery(".load_more_wrapper .loader").hide();
                            jQuery("#load_more_images").show();
                            jQuery(".category-link").dotdotdot();
                        }
                    );
        });';

Any idea how I could make this work? 知道我该怎么做吗? All I need is to activate that existant function that is currently bound to a click event, but on scroll, when I reach that button, or a certain element in the page at the bottom. 我需要的是激活当前绑定到click事件的现有功能,但是在滚动时,当我到达该按钮或底部页面中的某个元素时,就可以滚动该功能。

Thanks a lot 非常感谢

您可以使用该功能代替点击功能:-

$(window).scroll( function(e){  if($(window).scrollTop()>= jQuery('#load_more_images').position().top){ doYourFunctionHere(); } }

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

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