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

Alternative method to onload in dom? #45

Closed
oskarrough opened this issue Dec 14, 2013 · 4 comments
Closed

Alternative method to onload in dom? #45

oskarrough opened this issue Dec 14, 2013 · 4 comments

Comments

@oskarrough
Copy link

Hi, might be a stupid question but how'd you use lazyload without onload attributes on every element? Is it possible to initialize the method globally?

@vvo
Copy link
Owner

vvo commented Dec 15, 2013

Yes that is a very good idea and depending on how we would implement it, it could solves issues like #45.

Do you have any idea on how the manual initialize would look like?

@oskarrough
Copy link
Author

I would look for inspiration in https://github.com/desandro/imagesloaded

One method would be to define a container and attach lazyload to every image in it. A setting could override what we're looking for, allowing us to get more elements than just images. Like iframes and the other things currently supported.

Something along document.getElementsByClassName('lazyload')

@vvo
Copy link
Owner

vvo commented Jan 25, 2014

@oskarrough That is a good idea, and we can tackle it from different angles:

  1. Have an onload attribute on the container div.

    <div onload="lazyload.method(options)">
      <img data-src= src= > ..
    </div>

    In this solution when the img loads the blank src, it will trigger an onload on the container div and so you do not need
    to add onload= on every image.

    There is no delay compared to the second solution below:

  2. Adding an elements options

    <div id="lazy">
      <img data-src src class=lazyimg >
      <img data-src src class=lazyimg >
      <img data-src src class=lazyimg >
      <img data-src src class=lazyimg >
      <img data-src src class=lazyimg >
      <script>
      lazyload({
        container: document.getElementById('lazy'),
        elements: document.getElementsByClassName('lazyimg')
      });
      </script>
    </div>

    As you can see,
    the drawback to not using the onload="" attribute on the images or on container div is that
    you will need to ask the DOM for the elements.

    Rather than having the elements register themselves to the lazyload instance.

    It means you will have to wait for the elements to be in the DOM
    to query them and you will loose some time so the first images will take more time to load.

What do you think?

vvo added a commit that referenced this issue Jan 26, 2014
+ with right script position

fixes #49 #45
@vvo
Copy link
Owner

vvo commented Mar 5, 2014

Closing as no further discussion for the moment

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

No branches or pull requests

2 participants