Avoid DOM operation if applying an empty className #605
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Short version:
I noticed after image is loaded,
vanilla-lazyload
will automatically add a classloaded
.In my case is not needed (see the details in Long version section below). I'd like to avoid calling
addClass
andremoveClass
for a style not defined. It might not have any performance difference (my case I have 200 images in page and viewport can show 30 images) but who knows. The simple PR is about to check and avoid unnecessary DOM operation.Long version:
I see
vanilla-lazyload
is recommended in web.dev when I was troubleshooting why native image lazy loading is not working even in modern browsers (issue 4273). My case works in Chrome 109, but does not work in newer browsers (e.g. Chrome 120-123, Edge 122). web.dev explains that developer needs to specifywidth
andheight
, and native lazy loading might behave differently or even not working in the you expect.I think I have to use a JS based solution to fix this either implementing
IntersectionObserver
myself or usingvanilla-lazyload
. I choose the second and that's why I share this feedback.I'm using Vue and I have already a wrapper component
<LazyImage>
it wraps img element like thisBy using
vanilla-lazyload
, I want to keep changes minimal. so I only renamesrc
todatasrc
, and remain the rest unchanged.And then I initialize the lozyload like this
As you can see, if
class_*
is empty, it'll be great to skip any DOM operation.wdyt?
Thanks