diff --git a/.build/.beautify b/.build/.beautify new file mode 100644 index 0000000..5c3dfd9 --- /dev/null +++ b/.build/.beautify @@ -0,0 +1,28 @@ +{ + "brace_style": "collapse", + "break_chained_methods": false, + "comma_first": false, + "e4x": true, + "editorconfig": true, + "end_with_newline": true, + "eol": "\n", + "indent_char": " ", + "indent_inner_html": true, + "indent_level": 0, + "indent_scripts": "separate", + "indent_size": "2", + "indent_with_tabs": false, + "jslint_happy": true, + "keep_array_indentation": true, + "max_preserve_newlines": "2", + "operator_position": "before-newline", + "preserve_newlines": true, + "space_after_anon_function": false, + "space_after_named_function": false, + "space_before_conditional": true, + "space_in_empty_paren": false, + "space_in_paren": false, + "unescape_strings": true, + "unindent_chained_methods": false, + "wrap_line_length": "120" +} diff --git a/.build/.eslintrc b/.build/.eslintrc new file mode 100644 index 0000000..af23171 --- /dev/null +++ b/.build/.eslintrc @@ -0,0 +1,256 @@ +{ + "env": { + "browser": true, + "es6": false + }, + "extends": "eslint:recommended", + "globals": { + "Atomics": "readonly", + "SharedArrayBuffer": "readonly" + }, + "parserOptions": { + "ecmaVersion": 3 + }, + "rules": { + "accessor-pairs": "error", + "array-bracket-newline": "error", + "array-bracket-spacing": "error", + "array-callback-return": "error", + "array-element-newline": "off", + "arrow-body-style": "error", + "arrow-parens": "error", + "arrow-spacing": "error", + "block-scoped-var": "error", + "block-spacing": [ + "error", + "never" + ], + "callback-return": "error", + "camelcase": "off", + "capitalized-comments": "off", + "class-methods-use-this": "error", + "comma-dangle": "error", + "comma-spacing": "off", + "comma-style": "error", + "complexity": "error", + "computed-property-spacing": [ + "error", + "never" + ], + "consistent-return": "error", + "consistent-this": "off", + "curly": "error", + "default-case": "error", + "dot-location": "error", + "dot-notation": "error", + "eol-last": "off", + "eqeqeq": "off", + "func-call-spacing": "error", + "func-name-matching": "error", + "func-names": "off", + "func-style": "off", + "function-paren-newline": "error", + "generator-star-spacing": "error", + "global-require": "error", + "guard-for-in": "error", + "handle-callback-err": "error", + "id-blacklist": "error", + "id-length": "error", + "id-match": "error", + "implicit-arrow-linebreak": "error", + "indent": "error", + "indent-legacy": "error", + "init-declarations": "off", + "jsx-quotes": "error", + "key-spacing": "error", + "line-comment-position": "error", + "linebreak-style": [ + "error", + "unix" + ], + "lines-around-comment": "error", + "lines-around-directive": "error", + "lines-between-class-members": "error", + "max-classes-per-file": "error", + "max-depth": "error", + "max-len": "off", + "max-lines": "off", + "max-lines-per-function": "error", + "max-nested-callbacks": "error", + "max-params": "off", + "max-statements": "off", + "max-statements-per-line": "off", + "multiline-comment-style": "off", + "new-cap": "error", + "new-parens": "error", + "newline-after-var": "off", + "newline-before-return": "error", + "newline-per-chained-call": "error", + "no-alert": "error", + "no-array-constructor": "error", + "no-async-promise-executor": "error", + "no-await-in-loop": "error", + "no-bitwise": "error", + "no-buffer-constructor": "error", + "no-caller": "error", + "no-catch-shadow": "error", + "no-confusing-arrow": "error", + "no-continue": "error", + "no-console": "off", + "no-div-regex": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-empty-function": "off", + "no-eq-null": "error", + "no-eval": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-label": "error", + "no-extra-parens": "error", + "no-floating-decimal": "error", + "no-implicit-coercion": "error", + "no-implicit-globals": "error", + "no-implied-eval": "error", + "no-inline-comments": "error", + "no-invalid-this": "error", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "error", + "no-loop-func": "error", + "no-magic-numbers": "off", + "no-misleading-character-class": "error", + "no-mixed-operators": "off", + "no-mixed-requires": "error", + "no-multi-assign": "off", + "no-multi-spaces": "off", + "no-multi-str": "error", + "no-multiple-empty-lines": "error", + "no-native-reassign": "error", + "no-negated-condition": "error", + "no-negated-in-lhs": "error", + "no-nested-ternary": "error", + "no-new": "error", + "no-new-func": "off", + "no-new-object": "error", + "no-new-require": "error", + "no-new-wrappers": "error", + "no-octal-escape": "error", + "no-param-reassign": "off", + "no-path-concat": "error", + "no-plusplus": "off", + "no-process-env": "error", + "no-process-exit": "error", + "no-proto": "error", + "no-prototype-builtins": "error", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-properties": "error", + "no-restricted-syntax": "error", + "no-return-assign": "error", + "no-return-await": "error", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "off", + "no-shadow": "off", + "no-shadow-restricted-names": "error", + "no-spaced-func": "error", + "no-sync": "error", + "no-tabs": "error", + "no-template-curly-in-string": "error", + "no-ternary": "off", + "no-throw-literal": "error", + "no-trailing-spaces": "error", + "no-undef-init": "error", + "no-undefined": "error", + "no-underscore-dangle": "off", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "error", + "no-unused-expressions": "off", + "no-use-before-define": "error", + "no-useless-call": "error", + "no-useless-catch": "error", + "no-useless-computed-key": "error", + "no-useless-concat": "error", + "no-useless-constructor": "error", + "no-useless-rename": "error", + "no-useless-return": "error", + "no-var": "off", + "no-void": "error", + "no-warning-comments": "error", + "no-whitespace-before-property": "error", + "no-with": "error", + "nonblock-statement-body-position": "error", + "object-curly-newline": "error", + "object-curly-spacing": "error", + "object-property-newline": "error", + "object-shorthand": "error", + "one-var": "off", + "one-var-declaration-per-line": "error", + "operator-assignment": "error", + "operator-linebreak": "error", + "padded-blocks": "off", + "padding-line-between-statements": "error", + "prefer-arrow-callback": "off", + "prefer-const": "error", + "prefer-destructuring": "off", + "prefer-numeric-literals": "error", + "prefer-object-spread": "error", + "prefer-promise-reject-errors": "error", + "prefer-reflect": "off", + "prefer-rest-params": "off", + "prefer-spread": "error", + "prefer-template": "off", + "quote-props": "error", + "quotes": [ + "error", + "single" + ], + "radix": "error", + "require-atomic-updates": "error", + "require-await": "error", + "require-jsdoc": "off", + "require-unicode-regexp": "off", + "rest-spread-spacing": "error", + "semi": "off", + "semi-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "semi-style": "error", + "sort-imports": "error", + "sort-keys": "error", + "sort-vars": "off", + "space-before-blocks": "error", + "space-in-parens": [ + "error", + "never" + ], + "space-infix-ops": "off", + "space-unary-ops": "error", + "spaced-comment": "off", + "strict": [ + "error", + "never" + ], + "switch-colon-spacing": "error", + "symbol-description": "error", + "template-curly-spacing": "error", + "template-tag-spacing": "error", + "unicode-bom": [ + "error", + "never" + ], + "valid-jsdoc": "error", + "vars-on-top": "error", + "wrap-iife": "off", + "wrap-regex": "error", + "yield-star-spacing": "error", + "yoda": "off" + } +} diff --git a/.build/.uglifyjs b/.build/.uglifyjs new file mode 100644 index 0000000..597e260 --- /dev/null +++ b/.build/.uglifyjs @@ -0,0 +1,33 @@ +{ + "compress": {}, + "mangle": { + "eval": false, + "keep_fnames": false, + "reserved": [], + "toplevel": false + }, + "nameCache": {}, + "output": { + "ascii_only": true, + "beautify": false, + "braces": false, + "indent_level": 0, + "indent_start": 0, + "inline_script": true, + "keep_quoted_props": true, + "max_line_len": false, + "preamble": null, + "preserve_line": false, + "quote_keys": false, + "quote_style": 1, + "semicolons": true, + "shebang": true, + "source_map": null, + "width": 80, + "wrap_iife": false, + "comments": "/^@/", + "ie8": true, + "webkit": true + }, + "wrap": null +} diff --git a/README.md b/README.md index 2590039..8dd1f40 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ 🥇 A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript. [![NPM](https://img.shields.io/npm/l/@shinsenter/defer.js)](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) -[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](#) +[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](https://snyk.io/advisor/npm-package/@shinsenter/defer.js) [![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/shinsenter/defer.js)](https://www.codefactor.io/repository/github/shinsenter/defer.js) * * * @@ -16,7 +16,7 @@ [![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js) - **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js) -- **Version**: 2.2.0 +- **Version**: 2.3.0 - **Author**: Mai Nhut Tan - **Copyright**: 2021 AppSeeds - **License**: [MIT](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) @@ -82,7 +82,7 @@ Add `defer.min.js` from this library into your HTML page, just below the opening `` tag. You may download a ZIP of this library, -or load it from a CND like below example. +or load it from a CDN like below example. ```html @@ -90,7 +90,7 @@ or load it from a CND like below example. My Awesome Page - + @@ -111,7 +111,7 @@ instead of `defer.min.js`. ```html - + @@ -205,13 +205,16 @@ Defer(function() { ### Defer.all([selector]) ⇒ void -By default, this function is triggered automatically. +This function is useful for lazy-loading script tags. All script tags with attribute ` - + + ``` After: ```html - - + + ``` **Example** If you don't want the ` - + + ``` -Note: Please call `Defer.all()` at the bottom of the `` tag. - * * * @@ -338,7 +345,7 @@ you should load `IntersectionObserver` polyfill library right after the `defer.min.js` script tag as following example: ```html - + @@ -375,13 +382,13 @@ Lazy load background image of a `div` tag. ```html @@ -398,13 +405,13 @@ Delay lazy loading `` tags 200ms after the page has completely loaded. Then it will add a CSS class `loaded` to the fully lazy loaded image element. ```html - + -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** Advanced usage: Lazy load with [Intersection observer options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options) @@ -412,16 +419,16 @@ Advanced usage: Lazy load with [Intersection observer options](https://developer ```html -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** We can use CSS class that added to the lazy loaded element @@ -539,6 +546,9 @@ Defer.reveal(node); document.querySelectorAll('.multi-lazy').forEach(function(node) { Defer.reveal(node); }); + +// Or even shorter way +document.querySelectorAll('.multi-lazy').forEach(Defer.reveal); ``` * * * @@ -676,21 +686,21 @@ The DOM Node interface ### PHP library -https://github.com/shinsenter/defer.php/ +[https://github.com/shinsenter/defer.php/](https://github.com/shinsenter/defer.php/) 🚀 A PHP library that focuses on minimizing payload size of HTML document and optimizing processing on the browser when rendering the web page. ### Wordpress plugin -https://github.com/shinsenter/defer-wordpress/ +[https://github.com/shinsenter/defer-wordpress/](https://github.com/shinsenter/defer-wordpress/) ⚡️ A native, blazing fast lazy loader. ✅ Legacy browsers support (IE9+). 💯 SEO friendly. 🧩 Lazy load almost anything. ### Laravel package -https://github.com/shinsenter/defer-laravel/ +[https://github.com/shinsenter/defer-laravel/](https://github.com/shinsenter/defer-laravel/) Under development. diff --git a/build/.beautify b/build/.beautify deleted file mode 100644 index 8bd1a30..0000000 --- a/build/.beautify +++ /dev/null @@ -1,28 +0,0 @@ -{ - "indent_size": "2", - "indent_char": " ", - "indent_with_tabs": false, - "editorconfig": true, - "eol": "\n", - "end_with_newline": true, - "indent_level": 0, - "preserve_newlines": true, - "max_preserve_newlines": "2", - "space_in_paren": false, - "space_in_empty_paren": false, - "jslint_happy": true, - "space_after_anon_function": false, - "space_after_named_function": false, - "brace_style": "collapse", - "unindent_chained_methods": false, - "break_chained_methods": false, - "keep_array_indentation": true, - "unescape_strings": true, - "wrap_line_length": "120", - "e4x": true, - "comma_first": false, - "operator_position": "before-newline", - "indent_scripts": "separate", - "space_before_conditional": true, - "indent_inner_html": true -} diff --git a/build/.eslintrc b/build/.eslintrc deleted file mode 100644 index 8cdee4d..0000000 --- a/build/.eslintrc +++ /dev/null @@ -1,257 +0,0 @@ -{ - "env": { - "browser": true, - "es6": false - }, - "extends": "eslint:recommended", - "globals": { - "Atomics": "readonly", - "SharedArrayBuffer": "readonly" - }, - "parserOptions": { - "ecmaVersion": 3 - }, - "rules": { - "accessor-pairs": "error", - "array-bracket-newline": "error", - "array-bracket-spacing": "error", - "array-callback-return": "error", - "array-element-newline": "off", - "arrow-body-style": "error", - "arrow-parens": "error", - "arrow-spacing": "error", - "block-scoped-var": "error", - "block-spacing": [ - "error", - "never" - ], - "callback-return": "error", - "camelcase": "off", - "capitalized-comments": "off", - "class-methods-use-this": "error", - "comma-dangle": "error", - "comma-spacing": "off", - "comma-style": "error", - "complexity": "error", - "computed-property-spacing": [ - "error", - "never" - ], - "consistent-return": "error", - "consistent-this": "off", - "curly": "error", - "default-case": "error", - "dot-location": "error", - "dot-notation": "error", - "eol-last": "off", - "eqeqeq": "off", - "func-call-spacing": "error", - "func-name-matching": "error", - "func-names": "off", - "func-style": "off", - "function-paren-newline": "error", - "generator-star-spacing": "error", - "global-require": "error", - "guard-for-in": "off", - "handle-callback-err": "error", - "id-blacklist": "error", - "id-length": "off", - "id-match": "error", - "implicit-arrow-linebreak": "error", - "indent": "error", - "indent-legacy": "error", - "init-declarations": "off", - "jsx-quotes": "error", - "key-spacing": "error", - "line-comment-position": "error", - "linebreak-style": [ - "error", - "unix" - ], - "lines-around-comment": "error", - "lines-around-directive": "error", - "lines-between-class-members": "error", - "max-classes-per-file": "error", - "max-depth": "error", - "max-len": "off", - "max-lines": "off", - "max-lines-per-function": "error", - "max-nested-callbacks": "error", - "max-params": "off", - "max-statements": "off", - "max-statements-per-line": "off", - "multiline-comment-style": "off", - "new-cap": "error", - "new-parens": "error", - "newline-after-var": "off", - "newline-before-return": "error", - "newline-per-chained-call": "error", - "no-alert": "error", - "no-array-constructor": "error", - "no-async-promise-executor": "error", - "no-await-in-loop": "error", - "no-bitwise": "error", - "no-buffer-constructor": "error", - "no-caller": "error", - "no-catch-shadow": "error", - "no-confusing-arrow": "error", - "no-continue": "error", - "no-console": "off", - "no-div-regex": "error", - "no-duplicate-imports": "error", - "no-else-return": "error", - "no-empty-function": "off", - "no-eq-null": "error", - "no-eval": "error", - "no-extend-native": "error", - "no-extra-bind": "error", - "no-extra-label": "error", - "no-extra-parens": "error", - "no-floating-decimal": "error", - "no-implicit-coercion": "error", - "no-implicit-globals": "error", - "no-implied-eval": "error", - "no-inline-comments": "error", - "no-invalid-this": "error", - "no-iterator": "error", - "no-label-var": "error", - "no-labels": "error", - "no-lone-blocks": "error", - "no-lonely-if": "error", - "no-loop-func": "error", - "no-magic-numbers": "off", - "no-misleading-character-class": "error", - "no-mixed-operators": "off", - "no-mixed-requires": "error", - "no-multi-assign": "off", - "no-multi-spaces": "off", - "no-multi-str": "error", - "no-multiple-empty-lines": "error", - "no-native-reassign": "error", - "no-negated-condition": "error", - "no-negated-in-lhs": "error", - "no-nested-ternary": "error", - "no-new": "error", - "no-new-func": "off", - "no-new-object": "error", - "no-new-require": "error", - "no-new-wrappers": "error", - "no-octal-escape": "error", - "no-param-reassign": "off", - "no-path-concat": "error", - "no-plusplus": "off", - "no-process-env": "error", - "no-process-exit": "error", - "no-proto": "error", - "no-prototype-builtins": "error", - "no-restricted-globals": "error", - "no-restricted-imports": "error", - "no-restricted-modules": "error", - "no-restricted-properties": "error", - "no-restricted-syntax": "error", - "no-return-assign": "error", - "no-return-await": "error", - "no-script-url": "error", - "no-self-compare": "error", - "no-sequences": "off", - "no-shadow": "off", - "no-shadow-restricted-names": "error", - "no-spaced-func": "error", - "no-sync": "error", - "no-tabs": "error", - "no-template-curly-in-string": "error", - "no-ternary": "off", - "no-throw-literal": "error", - "no-trailing-spaces": "error", - "no-undef-init": "error", - "no-undefined": "error", - "no-underscore-dangle": "off", - "no-unmodified-loop-condition": "error", - "no-unneeded-ternary": "error", - "no-unused-expressions": "off", - "no-use-before-define": "error", - "no-useless-call": "off", - "no-useless-catch": "error", - "no-useless-computed-key": "error", - "no-useless-concat": "error", - "no-useless-constructor": "error", - "no-useless-rename": "error", - "no-useless-return": "error", - "no-var": "off", - "no-void": "error", - "no-warning-comments": "error", - "no-whitespace-before-property": "error", - "no-with": "error", - "nonblock-statement-body-position": "error", - "object-curly-newline": "error", - "object-curly-spacing": "error", - "object-property-newline": "error", - "object-shorthand": "error", - "one-var": "off", - "one-var-declaration-per-line": "error", - "operator-assignment": "error", - "operator-linebreak": "error", - "padded-blocks": "off", - "padding-line-between-statements": "error", - "prefer-arrow-callback": "off", - "prefer-const": "error", - "prefer-destructuring": "off", - "prefer-numeric-literals": "error", - "prefer-object-spread": "error", - "prefer-promise-reject-errors": "error", - "prefer-reflect": "off", - "prefer-rest-params": "off", - "prefer-spread": "error", - "prefer-template": "off", - "quote-props": "off", - "quotes": [ - "error", - "single" - ], - "radix": "error", - "require-atomic-updates": "error", - "require-await": "error", - "require-jsdoc": "off", - "require-unicode-regexp": "off", - "rest-spread-spacing": "error", - "semi": "off", - "semi-spacing": [ - "error", - { - "after": true, - "before": false - } - ], - "semi-style": "error", - "sort-imports": "error", - "sort-keys": "error", - "sort-vars": "off", - "space-before-blocks": "off", - "space-before-function-paren": "off", - "space-in-parens": [ - "error", - "never" - ], - "space-infix-ops": "off", - "space-unary-ops": "error", - "spaced-comment": "off", - "strict": [ - "error", - "never" - ], - "switch-colon-spacing": "error", - "symbol-description": "error", - "template-curly-spacing": "error", - "template-tag-spacing": "error", - "unicode-bom": [ - "error", - "never" - ], - "valid-jsdoc": "error", - "vars-on-top": "error", - "wrap-iife": "off", - "wrap-regex": "error", - "yield-star-spacing": "error", - "yoda": "off" - } -} \ No newline at end of file diff --git a/build/.uglifyjs b/build/.uglifyjs deleted file mode 100644 index c25c47f..0000000 --- a/build/.uglifyjs +++ /dev/null @@ -1,31 +0,0 @@ -{ - "compress": true, - "mangle": { - "eval": false, - "keep_fnames": false, - "reserved": [], - "toplevel": true - }, - "output": { - "ascii_only": true, - "beautify": false, - "braces": false, - "indent_level": 0, - "indent_start": 0, - "inline_script": true, - "keep_quoted_props": true, - "max_line_len": false, - "preamble": null, - "preserve_line": false, - "quote_keys": false, - "quote_style": 1, - "semicolons": true, - "shebang": true, - "source_map": null, - "width": 80, - "wrap_iife": false, - "comments": "/^@/", - "ie8": true, - "webkit": false - } -} \ No newline at end of file diff --git a/docs/about.md b/docs/about.md index 9bda21e..d0b155d 100644 --- a/docs/about.md +++ b/docs/about.md @@ -3,7 +3,7 @@ 🥇 A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript. [![NPM](https://img.shields.io/npm/l/@shinsenter/defer.js)](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) -[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](#) +[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](https://snyk.io/advisor/npm-package/@shinsenter/defer.js) [![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/shinsenter/defer.js)](https://www.codefactor.io/repository/github/shinsenter/defer.js) * * * @@ -16,7 +16,7 @@ [![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js) - **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js) -- **Version**: 2.2.0 +- **Version**: 2.3.0 - **Author**: Mai Nhut Tan - **Copyright**: 2021 AppSeeds - **License**: [MIT](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) @@ -82,7 +82,7 @@ Add `defer.min.js` from this library into your HTML page, just below the opening `` tag. You may download a ZIP of this library, -or load it from a CND like below example. +or load it from a CDN like below example. ```html @@ -90,7 +90,7 @@ or load it from a CND like below example. My Awesome Page - + @@ -111,7 +111,7 @@ instead of `defer.min.js`. ```html - + diff --git a/docs/apis.md b/docs/apis.md index 1d9a48e..e2012d2 100644 --- a/docs/apis.md +++ b/docs/apis.md @@ -67,13 +67,16 @@ Defer(function() { ### Defer.all([selector]) ⇒ void -By default, this function is triggered automatically. +This function is useful for lazy-loading script tags. All script tags with attribute ` - + + ``` After: ```html - - + + ``` **Example** If you don't want the ` - + + ``` -Note: Please call `Defer.all()` at the bottom of the `` tag. - * * * @@ -200,7 +207,7 @@ you should load `IntersectionObserver` polyfill library right after the `defer.min.js` script tag as following example: ```html - + @@ -237,13 +244,13 @@ Lazy load background image of a `div` tag. ```html @@ -260,13 +267,13 @@ Delay lazy loading `` tags 200ms after the page has completely loaded. Then it will add a CSS class `loaded` to the fully lazy loaded image element. ```html - + -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** Advanced usage: Lazy load with [Intersection observer options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options) @@ -274,16 +281,16 @@ Advanced usage: Lazy load with [Intersection observer options](https://developer ```html -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** We can use CSS class that added to the lazy loaded element @@ -401,6 +408,9 @@ Defer.reveal(node); document.querySelectorAll('.multi-lazy').forEach(function(node) { Defer.reveal(node); }); + +// Or even shorter way +document.querySelectorAll('.multi-lazy').forEach(Defer.reveal); ``` * * * diff --git a/docs/index.md b/docs/index.md index 2590039..8dd1f40 100644 --- a/docs/index.md +++ b/docs/index.md @@ -3,7 +3,7 @@ 🥇 A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript. [![NPM](https://img.shields.io/npm/l/@shinsenter/defer.js)](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) -[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](#) +[![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](https://snyk.io/advisor/npm-package/@shinsenter/defer.js) [![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/shinsenter/defer.js)](https://www.codefactor.io/repository/github/shinsenter/defer.js) * * * @@ -16,7 +16,7 @@ [![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js) - **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js) -- **Version**: 2.2.0 +- **Version**: 2.3.0 - **Author**: Mai Nhut Tan - **Copyright**: 2021 AppSeeds - **License**: [MIT](https://raw.githubusercontent.com/shinsenter/defer.js/master/LICENSE) @@ -82,7 +82,7 @@ Add `defer.min.js` from this library into your HTML page, just below the opening `` tag. You may download a ZIP of this library, -or load it from a CND like below example. +or load it from a CDN like below example. ```html @@ -90,7 +90,7 @@ or load it from a CND like below example. My Awesome Page - + @@ -111,7 +111,7 @@ instead of `defer.min.js`. ```html - + @@ -205,13 +205,16 @@ Defer(function() { ### Defer.all([selector]) ⇒ void -By default, this function is triggered automatically. +This function is useful for lazy-loading script tags. All script tags with attribute ` - + + ``` After: ```html - - + + ``` **Example** If you don't want the ` - + + ``` -Note: Please call `Defer.all()` at the bottom of the `` tag. - * * * @@ -338,7 +345,7 @@ you should load `IntersectionObserver` polyfill library right after the `defer.min.js` script tag as following example: ```html - + @@ -375,13 +382,13 @@ Lazy load background image of a `div` tag. ```html @@ -398,13 +405,13 @@ Delay lazy loading `` tags 200ms after the page has completely loaded. Then it will add a CSS class `loaded` to the fully lazy loaded image element. ```html - + -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** Advanced usage: Lazy load with [Intersection observer options](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options) @@ -412,16 +419,16 @@ Advanced usage: Lazy load with [Intersection observer options](https://developer ```html -Photo 1 -Photo 2 -Photo 3 +Photo 1 +Photo 2 +Photo 3 ``` **Example** We can use CSS class that added to the lazy loaded element @@ -539,6 +546,9 @@ Defer.reveal(node); document.querySelectorAll('.multi-lazy').forEach(function(node) { Defer.reveal(node); }); + +// Or even shorter way +document.querySelectorAll('.multi-lazy').forEach(Defer.reveal); ``` * * * @@ -676,21 +686,21 @@ The DOM Node interface ### PHP library -https://github.com/shinsenter/defer.php/ +[https://github.com/shinsenter/defer.php/](https://github.com/shinsenter/defer.php/) 🚀 A PHP library that focuses on minimizing payload size of HTML document and optimizing processing on the browser when rendering the web page. ### Wordpress plugin -https://github.com/shinsenter/defer-wordpress/ +[https://github.com/shinsenter/defer-wordpress/](https://github.com/shinsenter/defer-wordpress/) ⚡️ A native, blazing fast lazy loader. ✅ Legacy browsers support (IE9+). 💯 SEO friendly. 🧩 Lazy load almost anything. ### Laravel package -https://github.com/shinsenter/defer-laravel/ +[https://github.com/shinsenter/defer-laravel/](https://github.com/shinsenter/defer-laravel/) Under development. diff --git a/docs/links.md b/docs/links.md index d6f3497..29f344c 100644 --- a/docs/links.md +++ b/docs/links.md @@ -2,21 +2,21 @@ ### PHP library -https://github.com/shinsenter/defer.php/ +[https://github.com/shinsenter/defer.php/](https://github.com/shinsenter/defer.php/) 🚀 A PHP library that focuses on minimizing payload size of HTML document and optimizing processing on the browser when rendering the web page. ### Wordpress plugin -https://github.com/shinsenter/defer-wordpress/ +[https://github.com/shinsenter/defer-wordpress/](https://github.com/shinsenter/defer-wordpress/) ⚡️ A native, blazing fast lazy loader. ✅ Legacy browsers support (IE9+). 💯 SEO friendly. 🧩 Lazy load almost anything. ### Laravel package -https://github.com/shinsenter/defer-laravel/ +[https://github.com/shinsenter/defer-laravel/](https://github.com/shinsenter/defer-laravel/) Under development. diff --git a/package-lock.json b/package-lock.json index 6686d6d..5592e57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,30 @@ { "name": "@shinsenter/defer.js", - "version": "2.2.0", + "version": "2.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@shinsenter/defer.js", - "version": "2.2.0", + "version": "2.3.0", + "funding": [ + { + "type": "github", + "url": "https://github.com/shinsenter/defer.js/stargazers" + }, + { + "type": "paypal", + "url": "https://www.patreon.com/appseeds" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/shinsenter" + } + ], "license": "MIT", + "workspaces": [ + "dist/" + ], "devDependencies": { "intersection-observer": "latest" } diff --git a/package.json b/package.json index 9bd2fb4..0c1ee76 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "@shinsenter/defer.js", "title": "defer.js", - "version": "2.2.0", - "description": "🥇 A super small, super efficient library that helps you lazy load everything like images, video, audio, iframes as well as stylesheets, and JavaScript.", - "homepage": "https://code.shin.company/defer.js#readme", + "version": "2.3.0", + "description": "🥇 A super small, super efficient library that helps you lazy load almost everything like images, video, audio, iframes as well as stylesheets, and JavaScript.", + "homepage": "https://shinsenter.github.io/defer.js/", + "license": "MIT", "author": { "name": "Mai Nhut Tan", "email": "shin@shin.company", @@ -16,55 +17,73 @@ "bugs": { "url": "https://github.com/shinsenter/defer.js/issues" }, + "funding": [ + { + "type": "github", + "url": "https://github.com/shinsenter/defer.js/stargazers" + }, + { + "type": "paypal", + "url": "https://www.patreon.com/appseeds" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/shinsenter" + } + ], "keywords": [ "deferjs", - "defer.js", - "defer loading", - "lazy loading js", - "lazyloader", + "intersection-observer", + "lazy-audio", + "lazy-css", + "lazy-iframe", + "lazy-image", + "lazy-javascript", + "lazy-js", + "lazy-picture", + "lazy-stylesheet", + "lazy-video", "lazyload", - "image", - "picture", - "audio", - "video", - "iframe", - "srcset", - "script", - "stylesheet", - "img", - "css", - "native", + "lazyloader", "lazysizes", - "scroll reveal", - "scroll monitor", - "unveil js", - "vanillajs", - "micro library", - "nano library", - "intersection observer" + "micro-library", + "microjs", + "scroll-reveal", + "srcset-loader", + "srcset", + "unveil", + "vanilla-lazyload", + "vanilla-lazyloader", + "vanillajs" ], - "license": "MIT", "browser": "dist/defer.min.js", "main": "dist/defer.min.js", "files": [ "dist/" ], + "workspaces": [ + "dist/" + ], "devDependencies": { "intersection-observer": "latest" }, "scripts": { - "test": "npm run lint", - "format": "js-beautify --config build/.beautify -r -f src/*.js", - "lint": "eslint --config build/.eslintrc --ext .js src --fix", - "cleanup": "rm -rf ./node_modules package-lock.json", - "tools": "npm -g install jsdoc-to-markdown eslint js-beautify uglify-js", - "readme": "rm -f ./docs/index.md && jsdoc2md --separators -l html -g grouped ./src/*.js > ./docs/apis.md && cat ./docs/*.md > README.md && cp -p README.md ./docs/index.md", - "pull": "npm run cleanup && npm run tools && npm i && npm up && npm audit fix", - "build": "npm run lint && npm run s1 && npm run s2 && npm run s3 && npm run readme", - "s1": "uglifyjs --config-file build/.uglifyjs -o dist/defer.min.js src/defer.js", - "s2": "uglifyjs --config-file build/.uglifyjs -o dist/defer_plus.min.js src/defer.js src/fallback.js", - "s3": "uglifyjs --config-file build/.uglifyjs -o dist/polyfill.min.js node_modules/intersection-observer/intersection-observer.js", - "versionup": "npm run lint && npm --no-git-tag-version version patch -m 'Ready to publish version %s'", - "upload": "npm run pull && npm run build && npm prune --prod && npm publish --access=public" + "format": "js-beautify --config .build/.beautify -r -f src/*.js", + "uglify": "uglifyjs --config-file .build/.uglifyjs -o", + "s1": "npm run uglify dist/defer.min.js src/defer.js", + "s2": "npm run uglify dist/defer_plus.min.js src/defer.js src/fallback.js", + "s3": "npm run uglify dist/polyfill.min.js node_modules/intersection-observer/intersection-observer.js", + "test": "eslint --config .build/.eslintrc --ext .js src --fix", + "prebuild": "npm run test", + "build": "npm run s1 && npm run s2 && npm run s3", + "postbuild": "npm run docs", + "prepull": "rm -rf ./node_modules package-lock.json", + "pull": "npm -g install jsdoc-to-markdown eslint js-beautify uglify-js && npm i && npm up", + "postpull": "npm audit fix", + "predocs": "rm -f README.md ./docs/index.md ./docs/apis.md", + "docs": "jsdoc2md --separators -l html -g grouped ./src/*.js > ./docs/apis.md", + "postdocs": "cat ./docs/*.md > README.md && cp -p README.md ./docs/index.md", + "preupload": "npm run pull && npm run build", + "upload": "npm publish --access=public" } } diff --git a/src/defer.js b/src/defer.js index 46b6c5e..544ed1e 100644 --- a/src/defer.js +++ b/src/defer.js @@ -31,8 +31,8 @@ /** * 🥇 A super small, super efficient library - * that helps you lazy load everything like images, video, audio, iframes - * as well as stylesheets, and JavaScript. + * that helps you lazy load almost everything + * like images, video, audio, iframes as well as stylesheets, and JavaScript. * * @author Mai Nhut Tan * @copyright 2021 AppSeeds @@ -134,7 +134,7 @@ } } - function _deferAllScript(selector) { + function _deferScriptNodes(selector) { defer(function (_found) { _found = _query(selector || _selectorDeferJs); @@ -245,13 +245,16 @@ }; /** - * By default, this function is triggered automatically. + * This function is useful for lazy-loading script tags. * * All script tags with attribute ` - * + * + * * ``` * After: * ```html - * - * + * + * * ``` * * @example * If you don't want the ` - * + * + * * * * * ``` - * - * Note: Please call `Defer.all()` at the bottom of the `` tag. */ - defer.all = _deferAllScript; + defer.all = _deferScriptNodes; /** * For lazy loading external JavaScript files. @@ -383,7 +390,7 @@ * ```html * - * + * * * * @@ -420,13 +427,13 @@ * ```html * * * * @@ -444,13 +451,13 @@ * Then it will add a CSS class `loaded` to the fully lazy loaded image element. * * ```html - * + * * * * - * Photo 1 - * Photo 2 - * Photo 3 + * Photo 1 + * Photo 2 + * Photo 3 * ``` * * @example @@ -459,16 +466,16 @@ * ```html * * * * - * Photo 1 - * Photo 2 - * Photo 3 + * Photo 1 + * Photo 2 + * Photo 3 * ``` * * @example @@ -587,9 +594,13 @@ if (_IO in window) { _observer = new window[_IO](function (nodes) { nodes[_forEach](function (object, _node) { - if (object.isIntersecting && (_node = object.target)) { - _observer.unobserve(_node); - _present(_node); + if (object.isIntersecting) { + _node = object.target; + + if (_node) { + _observer.unobserve(_node); + _present(_node); + } } }); }, observeOptions); @@ -597,7 +608,7 @@ _observer = false; } - function _loop(node) { + _query(selector || _selectorMedia)[_forEach](function (node) { if (!node[_hasAttribute](_lazied)) { node[_setAttribute](_lazied, ''); @@ -607,9 +618,7 @@ _present(node); } } - } - - _query(selector || _selectorMedia)[_forEach](_loop); + }); } defer(_lazyload, delay); @@ -633,6 +642,9 @@ * document.querySelectorAll('.multi-lazy').forEach(function(node) { * Defer.reveal(node); * }); + * + * // Or even shorter way + * document.querySelectorAll('.multi-lazy').forEach(Defer.reveal); * ``` */ defer.reveal = _reveal; @@ -651,7 +663,7 @@ window[_listen]( 'on' + _pageshow in window ? _pageshow : _load, function () { - for (_deferAllScript(); _queue[0]; _domReady = 1) { + for (_deferScriptNodes(); _queue[0]; _domReady = 1) { caller(_queue[_shift](), _queue[_shift]()); } }