跳转到内容

MediaWiki:Gadget-jQueryLazyload.js:修订间差异

来自萌娘共享
代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot]
标签由机器人或全自动脚本执行的操作
代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot]
标签由机器人或全自动脚本执行的操作
 
第8行: 第8行:
/* <pre> */
/* <pre> */


"use strict";
"use strict";(()=>{const t={src:"data-src",srcset:"data-srcset",selector:".lazyload",root:null,rootMargin:"0px",threshold:0},extend=(t,...s)=>{const o={},r="boolean"==typeof t&&t,i=[..."boolean"!=typeof t?[t]:[],...s],merge=t=>{for(const s in t)Object.prototype.hasOwnProperty.bind(t)(s)&&(r&&"[object Object]"===Object.prototype.toString.bind(t[s])()?o[s]=extend(!0,o[s],t[s]):o[s]=t[s])};for(const t of i)merge(t);return o};class LazyLoad{constructor(s,o){this.settings=extend(t,o||{}),this.images=s||document.querySelectorAll(this.settings.selector),this.observer=null,this.init()}init(){if(!window.IntersectionObserver)return void this.loadImages();const t=this,s={root:this.settings.root,rootMargin:this.settings.rootMargin,threshold:[this.settings.threshold]};this.observer=new IntersectionObserver(s=>{Array.prototype.forEach.bind(s)(s=>{if(s.isIntersecting){t.observer.unobserve(s.target);const o=s.target.getAttribute(t.settings.src),r=s.target.getAttribute(t.settings.srcset);"img"===s.target.tagName.toLowerCase()?(o&&(s.target.src=o),r&&(s.target.srcset=r)):s.target.style.backgroundImage=`url(${o})`}})},s),Array.prototype.forEach.bind(this.images)(s=>{t.observer.observe(s)})}loadAndDestroy(){this.settings&&(this.loadImages(),this.destroy())}loadImages(){if(!this.settings)return;const t=this;Array.prototype.forEach.bind(this.images)(s=>{const o=s.getAttribute(t.settings.src),r=s.getAttribute(t.settings.srcset);"img"===s.tagName.toLowerCase()?(o&&(s.src=o),r&&(s.srcset=r)):s.style.backgroundImage=`url('${o}')`})}destroy(){this.settings&&(this.observer.disconnect(),this.settings=null)}}window.lazyload=(t,s)=>new LazyLoad(t,s),window.jQuery&&(jQuery.fn.lazyload=function(t){const s=t||{};return s.attribute||(s.attribute="data-src"),new LazyLoad(this.toArray(),s),this})})();  
(() => {
    const defaults = {
        src: "data-src",
        srcset: "data-srcset",
        selector: ".lazyload",
        root: null,
        rootMargin: "0px",
        threshold: 0,
    };
    const extend = (_deep, ..._args) => {
        const extended = {};
        const deep = typeof _deep === "boolean" ? _deep : false;
        const args = [...typeof _deep !== "boolean" ? [_deep] : [], ..._args];
        const merge = (obj) => {
            for (const prop in obj) {
                if (Object.prototype.hasOwnProperty.bind(obj)(prop)) {
                    if (deep && Object.prototype.toString.bind(obj[prop])() === "[object Object]") {
                        extended[prop] = extend(true, extended[prop], obj[prop]);
                    }
                    else {
                        extended[prop] = obj[prop];
                    }
                }
            }
        };
        for (const obj of args) {
            merge(obj);
        }
        return extended;
    };
    class LazyLoad {
        constructor(images, options) {
            this.settings = extend(defaults, options || {});
            this.images = images || document.querySelectorAll(this.settings.selector);
            this.observer = null;
            this.init();
        }
        init() {
            if (!window.IntersectionObserver) {
                this.loadImages();
                return;
            }
            const self = this;
            const observerConfig = {
                root: this.settings.root,
                rootMargin: this.settings.rootMargin,
                threshold: [this.settings.threshold],
            };
            this.observer = new IntersectionObserver((entries) => {
                Array.prototype.forEach.bind(entries)((entry) => {
                    if (entry.isIntersecting) {
                        self.observer.unobserve(entry.target);
                        const src = entry.target.getAttribute(self.settings.src);
                        const srcset = entry.target.getAttribute(self.settings.srcset);
                        if ("img" === entry.target.tagName.toLowerCase()) {
                            if (src) {
                                entry.target.src = src;
                            }
                            if (srcset) {
                                entry.target.srcset = srcset;
                            }
                        }
                        else {
                            entry.target.style.backgroundImage = `url(${src})`;
                        }
                    }
                });
            }, observerConfig);
            Array.prototype.forEach.bind(this.images)((image) => {
                self.observer.observe(image);
            });
        }
        loadAndDestroy() {
            if (!this.settings) {
                return;
            }
            this.loadImages();
            this.destroy();
        }
        loadImages() {
            if (!this.settings) {
                return;
            }
            const self = this;
            Array.prototype.forEach.bind(this.images)((image) => {
                const src = image.getAttribute(self.settings.src);
                const srcset = image.getAttribute(self.settings.srcset);
                if ("img" === image.tagName.toLowerCase()) {
                    if (src) {
                        image.src = src;
                    }
                    if (srcset) {
                        image.srcset = srcset;
                    }
                }
                else {
                    image.style.backgroundImage = `url('${src}')`;
                }
            });
        }
        destroy() {
            if (!this.settings) {
                return;
            }
            this.observer.disconnect();
            this.settings = null;
        }
    }
    window.lazyload = (images, options) => new LazyLoad(images, options);
    if (window.jQuery) {
        jQuery.fn.lazyload = function (_options) {
            const options = _options || {};
            options.attribute || (options.attribute = "data-src");
            new LazyLoad(this.toArray(), options);
            return this;
        };
    }
})();  


