我正在尝试用无限卷轴和同位素构建网格。我还使用ImagesLoaded插件处理相互重叠的图像。问题是,即使我触发了同位素初始化的图像,但在同位素重新显示之前,我仍然会得到重叠的图像。以下是我的JavaScript代码:
jQuery(document).ready(function($){
//var $ = jQuery;
// init Isotope
var $grid = $(\'#grid\');
$grid.imagesLoaded().done( function() {
$grid.isotope({
itemSelector: \'.element-item\',
percentPosition: true,
layoutMode: \'masonry\',
masonry: {
columnWidth: \'.grid-element-sizer\',
}
});
});
// bind filter button click
$(\'.filters-button-group\').on( \'click\', \'button\', function() {
var filterValue = $( this ).attr(\'data-filter\');
$grid.isotope({ filter: filterValue });
});
// change is-checked class on buttons
$(\'.button-group\').each( function( i, buttonGroup ) {
var $buttonGroup = $( buttonGroup );
$buttonGroup.on( \'click\', \'button\', function() {
$buttonGroup.find(\'.is-checked\').removeClass(\'is-checked\');
$( this ).addClass(\'is-checked\');
});
});
});
jQuery(document).ready(function($){
infinite_count = 0;
// Triggers re-layout on infinite scroll
$( document.body ).on( \'post-load\', function () {
infinite_count = infinite_count + 1;
var $container = $(\'#grid\');
var $selector = $(\'#infinite-view-\' + infinite_count);
var $elements = $selector.find(\'.hentry\');
$elements.hide();
$container.imagesLoaded().done( function(){
$container.append( $elements ).isotope( \'appended\', $elements );
$elements.fadeIn();
//$container.isotope(\'reloadItems\')
});
});
});
我的错误在哪里?触发同位素过滤或调整浏览器窗口大小后,一切都已修复。
最合适的回答,由SO网友:worldwildwebdev 整理而成
问题是,我将图像放在同位素网格上,而不是新添加的项目。我只更改了添加新项目时触发的事件,以等待加载新添加帖子的图像,然后将其附加到同位素布局。
infinite_count = 0;
// Triggers re-layout on infinite scroll
$( document.body ).on( \'post-load\', function () {
var $container = $(\'#grid\');
infinite_count = infinite_count + 1;
var $selector = $(\'#infinite-view-\' + infinite_count);
var $elements = $selector.find(\'.hentry\');
$elements.hide();
$elements.imagesLoaded().done( function(){
$container.append( $elements ).isotope( \'appended\', $elements );
$elements.fadeIn();
});
});