samedi 28 janvier 2017

Triggering a function after ajax call has finished loading images in Laravel

I've got an image gallery on my page, which works a lot like pinterest.

$images = Images::paginate(5);

On my image page, I start off with 5 images.

Controller:

if($request->ajax()) {
    return [
        'images' => view('browse.partials.imagePartials')->with(compact('images'))->render(), 'next_page' => $images->nextPageUrl()
    ];
}           

return view('browse.combined_results')->with('images', $images);

Markup:

<div id="freewall" class="freewall endless-pagination" data-next-page=>
@foreach ($images as $image)
<a href="">
    <div class='brick featuredTextContainer' style="width:150px">
        <img src='' width='100%' class="profileImg">
        <span class="featuredArtistName"></span>             
    </div>
</a>
@endforeach
</div>

These images are arranged using freewall:

var wall = new Freewall("#freewall");
wall.reset({
    selector: '.brick',
    animate: true,
    cellW: 150,
    cellH: 'auto',
    onResize: function() {
        wall.fitWidth();
    }
});

var images = wall.container.find('.brick');
images.find('img').on('load', function() {
    wall.fitWidth();
});

wall.fitWidth();    

wall.fitWidth() is the method that arranges the images all neatly, like so:

enter image description here

However, if you scroll down, you get an ajax request

$(window).scroll(fetchImages);

function fetchImages() {
    var page = $('.endless-pagination').data('next-page');

    if(page !== null) {

        clearTimeout( $.data( this, "scrollCheck" ) );

        $.data( this, "scrollCheck", setTimeout(function() {
            var scroll_position_for_images_load = $(window).height() + $(window).scrollTop() + 100;

            if(scroll_position_for_images_load >= $(document).height()) {
                $.get(page, function(data){
                    $('.endless-pagination').append(data.images);
                    $('.endless-pagination').data('next-page', data.next_page);
                });

            }
        }, 350))

    }
}

The ajax request loads a partial, which is essentially just the next page of the pagination.

imagePartial.blade.php

@foreach ($images as $image)
        <a href="">
            <div class='brick featuredTextContainer' style="width:150px">
                <img src='' width='100%' class="profileImg">
                <span class="featuredArtistName"></span>             
            </div>
        </a>
@endforeach

In other words, it loads in more images from the $images pagination into the images container.

Which is great. It works. However, the images aren't arranged right away. You have to execute wall.fitWidth() once more for the images to fit into place. The problem is, I can't simply put this at the end of the fetchImages function, because the images take a moment to load in. Therefore, all the images are loaded behind the other images, unarranged.

A solution to this would be to, say, have the new images loaded in with opacity set to 0, have the wall.fitWidth() function executed after a check to make sure all the images have been loaded into the DOM, THEN set the images back to opacity: 1. But I'm not sure how to do this.

Help?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire