微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress Customizer:选择性刷新完成后触发JS

如何解决WordPress Customizer:选择性刷新完成后触发JS

当特定设置更改时,我尝试在“定制程序预览”中执行一些JS。 就我而言,我需要在选择性刷新完成后获取标头的高度。

我通过以下方式尝试过:

*--------------------------------------------------------------
# Selective Refresh
--------------------------------------------------------------*/
/**
 * Custom JavaScript functions for the customizer preview after selective refresh
 *
 * @version 1.0.0
 */
;(function () {
    document.addEventListener('DOMContentLoaded',function () {
        /**
         * Selectice refresh check from https://github.com/xwp/wp-jetpack/blob/feature/selective-refresh-widget-support/modules/widgets/contact-info/contact-info-map.js#L35
         * @type {*}
         */
        
        hasSelectiveRefresh = (
            'undefined' !== typeof wp &&
            wp.customize &&
            wp.customize.selectiveRefresh &&
            wp.customize.widgetsPreview &&
            wp.customize.widgetsPreview.WidgetPartial
        );
        if (hasSelectiveRefresh) {
            wp.customize.selectiveRefresh.bind('partial-content-rendered',function (placement) {


                // Header
                wp.customize( 'hm_style_setting',function( setting ) {
                    var newHeaderStyle = setting.get();

                    if ( 'logo-top-nav' === newHeaderStyle ) {
                        var headerHeight   = $('#masthead).height();
                        
                        console.log(headerHeight);
        
                    }

                });


            });
        }
    });

})();

我的问题是在事件DOMContentLoaded上没有其他资源被加载。 为了获得正确的标题高度,必须加载徽标,但此时还没有。

有人知道如何解决此问题吗?

非常感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。