ressio / lazy-load-xt

Lazy load XT is a jQuery plugin for images, videos and other media
http://ress.io/lazy-load-xt-jquery/
MIT License
1.36k stars 243 forks source link

Lazy loading on scrolling div (iPad) #24

Closed shi-sergiom closed 9 years ago

shi-sergiom commented 9 years ago

Hello, I'm having some problems trying to make a scrollable div lazy load the images inside. It works perfectly on the desktop browser but when on mobile safari when I scroll (scrolls with momentum) the images are not shown until the scrolling stops completely. This also means that the onshow function is not executed until the scrolling stops. BTW I'm using both spinner and fade effect.

This is how I'm initialising the plugin.

$.extend($.lazyLoadXT, {
                    autoInit:false,
                    edgeY:0,
                                scrollContainer: "#searchResultsList",
                    onshow: function(){
                        $(this).addClass('lazy-hidden lazy-hidden-spinner lazy-force-spinner');
                }, 
                    onload: function(){
                        //remove the forced spinner first to allow fade
                        $(this).removeClass("lazy-force-spinner");
                        $(this).removeClass("lazy-hidden-spinner");
                        $(this).removeClass("lazy-hidden");
                        $(this).addClass("lazy-loaded");
                    }
            });

Later on the code once an item has been added to the DOM I do the following.

var toAppend = $( itemHTML );
resultsContainer.append(toAppend);
toAppend.find("img[data-src]").lazyLoadXT();

Related CSS is below. I am using -webkit-transform: translate3d(0,0,0); in the scrollable div to use hardware acceleration but that doesn't help. I also tried adding it to the images but still the same.

.lazy-hidden-spinner
{
    background:url('../images/lazy_loading.gif') no-repeat 50% 50%;

}

.lazy-force-spinner
{
    height: 100%;
    width: 100%;
    opacity: 1 !important;
}

#searchResultsList
{
    padding:0px 62px;
    height: 100%;   
    overflow:scroll;  
    -webkit-overflow-scrolling: touch;      
    -webkit-transform: translate3d(0,0,0);
}

I'm sure there is something I'm missing but cant figure out what.

Thanks for and awesome plugin.

Sergio

Update

I noticed that the problem really is because the scroll event is not triggered on safari mobile when "momentum scrolling".

dryabov commented 9 years ago

It's known issue in Safari (it uses "screenshot" of the page during scroll, in iPhone 1 it was significant performance optimization, but now it looks like a kind of legacy code). The only solution is to move page content into manually positioned layer and process touchmove event to change its position (see projects like https://github.com/cubiq/iscroll or https://github.com/filamentgroup/Overthrow/). Anyway, it is too complicated thing to be implemented in the framework of LazyLoadXT project.