Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add after-unveil event. #25

Closed
wants to merge 1 commit into from
Closed

Add after-unveil event. #25

wants to merge 1 commit into from

Conversation

joelhawksley
Copy link

This added event allows one to bind to the completion of the image srcset being set, allowing for easy implementation of fade-in effects.

@aFarkas
Copy link
Owner

aFarkas commented Nov 18, 2014

Not sure wether you need this. In case you are using jQuery, the following code should just work:

<style>
img.lazyload {
    opacity: 0;
}
</style>

<script>
$(document).on('lazybeforeunveil', (function(){
    var onLoad = function(e){
        $(e.target)
            .animate({opacity: 1})
            .off('load error', onLoad)
        ;

    };
    return function(e){
        if(!e.isDefaultPrevented()){
            $(e.target)
                .filter('img')
                    .css({opacity: 0})
                    .on('load error', onLoad)
            ;
        }
    };
})());
</script>

There is no really need to do this right after unveil.

@joelhawksley
Copy link
Author

You're right, that works just fine. Closing. Thanks for the quick response!

@joelhawksley
Copy link
Author

Maybe you could add that code to the README as an example?

@aFarkas
Copy link
Owner

aFarkas commented Nov 18, 2014

Yes will do. Additionally, I'm currently working on a small extension. Current code looks like this:

/**
 * effect plugin for lazySizes:
 *
 * Usage:
 *
 * 1. fadein after loading:
 *

    img.lazyload,
    img.lazyloading {
        opacity: 0;
    }
    img.lazyloaded {
        opacity: 1;
        transition: opactiy 300ms;
    }

 *
 * 2. fadein while loading:
 *

    img.lazyload {
        opacity: 0;
    }
    img.lazyloading {
        opacity: 1;
        transition: opactiy 300ms;
    }
 */

(function(window, document){
    'use strict';
    var config, animEvent;

    if(document.addEventListener){
        config = window.lazySizesConfig || (window.lazySizes && lazySizes.cfg);
        animEvent = 'onwebkitanimationend' in window  ? 'webkitanimationend' : 'animationend';

        if(!config){
            config = {};
            window.lazySizesConfig = config;
        }

        if(!('loadingClass' in config)){
            config.loadingClass = 'lazyloading';
        }
        if(!('loadedClass' in config)){
            config.loadedClass = 'lazyloaded';
        }



        document.addEventListener('lazybeforeunveil', function(e){
            if(!e.defaultPrevented && e.target.nodeName.toUpperCase() == 'IMG' && lazySizes.aC) {

                e.target.addEventListener('load', onLoad, false);
                e.target.addEventListener('error', onLoad, false);
                if(config.loadingClass){
                    lazySizes.aC(e.target, config.loadingClass);
                }
            }
        }, false);

    }

    function removeLoadedClass(e){
        e.target.removeEventListener('transitionend', removeLoadedClass, false);
        e.target.removeEventListener(animEvent, removeLoadedClass, false);
        lazySizes.rC(e.target, config.loadedClass);
    }

    function onLoad(e){
        e.target.removeEventListener('load', onLoad, false);
        e.target.removeEventListener('error', onLoad, false);
        if(config.loadingClass){
            lazySizes.rC(e.target, config.loadingClass);
        }
        if(config.loadedClass){
            e.target.addEventListener('transitionend', removeLoadedClass, false);
            e.target.addEventListener(animEvent, removeLoadedClass, false);
            lazySizes.aC(e.target, config.loadedClass);
        }
    }


})(window, document);

You can use css transitions or css animations.

@aFarkas aFarkas reopened this Nov 18, 2014
@joelhawksley
Copy link
Author

Cool! I'd be happy to test it once you have a PR. I'm implementing this for https://github.com/joelhawksley/SoloFolio/issues/135

@aFarkas
Copy link
Owner

aFarkas commented Nov 18, 2014

Looks great, will ping you as soon as this is pushed. Will be on saturday.

aFarkas pushed a commit that referenced this pull request Nov 19, 2014
@aFarkas
Copy link
Owner

aFarkas commented Nov 19, 2014

@joelhawksley
Hi,

I have just updated and pushed the extension to the master branch. To get this work you also need to update lazysizes itself.

As a side note, with lazysizes you can also fully substitute the current lazyloader (unveil).

Hope you like ti.

Cheers
alex

@aFarkas aFarkas closed this Nov 19, 2014
@aFarkas
Copy link
Owner

aFarkas commented Nov 26, 2014

Just wanted to inform you, that I directly added the effect feature into lazySizes. To get the loaded and loading classes you need to turn on the addClasses option. See also here and here the updated documentation.

@joelhawksley
Copy link
Author

Thanks Alex! I'm going to work on integrating this with SoloFolio when I'm back from vacation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants