jQuery masonry layout complete event not working
Asked Answered
P

4

5

I have followed the documentation exactly and the layout complete event isn't working. Example can be seen here:

http://jsfiddle.net/9464buy5/

<div id="items">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>
#items {
    width: 500px;
}
.item { 
    background: #ff0000;
    width: 200px;
    height: 200px;
    margin-bottom:20px;
}
$(document).ready(function() {
    var $container = $('#items');
    $container.masonry({
        itemSelector: '.item',
        columnWidth: 220,
        gutter: 20
    });

    $container.masonry('on', 'layoutComplete', function(msnryInstance, laidOutItems) {
        alert("");
    });
});

Anyone know if this a known bug or have I done something wrong?

Papen answered 10/12, 2014 at 12:0 Comment(0)
P
3

Ah I've sorted it using the non jquery method. You have to call the following function after initialisation to trigger the event:

msnry.layout();

e.g

var container = document.querySelector('.masonry');
var msnry = new Masonry( container, {
    itemSelector: '.item',
    columnWidth: 220,
    gutter: 20
});

msnry.on( 'layoutComplete', function( msnryInstance, laidOutItems ) {
    alert("");
});

msnry.layout();
Papen answered 10/12, 2014 at 12:23 Comment(0)
A
7

Here is the code to do this in jQuery:

var $grid = $('.grid').masonry({
    // disable initial layout
    isInitLayout: false,
    //...
});
// bind event
$grid.masonry( 'on', 'layoutComplete', function() {
    console.log('layout is complete');
});
// manually trigger initial layout
$grid.masonry();

This was taken from the bottom of this page: http://masonry.desandro.com/options.html

Agro answered 1/9, 2015 at 17:21 Comment(0)
B
4

You can try for

var msnry = new Masonry( $container, {
     itemSelector: '.item',
        columnWidth: 220,
        gutter: 20
  });

Then use:

msnry.on( 'layoutComplete', function( msnryInstance, laidOutItems ) {
    //custom code
  });

And trigger layout to call layoutComplete callback

msnry.layout();

Have look at this codepen

Blackberry answered 10/12, 2014 at 12:13 Comment(2)
Yeah I tried that see this jsfiddle: jsfiddle.net/9464buy5/1 still doesn't work thoughPapen
@geoffs3310, use msnry.layout(); to trigger layoutCompleteBlackberry
P
3

Ah I've sorted it using the non jquery method. You have to call the following function after initialisation to trigger the event:

msnry.layout();

e.g

var container = document.querySelector('.masonry');
var msnry = new Masonry( container, {
    itemSelector: '.item',
    columnWidth: 220,
    gutter: 20
});

msnry.on( 'layoutComplete', function( msnryInstance, laidOutItems ) {
    alert("");
});

msnry.layout();
Papen answered 10/12, 2014 at 12:23 Comment(0)
M
3

You just need to add the event listener before you call masonry()

var $container = $('#items');
$container.on('layoutComplete', function() {
  console.log('complete');
});
$container.masonry({
  itemSelector: '.item',
  columnWidth: 220,
  gutter: 20
});

You can even make a chain:

$('#items').on('layoutComplete', function() {
  console.log('complete');
})
.masonry({
  itemSelector: '.item',
  columnWidth: 220,
  gutter: 20
});
Maestoso answered 14/7, 2017 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.