最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery masonry layout complete event not working - Stack Overflow

programmeradmin7浏览0评论

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

/

<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?

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

http://jsfiddle/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?

Share Improve this question edited Dec 10, 2014 at 12:01 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Dec 10, 2014 at 12:00 tuscan88tuscan88 5,82912 gold badges55 silver badges111 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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 plete');
});
// manually trigger initial layout
$grid.masonry();

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

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

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();

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

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

You can even make a chain:

$('#items').on('layoutComplete', function() {
  console.log('plete');
})
.masonry({
  itemSelector: '.item',
  columnWidth: 220,
  gutter: 20
});
发布评论

评论列表(0)

  1. 暂无评论