14

所以我有一个奇怪的问题,我似乎在其他任何地方都找不到,关于 masonry.js。

我有最新的(3.10)版本,我将它包含在我的 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。)

4

1 回答 1

33

我有这个确切的问题。我的猜测是您只使用来自 github 的原始 masonry.js 文件。这个版本的砌体需要一些其他插件才能正常工作。

具体来说,您收到此错误是因为您没有安装Outlayer。如果您只是想启动并运行砌体,则应从此处的砌体网站获取完整的生产包。 它包括必要的插件。

希望这可以帮助!

于 2013-08-15T20:39:10.267 回答