简体   繁体   English

jQuery:正确初始化砌体插件?

[英]jquery: Initialising masonry plugin properly?

I would like to use the masonry plugin by Dave DeSandro for the construction of a site. 我想使用Dave DeSandro砌体插件来构建站点。 As a first step, in order to learn how it works, I tried recreating one of the demos on the masonry page, this one. 第一步,为了学习其工作原理,我尝试在砖石页面上重新创建一个演示

Unfortunately, recreating it did not work, the layout of the divs is not working as intended. 不幸的是,重新创建它没有用,div的布局无法按预期工作。 But I don't understand why, here's a jsfiddle of my attempt to recreate exactly what the demo shows: 但是我不明白为什么,这是我尝试完全重现演示内容的尝试:

https://jsfiddle.net/johschmoll/L1L2kgkz/2/ https://jsfiddle.net/johschmoll/L1L2kgkz/2/

Can someone point out the mistake to me? 有人可以向我指出错误吗? I believe it has something to do with me not initialising the plugin properly with jquery? 我相信这与我没有正确使用jquery初始化插件有关吗?

I am trying to initialise the plugin just as it is mentioned in the documentation of the demo : 我正在尝试初始化插件,就像在演示文档中提到的那样:

$('.grid').masonry({
  // set itemSelector so .grid-sizer is not used in layout
  itemSelector: '.grid-item',
  // use element for option
  columnWidth: '.grid-sizer',
  percentPosition: true
})

UPDATE: 更新:

I'm slightly more confused, now that recreating it on codepen worked perfectly well https://codepen.io/anon/pen/JrjmRa 我有点困惑,现在在Codepen上重新创建它效果很好https://codepen.io/anon/pen/JrjmRa

you have to first load the jquery, then the masnry plugin. 您必须先加载jquery,然后再加载masnry插件。 see the update here jsfiddle 在这里查看更新jsfiddle

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="https://masonry.desandro.com/masonry.pkgd.js"></script>

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

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