Masonry:在AJAX回调上创建一个网格
Masonry: Creating a grid on AJAX callback
所以我对Masonry很熟悉,但我以前从未这样做过。
我知道在通过ajax加载页面后向网格添加项目的"added"方法,但这并不是我想要做的
我的网格结构在通过ajax调用之前根本不存在。
例如(伪代码):
- 页面加载
- 用户单击链接
- AJAX调用,返回HTML字符串并通过jQuery添加到dom中
- 需要实例化基于此HTML的砖石网格
基本上,通过AJAX返回的HTML看起来像这样:
<div class="grid-57">
<a class="item" href="http://www.example.com">
<img src="http://fpoimg.com/350x350"/></a>
</a>
<a class="item" href="http://www.example.com">
<img src="http://fpoimg.com/350x250"/></a>
</a>
<a class="item" href="http://www.example.com">
<img src="http://fpoimg.com/350x450"/></a>
</a>
</div>
所以,正如您所看到的,我并没有添加到现有的网格或砖石结构中,我需要在AJAX成功返回时创建一个新的网格或砌体。
我的JS看起来像这样:
$('.get-grid').click(function(e){
var id = $(this).data('id');
$.post(ajaxurl, { action: 'get_grid', id: id }, function(response) {
// Append this html to the area of the site where I have the grids.
$('.all-the-grids').append(response);
// Here is where i need to actually turn this HTML that I just added into a masonry object and lay it out.
var $container = $('.grid-' + id);
msnry = new Masonry( $container[0], {
columnWidth: 188,
itemSelector: '.item',
gutter: 10
});
msnry.on('layoutComplete', function(){
// never making it in here.
});
});
});
知道怎么做吗?
编辑
看起来我的网格布局正确,没有错误,但它从未到达这里的layoutComplete回调。
以与通常使用Masonry相同的方式进行操作,在回调中进行操作并不会真正改变任何事情。
$('.get-grid').click(function(e){
var id = $(this).data('id');
$.post(ajaxurl, { action: 'get_grid', id: id }, function(response) {
// Append this html to the area of the site where I have the grids.
var grids = $('.all-the-grids');
grids.append(response);
var msnry = new Masonry(grids[0], {
});
});
});
当您在Layout中使用Images时,首先要做的就是等待所有图像加载到该元素中,然后实例化。尝试以下代码
$containers = $('.all-the-grids');
$containers.append(response).imagesLoaded(function() {
$containers.masonry({
itemSelector: '. a'
});
});
注意:imagesLoaded
是一个jQuery插件
相关文章:
- 使用MixItUp在一个网格中进行多个排序
- 用javascript制作一个网格视图图像
- EXTJS 4.2:点击按钮,在新窗口中将选定的网格行从一个网格复制到另一个网格
- Masonry:在AJAX回调上创建一个网格
- 在 threejs 中确定一个网格是否在另一个网格内
- 我在同一域中并排打开了两个浏览器.我想将一个网格的选定行拖到另一个浏览器.可能吗
- 如何使用 ExtJS 4.1 将网格放入另一个网格的单元格中
- 我如何浏览一个网格,询问是否使用 jQuery 选中了复选框
- 构建一个网格并使用jQuery捕捉到它
- 当我的页面有两个网格时,如何在一个网格中设置td的样式
- 如何创建一个网格,其中可变数量的正方形会自动调整大小以适应空间
- Sencha ExtJS 4.2:如何在一个网格中同步两个组合编辑器
- 相机's位置不改变时,添加到一个网格
- 剑道-在选择网格单元格时更新另一个网格(mvvm)
- 建立一个网格的圆圈与文本内部和…移动东西
- 如何从一个网格过渡到另一个
- ExtJs双击网格行应该将项目移动到另一个网格
- 如何使一个网格布局与未知数量的列,每一列有相同的大小
- 如何将三个.js网格合并为一个网格
- three.js:如何为一个网格获得多个材料