/* </pre> */
/* </pre> */

2025年7月26日 (六) 00:10的最新版本

/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/jQueryLazyload/Gadget-jQueryLazyload.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]|longId=103d1a563ea4ccc8ff29fb55c9bcd88329a56eb5|shortId=103d1a56|summary=feat: rename (#594)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com></nowiki>}}'; 

/* <pre> */

"use strict";(()=>{const t={src:"data-src",srcset:"data-srcset",selector:".lazyload",root:null,rootMargin:"0px",threshold:0},extend=(t,...s)=>{const o={},r="boolean"==typeof t&&t,i=[..."boolean"!=typeof t?[t]:[],...s],merge=t=>{for(const s in t)Object.prototype.hasOwnProperty.bind(t)(s)&&(r&&"[object Object]"===Object.prototype.toString.bind(t[s])()?o[s]=extend(!0,o[s],t[s]):o[s]=t[s])};for(const t of i)merge(t);return o};class LazyLoad{constructor(s,o){this.settings=extend(t,o||{}),this.images=s||document.querySelectorAll(this.settings.selector),this.observer=null,this.init()}init(){if(!window.IntersectionObserver)return void this.loadImages();const t=this,s={root:this.settings.root,rootMargin:this.settings.rootMargin,threshold:[this.settings.threshold]};this.observer=new IntersectionObserver(s=>{Array.prototype.forEach.bind(s)(s=>{if(s.isIntersecting){t.observer.unobserve(s.target);const o=s.target.getAttribute(t.settings.src),r=s.target.getAttribute(t.settings.srcset);"img"===s.target.tagName.toLowerCase()?(o&&(s.target.src=o),r&&(s.target.srcset=r)):s.target.style.backgroundImage=`url(${o})`}})},s),Array.prototype.forEach.bind(this.images)(s=>{t.observer.observe(s)})}loadAndDestroy(){this.settings&&(this.loadImages(),this.destroy())}loadImages(){if(!this.settings)return;const t=this;Array.prototype.forEach.bind(this.images)(s=>{const o=s.getAttribute(t.settings.src),r=s.getAttribute(t.settings.srcset);"img"===s.tagName.toLowerCase()?(o&&(s.src=o),r&&(s.srcset=r)):s.style.backgroundImage=`url('${o}')`})}destroy(){this.settings&&(this.observer.disconnect(),this.settings=null)}}window.lazyload=(t,s)=>new LazyLoad(t,s),window.jQuery&&(jQuery.fn.lazyload=function(t){const s=t||{};return s.attribute||(s.attribute="data-src"),new LazyLoad(this.toArray(),s),this})})(); 

/* </pre> */