简体   繁体   English

Polyfill for wordpress中的enquire.js

[英]Polyfill for enquire.js in wordpress

I'm trying to use Enquire.js in my Wordpress install. 我正在尝试在Wordpress安装中使用Enquire.js。 I'm trying just to push the left column under the central column for small screens. 我正在尝试仅将左列推到小屏幕的中央列下方。

I got it to work in most browser my the laptop, but it does not work on older Androids browser (2.2), saying it needs a polyfill. 我可以在笔记本电脑的大多数浏览器中使用它,但不能在较旧的Android浏览器(2.2)上运行,因为它需要使用polyfill。 I tried to get this polyfill to work, but still not getting the functionality on Android, I still get the warning about matchmedia polyfill in android console. 我试图使此polyfill正常工作,但仍未获得Android上的功能,但仍收到有关Android控制台中matchmedia polyfill的警告。

Please help me pinpoint what is possible problem. 请帮助我找出可能的问题。 Basically, I load couple of scripts in function.php: 基本上,我在function.php中加载了几个脚本:

function rr_scripts() {
  wp_enqueue_script( 'polyfill', get_stylesheet_directory_uri() . '/js/polyfill.js', array( 'Modernizr' ), '1.0.0', true );
  wp_enqueue_script( 'enquire', get_stylesheet_directory_uri() . '/js/enquire.js', array( 'jquery' ), '1.0.0', true );
  wp_enqueue_script( 'reorder', get_stylesheet_directory_uri() . '/js/reorder.js', array( 'enquire' ), '1.0.0', true ); 
} 
add_action( 'wp_enqueue_scripts', 'rr_scripts' );

Then my Child template has js folder with couple of files, polyfill.js, that is supposed to polyfill the mediamatch (media.match.js is in the same folder): 然后我的Child模板有一个js文件夹,其中包含几个文件polyfill.js,应该用来填充mediamatch(media.match.js在同一文件夹中):

Modernizr.load([
{
    test: window.matchMedia,
    nope: "media.match.js"
} ]);

reorder.js (below), that actually uses the enquire.js that is in the same folder as well: reorder.js(如下所示),实际上也使用了同一文件夹中的enquire.js:

jQuery(document).ready(function() {
   enquire.register("screen and (max-width: 599px)", {
       match: function() { jQuery('#leftside').insertAfter('#primary'); },
       unmatch: function() { jQuery('#leftside').insertBefore('#primary'); }
   }); 
});

If I understand your scenario correctly, then it might be related to a timing issue, ie enquire.js is being loaded before media.match.js . 如果我正确理解了您的情况,则可能与计时问题有关,即enquire.jsmedia.match.js之前被加载。 I'm not sure how .wp_enqueue_script() works but you could try the following (based on this comment: 我不确定.wp_enqueue_script()工作方式,但是您可以尝试以下操作(基于评论:

Modernizr.load([{
    // First test requirement for .matchMedia() polyfill
    test: window.matchMedia,
    nope: 'media.match.min.js'
  }, {
    // ...and then load enquire.js
    load: 'enquire.min.js',
    complete: function() {
      enquire.register(...);
    }
  }
]);

Side note: enquire.js doesn't depend on jQuery. 旁注:enquire.js不依赖jQuery。

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

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