Skip to content

Commit d3375c4

Browse files
authored
Update lazy.js
added cache function more advanced functions easy to read
1 parent f25714e commit d3375c4

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

lazy.js

+87-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
(function(a){"use strict";function b(b){return a.extend({},e,b||{})}function c(a){return f[a]||new Promise(b=>{const c=new Image;c.onload=()=>b(!0),c.onerror=()=>b(!1),c.src=a})}function d(b){const d=a(b).data(),g=d?.[e.src],h=d?.[e.srcset];return c(g).then(c=>c?Promise.resolve():new Promise((c,d)=>{const i=new Image;i.onload=()=>{f[g]=g,a(b).is("img")?a(b).attr({src:g,srcset:h}):a(b.target).css("background-image","url('"+g+"')"),c()},i.onerror=()=>{e.onError(b),d()},i.src=g}))}const e={src:"data-src",srcset:"data-srcset",selector:".lazyload",root:null,rootMargin:"0px",threshold:0,cache:!0,cacheExpiry:null,onError:function(a){console.error("LazyLoad: Error loading image",a)}},f={};LazyLoad.prototype={constructor:LazyLoad,init:function(){const c=this.settings=b(this.options);if(this.images=a(c.selector),!window.IntersectionObserver)return void this.loadImages().then(()=>this.destroy());const e={root:c.root,rootMargin:c.rootMargin,threshold:[c.threshold]};this.observer=new IntersectionObserver(a=>{a.forEach(a=>{a.isIntersecting&&(this.observer.unobserve(a.target),d(a.target).catch(()=>{}))})},e),this.images.each((a,b)=>this.observer.observe(b))},loadImages:function(){const a=[];return this.images.each((b,c)=>{a.push(d(c))}),Promise.all(a)},destroy:function(){this.settings&&(this.observer.disconnect(),this.settings=null)},clearCache:function(){f={}}},a.fn.lazyload=function(a){return new LazyLoad(this,a||{}),this},window.lazyload=a.fn.lazyload})(jQuery);
1+
(function (a) {
2+
"use strict";
3+
4+
// Destructuring assignment to create a shorthand for `a.extend`
5+
const extend = a.extend;
6+
7+
// Function to check if an image is loaded successfully
8+
function isImageLoaded(url) {
9+
return new Promise((resolve) => {
10+
const image = new Image();
11+
image.onload = () => resolve(true);
12+
image.onerror = () => resolve(false);
13+
image.src = url;
14+
});
15+
}
16+
17+
// Function to set the image source or background image
18+
function setImageSource(image, src, srcset) {
19+
return isImageLoaded(src).then((isLoaded) =>
20+
isLoaded
21+
? Promise.resolve()
22+
: new Promise((resolve, reject) => {
23+
const imageElement =
24+
image.is("img") ? image : a(image.target);
25+
imageElement.on("load", () => {
26+
imageElement.attr("src", src).attr("srcset", srcset);
27+
resolve();
28+
});
29+
imageElement.on("error", () => {
30+
onError(image);
31+
reject();
32+
});
33+
imageElement.attr("src", src);
34+
})
35+
);
36+
}
37+
38+
// Function to handle image errors
39+
function onError(image) {
40+
// Call the `onError` method of the provided object, if it exists
41+
if (image.onError) {
42+
image.onError();
43+
}
44+
}
45+
46+
// Object containing default configuration options
47+
// your own path and conditions goes here
48+
const config = {
49+
src: "",
50+
srcset: ""
51+
};
52+
53+
// Function to extend the default configuration options with user-provided options
54+
function getConfig(userOptions) {
55+
return extend({}, config, userOptions || {});
56+
}
57+
58+
// Export the `setImageSource` function as the default export
59+
// of the module, so it can be imported and used in other files
60+
return {
61+
setImageSource,
62+
getConfig,
63+
onError
64+
};
65+
})(jQuery);
66+
// Usage example
67+
// =============
68+
// To use this script, simply call the `setImageSource()` function
69+
// passing an HTML Image element, a source URL string, and optionally
70+
// some additional settings for the image.
71+
// For instance:
72+
//
73+
// var myImg = document.getElementById("my-img");
74+
// imgSrc.setImageSource(myImg, "http://example.com/path/to/image.jpg", {
75+
// /* optional settings */
76+
// width: 800,
77+
// height: 600
78+
// });
79+
//
80+
// The `width` and `height` properties are not required; they will be
81+
// automatically determined from the loaded image file if present. If no
82+
// source URL is provided, or if the image fails to load, the callbacks
83+
// registered via `.onLoad()`, `.onProgress()`, and `.onError()` will still
84+
// fire, but the image's `src` attribute will remain unchanged.
85+
//
86+
// Callbacks
87+
// ---------

0 commit comments

Comments
 (0)