I think I have a workaround for you, but you'll need to give it a try to be sure. It works when I step through the code and do it manually, and I think it'll work. I'll explain at the bottom.
Before the line of code where you call the masonry()
plugin...
$('#grid').masonry({ columnWidth:250, itemSelector:".awallpost", animate:false, resizeable:false });
...set the height of the grid to its current height.
var $grid = $('#grid');
$grid.height( $grid.height() ); // fix the height at its current height
$('#grid').masonry({ columnWidth:250, itemSelector:".awallpost", animate:false, resizeable:false });
If that gives you problem when you initially load, or in some browsers, then try setting the .height()
only if it is greater than some amount. Try 0
or some small number like 250
.
var $grid = $('#grid');
var height = $grid.height();
if( height > 0 ) { // or some other number
$grid.height( height );
}
$('#grid').masonry({ columnWidth:250, itemSelector:".awallpost", animate:false, resizeable:false });
The cause of the issue seems to be because masonry
sets the positioning of each item to absolute
, then it walks through them and sets its top
and left
positions manually to whatever they were before they received absolute
positioning.
The trouble is that until masonry
has a chance to set the positions, the #grid
becomes collapsed down to 0
height since the items inside no longer affect its height.
It appears as though in some browsers, when the height of the #grid
(and therefore the rest of the document) is collapsed, it forgets its scroll position. That's why we fix its height to a set amount before calling masonry()
, so that it doesn't collapse when its content gets absolute
positioning.