因此,关于masonry.js,我遇到了一个奇怪的问题,在其他地方似乎找不到。

我有the latest (3.10) version,并将其包含在我的wordpress主题的functions.php文件中,如下所示:

function enqueue ()
{
    wp_register_script( 'masonry', get_stylesheet_directory_uri() . '/js/masonry.js', array( 'jquery' ) );
    wp_enqueue_script( 'masonry' );
}

add_action( 'wp_enqueue_scripts', 'enqueue');

加载脚本很好。现在,我什么都没做,但是脚本失败了:
Uncaught TypeError: Cannot call method 'create' of undefined masonry.js?ver=3.5.2:37

似乎它无法在window.Outerlayer上调用create,因为它不存在。

这是有问题的代码,从masonry.js的第34行开始:
// used for AMD definition and requires
function masonryDefinition( Outlayer, getSize ) {
  // create an Outlayer layout class
  var Masonry = Outlayer.create('masonry');

  Masonry.prototype._resetLayout = function() {
    this.getSize();
    this._getMeasurement( 'columnWidth', 'outerWidth' );
    this._getMeasurement( 'gutter', 'outerWidth' );
    this.measureColumns();

    // reset column Y
    var i = this.cols;
    this.colYs = [];
    while (i--) {
      this.colYs.push( 0 );
    }

    this.maxY = 0;
  };

我尝试以不同的方式使脚本入队,以及仅在$(document).ready()时才将其拉入(我相信wordpress的enqueue_script仍然可以吗?)。

那么,有谁知道这里可能存在什么问题或冲突?还是有人经历过类似的经历?

(我使用的是jQuery 1.83,尽管根据现场情况,砌体不需要jquery。)

最佳答案

我有这个确切的问题。我的猜测是您仅使用github中的raw masonry.js文件。此版本的砌体需要其他几个插件才能正常工作。

具体来说,由于未安装Outlayer,因此收到此错误。如果您只是想让砌体投入使用,则应从masonry website here.中获取完整的生产包,其中包括必要的插件。

希望这可以帮助!

关于javascript - Masonry.js加载失败。无法在Wordpress中调用未定义(外层)的 'create',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17992216/

10-12 18:30