{"id":6157,"date":"2021-04-12T15:29:00","date_gmt":"2021-04-12T15:29:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=6157"},"modified":"2021-04-07T15:35:25","modified_gmt":"2021-04-07T15:35:25","slug":"delay-the-loading-of-anything-with-defer-js","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/delay-the-loading-of-anything-with-defer-js\/","title":{"rendered":"Delay The Loading Of Anything With Defer.js"},"content":{"rendered":"\n<p>Defer.js is a small and lightweight JavaScript library that delays the loading of web resources to improve page speed.<\/p>\n\n\n\n<p>Supports JavaScript, CSS, Images, Iframes, Video, Audios, and any DOM you can imagine.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2>How to use it:<\/h2>\n\n\n\n<p>1. Load the minified version of the defer.js library in the document.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src=\"defer.min.js\"&gt;&lt;\/script&gt;<\/pre>\n\n\n\n<p>2. Defer the loading of JavaScript within the document. In this example, the target script will wait for 3 seconds to load after the page is fully loaded. The second parameter is used to append a unique ID to the script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Defer.js(src, id, delay, callback)\nDefer.js('target.js', 'target-id', 2000, function() {\n  \/\/ do something\n});\n\n\/\/ Or\n&lt;script type=\"deferjs\" src=\"1.js\"&gt;&lt;\/script&gt;<\/pre>\n\n\n\n<p>3. Defer the loading of CSS within the document.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Defer.css(src, id, delay, callback)\nDefer.css('target.css', 'target-id', 2000);<\/pre>\n\n\n\n<p>4. Defer the loading of any DOM elements (like images, iframes, videos, audios, etc) within the document.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Defer.dom(selector, delay, cssclass, validate, observeOptions)\n\n\/\/ lazy load images with the CSS class of lazy\n\/\/ and add the loaded to those images when they're finished loading\n\/\/ place images in the data-src attribute\n&lt;img class=\"lazy\" data-src=\"1.jpg\" \/&gt;\nDefer.dom('img.lazy', 200, 'loaded');\n\n\/\/ lazy load containers with the CSS class of container\nDefer.dom('.container');\n&lt;div id=\"container\" data-style=\"background: url(1.jpg)\"&gt;\n&lt;\/div&gt;\n\n\/\/ lazy load iframes, pictures, videos, audios...\n&lt;iframe class=\"multi-lazy\" title=\"Youtube\"\n  width=\"400\" height=\"300\" allowfullscreen\n  allow=\"accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture\"\n  data-style=\"background: url(https:\/\/img.youtube.com\/vi\/Uz970DggW7E\/hqdefault.jpg) 50% 50% \/ cover no-repeat;\"\n  data-src=\"https:\/\/www.youtube.com\/embed\/Uz970DggW7E\"&gt;\n&lt;\/iframe&gt;\n&lt;picture class=\"multi-lazy\"&gt;\n  &lt;source media=\"(min-width:800px)\" data-srcset=\"large.jpg\"&gt;\n  &lt;source media=\"(min-width:600px)\" data-srcset=\"medium.jpg\"&gt;\n  &lt;img data-src=\"small.jpg\" alt=\"Photo\" style=\"width:auto;\"&gt;\n&lt;\/picture&gt;\n&lt;audio class=\"multi-lazy\" controls&gt;\n  &lt;source data-src=\"1.ogg\" type=\"audio\/ogg\"&gt;\n  &lt;source data-src=\"1.mp3\" type=\"audio\/mpeg\"&gt;\n  Your browser does not support the audio tag.\n&lt;\/audio&gt;\n&lt;video class=\"multi-lazy\" width=\"320\" height=\"240\" controls&gt;\n  &lt;source data-src=\"m1.mp4\" type=\"video\/mp4\"&gt;\n  &lt;source data-src=\"1.ogg\" type=\"video\/ogg\"&gt;\n  Your browser does not support the video tag.\n&lt;\/video&gt;<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Author<\/strong>: shinsenter<\/p><p><strong>License<\/strong>: MIT<\/p><p><strong>Official Page<\/strong>: <a href=\"https:\/\/github.com\/shinsenter\/defer.js\">GitHub<\/a><\/p><p><strong>Demo<\/strong>: <a href=\"https:\/\/appseeds.net\/defer.js\/demo.html\">Link<\/a><\/p><p><strong>Download<\/strong>: <a href=\"https:\/\/github.com\/shinsenter\/defer.js\/archive\/refs\/heads\/master.zip\">Link<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Defer.js is a small and lightweight JavaScript library that delays the loading of web resources to improve page speed. Supports JavaScript, CSS, Images, Iframes, Video, Audios, and any DOM you can imagine.<\/p>\n","protected":false},"author":2,"featured_media":6158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,12],"tags":[25,146,87,90],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2021\/04\/Delay-The-Loading-Of-Anything-With-Defer.js.jpg","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1Bj","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/6157"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=6157"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/6157\/revisions"}],"predecessor-version":[{"id":6159,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/6157\/revisions\/6159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/6158"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=6157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=6157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=6157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}