imagesLoaded jquery function not working with masonry

风流意气都作罢 提交于 2019-12-25 02:46:07

问题


I've been working on this for some time.

I am trying to use a fadein function after all of my post content has been loaded and had the relevant masonry layout applied. Have researched this in several places, tried different versions of the imagesLoaded plugin but cannot seem to get it working.

This is my basic html structure:

<div id="posts">

    <article class="post text clearfix">
    // text post etc
    </article>

</div>

I am calling masonry and then the imagesLoaded jquery plugin.

My js looks like this (I realise I need to set display:none in the css at some point).

<script type="text/javascript">
$(document).ready(function() {
    var $eachContainer = $('.post');
    $eachContainer.hide();
    var $wall = $('#posts');
    $wall.imagesLoaded( function() {    
        $eachContainer.fadeIn();
        $wall.masonry({
            columnwidth: 84,
            itemSelector : '.post'
        });
    });
});
</script>

I've tried running the above in a window.load function too. It seems as though with this function the masonry columnWidth is not being calculated.

I actually have two tumblrs where this code is being tested.

  1. http://kidsofdada.tumblr.com/ (this is working but I want the images to fade in AFTER the masonry grid has been set.
  2. http://childofdada.tumblr.com/ images fading in, but masonry border not setting properly, also not resizing when the browser is dragged.

EDIT 1:

I have tried to recode the above function using the example provided at the plugin site. So now my code looks like this:

 var container = document.querySelector('#posts');
 var msnry = new Masonry( container, {
    columnWidth: 84,
    itemSelector: '.post',
    isInitLayout: false
});

msnry.on( 'layoutComplete', function() {
    console.log('layout is complete');
    $('.post').fadeIn();
});

When the page is loaded all posts are hidden. How do I bind the function only to the window.load, or document.ready functions? If you resize the browser, this triggers and will display the posts: http://childofdada.tumblr.com/

EDIT 2:

Trying to implement the false flag code, I realise I may have this written incorrectly (have tried switching it).

var firstTime = 'false';

var container = document.querySelector('#posts');
var msnry = new Masonry( container, {
    columnWidth: 84,
    itemSelector: '.post',
    isInitLayout: false
});

if (firstTime == 'false') {
    msnry.on( 'layoutComplete', function() {
        console.log('layout is complete');
        $('.post').fadeIn();
        firstTime = 'true';
    });
}

回答1:


I hope this is what you want: http://jsfiddle.net/nVYcP/

What i had to do is hide the body with visibility: hidden, and not hide() (which set display: none). This tricks jQuery/masonry to calculate the correct places.

After the .masonry you display it back by first hiding with the jQuery accepted method, removing the visibility tag, and showing with fadeIn. The time here have to be a bit big (i put 2 seconds) or else the animation is never visible due to the lag to fade in the large amount of itens.

The code to show the posts can be put in the imagesLoaded callback as well, but I think it's working fine this way.

Final code:

$('#posts').masonry({columnWidth: 84});

$("#site")
    .hide()
    .css("visibility", "")
    .fadeIn(2000);


来源:https://stackoverflow.com/questions/21511005/imagesloaded-jquery-function-not-working-with-masonry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!