masonry icon indicating copy to clipboard operation
masonry copied to clipboard

Reloading masonry after ajaxing new content

Open alexvaxx opened this issue 12 years ago • 2 comments

Hi desandro, thanks for a really great plugin.

I'm having some issues getting masonry to work after loading in new content with ajax. I think I've read every single question on stackoverflow and here about appending items to a masonry, as well as the documentation, and I haven't come up with an answer that seems to able to do what I want.

Here's a link to my reduced test case: http://alxvo.com/reduced (click on the images on the front page)

As you can see, it's a masonry of images, when one image is clicked, all contents fade out, and a new page is ajaxed in. Once the new images are loaded, I want to have masonry lay them out.

I have left out any attempts to write the necessary code myself, as none of the things I did, seemed to work properly.

Any advice here would be greatly appreciated.

alexvaxx avatar Mar 14 '14 14:03 alexvaxx

maybe this would help, #516

I too, was banging my head on the wall for a few days before achieving what I want.. :D

SilverKenn avatar Mar 17 '14 01:03 SilverKenn

I finally managed to get it to work!

I think there were a variety of reasons why it didn't work but with help from the threads here and other places it now does exactly what I wanted it to do.

One of the problems I had, seems to be that I hid the container while the data was loading. I hid it with fadeOut and fadeIn which seems to cause problems with masonry. Insted of hiding it per se, I animated the opacity to 0 and back to 1 once the data is loaded. The script is as follows:

   $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);

        if (newHash) {
            $('#ajaxcontainer').fadeTo(500, 0, function() {

                $ajaxSpinner.fadeIn();
                $mainContent.empty();
                $.get(newHash, function(data){

                    var $data = $(data).find("#container > *");

                    $container.prepend($data).imagesLoaded(function(){
                        $container.masonry( 'prepended', $data, true );
                    });

                    $ajaxSpinner.fadeOut( function() {
                        $('#ajaxcontainer').fadeTo(1000, 1);
                    });     
                });

            });
        };

    });

alexvaxx avatar Mar 17 '14 14:03 alexvaxx