Skip to content

Use lazysizes only on specified element #399

Closed
@alexander-schranz

Description

@alexander-schranz

Is there a way using lazysizes without the class attribute and getElementsByClassName and initialize it in JS for a specific element.

E.g.:

lazySizes.addElement(DOMNode);
var mainImage = document.getElementById('main-image');
lazySizes.addElement(mainImage);

Activity

aFarkas

aFarkas commented on Jun 8, 2017

@aFarkas
Owner

Not really. Even if I remove the getElementsByClassName some kind of DOM marker (attribute i.e.: data-lazyload) should always be used otherwise I could produce memory leaks. (We currently can not create weak references in JS...)

What is wrong with this?:

DOMNode.classList.add('lazyload');
alexander-schranz

alexander-schranz commented on Jun 8, 2017

@alexander-schranz
Author

I'm getting the DomNode so in my case its not necassery that lazysizes parse the dom to get the elements so I just want to avoid that. My idea was implementing something like this:

window.lazySIzesConfig.autoInit = false;


if (window.lazySIzesConfig.autoInit) {
      lazyloadElems = document.getElementsByClassName(lazySizesConfig.lazyClass);
}
lazyload.addElement(node) {
      lazyloadElems.push(node);
}
aFarkas

aFarkas commented on Jun 8, 2017

@aFarkas
Owner

@alexander-schranz
Yes, this would create a hard reference with the potential of leaking dom nodes. . Additionally it would be very different to the normal way so that it would add a lot of code.

Again, what is the problem with the DOM API(s):

DOMNode.classList.add('lazyload');

//or maybe also possible in the future:
//DOMNode.setAttribute('data-lazyload', '');
alexander-schranz

alexander-schranz commented on Jun 11, 2017

@alexander-schranz
Author

As told I have the element in JS. In this case I want to avoid to manipulate the DOM and throw a rerender.
About the hard reference I would say that a removeElement method could be added.
But I understand you when it will add a lot of code that you cant change the current behaviour.

Just to understand it how you handle it currently are you calling getElementsByClassName on every resize/scroll to not keep the nodes in the memory?

added a commit that references this issue on Jun 13, 2017
57e719f
aFarkas

aFarkas commented on Jun 13, 2017

@aFarkas
Owner

Just to understand it how you handle it currently are you calling getElementsByClassName on every resize/scroll to not keep the nodes in the memory?

Produces a live nodelist you don't need to call it.

In this case I want to avoid to manipulate the DOM and throw a rerender.

If you use lazySizes.rAF/requestAnimationFrame you shouldn't add any noticeable workload to the browser. (I would call it premature optimization.)

But I understand you when it will add a lot of code that you cant change the current behaviour.

I just exposed lazySizes.elements, if you overwrite it by adding a getter, you can implement your own logic.

But you have to handle the full remove logic and add logic yourself.

alexander-schranz

alexander-schranz commented on Jun 13, 2017

@alexander-schranz
Author

@aFarkas thanks for the create explanation didn't know about live nodelist (learned something new 😄 ) and thanks for the change. Will try using the lazySizes.elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Use lazysizes only on specified element · Issue #399 · aFarkas/lazysizes