lazyImages | 基于ES6的轻量级,高性能,简便的图片懒加载
kandi X-RAY | lazyImages Summary
kandi X-RAY | lazyImages Summary
lazyImages
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of lazyImages
lazyImages Key Features
lazyImages Examples and Code Snippets
Community Discussions
Trending Discussions on lazyImages
QUESTION
One of my websites has a really bad LCP, 4.6. I realized that without active lazyload my images, the LCP is 2.0 . The TCB increased by using lazyload, too. So Im thinking it must be my bad JS-Skills, so could give me anyone of you a hint?
My actual code:
...ANSWER
Answered 2020-Sep-19 at 09:43This tends to be an implementation issue.
LCP measures when the largest paint appears on the page. If you use lazy loading and the images are visible "above the fold" (without scrolling) and they are the largest paint on the page then they will be reported as LCP. This can also be the case if just the top of an image is showing.
Now if you are lazy loading them and your JavaScript is loaded late in the page load order it means that the images will get downloaded later, increasing your LCP time.
Steps to fixDon't lazy load images that appear "above the fold" just use a standard
or
element.
That's it! Lazy load everything that is out of view when the page loads as the performance gains are worth it (as well as improving bandwidth usage etc.).
TBT is looking for a "quiet time" on the CPU to determine when the main work on the page is done.
What the test also does is scroll the page to the bottom.
Because of this your function is probably being called multiple times in quick succession and bottle-necking the CPU (bearing in mind that the CPU has a 4x slowdown applied for mobile scoring to simulate a real world mobile device with less powerful CPU.)
Probable causes / possible solutionsBecause you have the event listener set to fire on 'scroll'
it is being fired multiple times and I assume you have multiple images on the page.
One thing you could do is to change your actual function so it isn't having to do as many loops each time.
i.e. once you have set your image src
or background style
, remove that item from your lazyImages
list. This reduces the amount of items that need checking when the page is scrolled and will result in better performance.
Also just check how often slick
slider causes your function to be called as that could be causing performance issues also.
The above may not fix your TBT but is good practice anyway.
QUESTION
I can't figure out why Safari (13.0.2) is throwing a TypeError: Type error
when using IntersectionObserver()
. The other major browsers don't.
ANSWER
Answered 2020-Jul-02 at 16:03I don't know how to mark a comment as answer. Anyways, the suggestion in @user4642212's comment to use document.body
instead of just document
, works.
Try
document.documentElement
ordocument.body
instead ofdocument
This may be related to
document
being anHTMLDocument
, but the other two beingHTMLElements
each. Their prototype chains diverge afterNode
. Perhaps Safari can’t handleHTMLDocuments
beingroots
inIntersectionObservers
.
Further reading provided by @SurajRao: IntersectionObserver not working in Safari or iOS
QUESTION
Following this quick guide here: https://developers.google.com/search/docs/guides/lazy-loading
Scroll down to test. It appears to be pretty straight forward. Clone the repository and then install the packages and run the node lazy load script. Problem is that it never runs and complains about syntax. I looked at the file but i'm not seeing any broken syntax.
I run this command: node lazyimages_without_scroll_events.js -h
Here is the error i get in terminal:
...ANSWER
Answered 2019-Mar-15 at 17:54QUESTION
I'm using IntersectionObserver
to replace an initially-loaded image with another one as the original image comes into the user's viewport.
I want the image to fade in, as opposed to just straight replacement.
I've tried adding a Jquery loader to the image, but it is not working as I'd like.
...ANSWER
Answered 2018-Nov-30 at 08:57Here is a solution that works decently well using jQuery animation: https://jsfiddle.net/ea7fxrL5/
There are two problems in your current code as far as I can tell:
The
fadeIn
function is actually being called before the image's source is changed to the "TN_december-happy-holidays_2.jpg" image because the load event is triggered immediately on intersection, since the image's "christmas-tree" src has already been loaded.The image is already at full opacity, so it needs to be hidden before you can fade it in.
Hope this helps!
QUESTION
ANSWER
Answered 2018-Nov-29 at 13:07It's not supported, https://caniuse.com/#feat=queryselector
You can use alternatives like findElementById
or findElementByClassName
QUESTION
I am getting a TYPE: Mismatch error in IE8 with the following code.
...ANSWER
Answered 2018-Nov-28 at 15:23With attachEvent
you need to add on + name of the event, so the event will be called onload
UPDATE
Also the second parameter of both of the event listeners, are callbacks, so they get executed when the event is triggered. To be able to achieve that, you need to remove the parenthesis of the function call.
QUESTION
I'm attempting to implement lazy loading of images using IntersectionObserver where available, and by using a polyfill otherwise (as recommended here).
...ANSWER
Answered 2018-May-31 at 20:15I usually use in-view for this sort of things whenever I cannot use observers, but you said that you've been asked not to use any external library or plugin, so I've written you this code in vanilla javascript. This code will run on IE9=< without polyfills.
Note: Please take into account that I did not make any optimizations of performance, for example, say in case all images have been loaded remove event listener and such.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install lazyImages
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page