{"id":8811,"date":"2025-04-22T18:47:42","date_gmt":"2025-04-22T13:17:42","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8811"},"modified":"2025-04-22T18:47:46","modified_gmt":"2025-04-22T13:17:46","slug":"lazyloading-in-javascript","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/lazyloading-in-javascript\/","title":{"rendered":"LAZYLOADING IN JAVASCRIPT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Lazyloading :<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">Lazyloading  is a technique used in web development  to delay the loading of non essential resources such as images and videos or script until they are needed.<br>Lazy loading helps faster Initial Load time ,Improves Performance,Enhances user Experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Loading Attribute :<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example demonstrating lazyloading of images using the loading attribute<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"958\" height=\"116\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM.png\" alt=\"\" class=\"wp-image-8814\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM.png 958w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM-300x36.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM-768x93.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-5.21.58\u202fPM-850x103.png 850w\" sizes=\"(max-width: 958px) 100vw, 958px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">loading attribute allows a  browser to defer loading offscreen image and i frames<br>Delays loading the resources images or iframe until about to enter the viewport.                                       Ideal for below the fold content like galleries , embedded videos  or scroll pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Intersection Observer API :<\/strong><br> Here is an example demonstrating  lazyloading of images using the Intersection Observer API<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM.png\"><img decoding=\"async\" width=\"852\" height=\"224\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM.png\" alt=\"\" class=\"wp-image-8812\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM.png 852w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM-300x79.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM-768x202.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.38\u202fPM-850x223.png 850w\" sizes=\"(max-width: 852px) 100vw, 852px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">data-src  is a custom attribute ,it&#8217;s stores the actual image URL temporarily without loading it right away .<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM.png\"><img decoding=\"async\" width=\"1024\" height=\"479\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM-1024x479.png\" alt=\"\" class=\"wp-image-8813\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM-1024x479.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM-300x140.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM-768x360.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM-641x300.png 641w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-22-at-4.15.46\u202fPM.png 1162w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Explanation :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>const lazyImg =document.querySelector(&#8216;.lazy&#8217;); &#8211;&gt; this should be &#8216;queryselectorall &#8216; if want to select all images .<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>const observer = new IntersectionObserver((entries,observer)=&gt;{} &#8211;&gt; it watches  to see when they come into the viewport.<\/li>\n\n\n\n<li>entries.forEach(entry =&gt;{}  &#8211;&gt; Loops through every observed element image that changed visibility <\/li>\n\n\n\n<li>if(entry.isIntersecting){} &#8211;&gt; This checks if the image is visible on the screen , only then do we load it <\/li>\n\n\n\n<li>const img = entry.target &#8211;&gt; get the image element <\/li>\n\n\n\n<li>img.src=img.dataset.src &#8211;&gt;  Image starts loading <\/li>\n\n\n\n<li>img.classList.remove(&#8216;lazy&#8217;) &#8211;&gt; Remove the lazy class <\/li>\n\n\n\n<li>observer.unobserve(img) &#8211;&gt; stop observing this image since it&#8217;s already loaded.<\/li>\n\n\n\n<li>lazyImg.forEach(img=&gt;observer.observe(img)) &#8211;&gt; whenever they appear on screen  the observer will trigger load them.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Benefits of lazyloading :<\/strong>                                                                                                                                     Faster page load                                                                                                                                               improves performance &amp; scrolling                                                                                                                   Saves user bandwidth                                                                                                                                        Reduces the server load                                                                                                                                       Load&#8217;s  only what&#8217;s needed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lazyloading is used image galleries , e-commerce product lists ,embedded videos ,infinite scroll pages, mobile apps,long landing pages and dynamic content loading .<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lazyloading : Lazyloading is a technique used in web development to delay the loading of non essential resources such as [&hellip;]<\/p>\n","protected":false},"author":520,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8811","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2i7","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/520"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8811"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8811\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}