首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将不确定负载添加到FacetWP寻呼按钮中

将不确定负载添加到FacetWP寻呼按钮中
EN

Stack Overflow用户
提问于 2021-12-23 13:13:34
回答 1查看 545关注 0票数 -2

我有一个wordpress/woocommerce商店,安装了FacetWP插件,用于分类和过滤我的商店中的产品。这个插件提供了一个“加载更多”的功能,以惰性地在产品类别屏幕上加载更多的产品。

在这里,我想将不确定加载功能添加到FacetWPs "load more“按钮中。因此,一旦用户从页面底部滚动到某个偏移量,就应该自动单击"load“按钮。

不幸的是,他们没有为这个提供一个现成的功能。

因此,我的问题是:如何实现无限加载功能到方面-wps“加载更多”按钮?

我试图从这里将代码https://gist.github.com/hirejordansmith/cc2363a860a7ed8320307b46f1196407#gistcomment-3921811添加到我的function.php文件中,但并不是所有浏览器都能工作:

代码语言:javascript
复制
/* globals FWP */

    /**
     * JavaScript for FacetWP Infinite Scroll
     */
    ( function( $ ) {
        'use-strict';

        // TODO: update for your requirements
        var bottomDistance = 1500; // the distance in px to the bottom of the page, when facetwp should trigger
        var throttleFetchDelay = 5000; // the timeout for checking if to fetch new products again after a fetch if triggered

        var isFetching = false; // if facetwp is already fetching new products
        var isChecking = false; // if a timeout for checking if to fetch new products is already set

        var throttleTimer = null; // the timer for checking if to fetch new products
        var throttleDelay = 100; // the timeout for checking if to fetch new products

        $( function() {
            var $win = $( window );
            var $doc = $( document );

            function ScrollHandler() {
                if (isChecking) {
                    return;
                }
                clearTimeout( throttleTimer );
                console.log( 'start timer');
                isChecking = true;
                throttleTimer = setTimeout( function() {
                    isFetching = false;
                    throttleDelay = 100;

                    if ( ($( window ).scrollTop()
                        + $( window ).height()
                        > $( document ).height() - bottomDistance)
                        && !isFetching ) {
                        console.log( 'aaaaaaaaaaaaaa' );
                    } else {
                        console.log( 'bbbbbbbbbbbbb' );
                        isChecking = false;
                        return;
                    }
                    isFetching = true;
                    throttleDelay = throttleFetchDelay;

                    if ( FWP.settings.pager.page < FWP.settings.pager.total_pages ) {
                        FWP.paged = parseInt( FWP.settings.pager.page ) + 1;
                        FWP.is_load_more = true;
                        //alert("start");
                        if ( jQuery( '.mycurellsloadder' )
                            .length == 0 ) {

                            jQuery( ".woocommerce-pagination" )
                                .append( "<div class='mycurellsloadder'></div>" );
                        }
                        FWP.soft_refresh = false;
                        FWP.refresh();
                        isChecking = false;
                    }
                }, throttleDelay );
            }

            wp.hooks.addFilter( 'facetwp/template_html', function( resp, params ) {
                if ( FWP.is_load_more ) {
                    //   alert("end");
                    jQuery( ".mycurellsloadder" )
                        .remove();
                    FWP.is_load_more = false;
                    $( '.facetwp-template' )
                        .append( params.html );
                    return true;
                }

                return resp;
            } );

            $doc.on( 'facetwp-loaded', function() {
                if ( !FWP.loaded ) {
                    console.log( 'your message' );
                    $win.off( 'scroll', ScrollHandler )
                        .on( 'scroll', ScrollHandler );
                }
            } );
        } );
    } )( jQuery );
EN

回答 1

Stack Overflow用户

发布于 2021-12-23 13:13:34

可以通过将此代码放置在functions.php文件中来实现无限加载功能。通过更改"intBottomMargin“变量,您可以根据自己的情况来决定,在哪种情况下应该触发”负载更多“:

代码语言:javascript
复制
add_action('wp_footer', 'add_faceet_wp_infinite_scroll');
function add_faceet_wp_infinite_scroll() {
    if (is_admin() || is_checkout()) {
        return;
    }
    ?>
        <script>

            jQuery(document).ready(function($){
                var intBottomMargin = 1500; //Pixels from bottom when script should trigger
                setInterval(() => {
                    if (($(window).scrollTop() >= $(document).height() - $(window).height() - intBottomMargin)
                        && (!$(".facetwp-load-more").hasClass("loading"))
                        && (!$(".facetwp-load-more").hasClass("facetwp-hidden"))
                    ) {
                        $(".facetwp-load-more").addClass('loading');
                        $(".facetwp-load-more").click(); //trigger click
                        // the class is automatically removed, as the button is recreated, as soon as it loaded the products
                    }
                }, 1000);
            });
        </script>
    <?php
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70462611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档