LazyLoading
Instruct browsers to fetch pictures when they are about to appear on the screen.
Description
Pictures files have usually much larger filesizes than hypertext, and a web page with many pictures may take a long time to load. This can be both inconvenient to visitors, and can downgrade the page rating in search engines.
"Lazy loading" is a technique where browsers make a request for a picture only when it is already on the screen or about to scroll into view. Pictures "below the fold" or in collapsed sections are not fetched from the server until they are about to become visible.
In the past, this technique required a placeholder image and a JavaScript function that would track the scroll position of every picture in the page relative to the viewport, and would rewrite the image src
attribute when needed.
Recent browsers support this feature natively with a new HTML5 loading
attribute. This recipe makes use on the native feature.
This page has the feature enabled and many pictures at the bottom. Open your Web developer tools (F12) and scroll down slowly to observe the delayed fetch requests.
Configuration
Add to local/config.php:
## core images $ImgTagFmt= '<img src="$LinkUrl" alt="$LinkAlt" title="$LinkAlt" loading="lazy" />';
If you use Cookbook:Mini, to make thumbnails load when in viewport:
## Lazy Cookbook.Mini thumbnails $Mini['ImgFmt'] = '<img class="mini" src="%1$s" title="%2$s" alt="%2$s" border="0" loading="lazy" />';
If you use Cookbook:ThumbList, to make thumbnails load when in viewport:
## Lazy Cookbook.ThumbList thumbnails $ThumbList['_tmpl']['imgwrap'] = '<img class="thumbs" src="?u" title="%1$s" alt="%1$s" %2$s border="0" loading="lazy" />';
Usage
There should be no change to your workflows.
Notes
To see which browsers natively support the feature, check https://caniuse.com/loading-lazy-attr.
If you have custom markup or other recipe producing embedded images, you may want to update the HTML format for the <img/>
tag in a similar manner.
To do / some day / maybe
Maybe include a polyfill for old browsers.
Change log / Release notes
- 20220323 Recipe published, ready to be tested.
See also
Contributors
Recipe written and maintained by Petko.
Comments
See discussion at LazyLoading-Talk?
Sandbox
These are screenshots from Skins:SkinsGallery.
Collapsed section, click to open
![ABitModern ABitModern](/pmwiki/uploads/Cookbook/abitmodern.jpg)
![Academian Academian](/pmwiki/uploads/Cookbook/academianskin.png)
![Adapt Adapt](/pmwiki/uploads/Cookbook/Adapt_Skin_SiteView.jpg)
![Alalike Alalike](/pmwiki/uploads/Cookbook/alalike.jpg)
![Amber Amber](/pmwiki/uploads/Cookbook/amber.jpg)
![Barthelme Barthelme](/pmwiki/uploads/Cookbook/barthelme.jpg)
![BeeblebroxNetGila BeeblebroxNetGila](/pmwiki/uploads/Cookbook/BeebleBroxNetGila.jpg)
![BitterLemon BitterLemon](/pmwiki/uploads/Cookbook/bitterlemonskin.jpg)
![Blix Blix](/pmwiki/uploads/Cookbook/blix_screenshot_medium.jpg)
![Blog Blog](/pmwiki/uploads/Cookbook/BlogSkin.gif)
![BlueBerry BlueBerry](/pmwiki/uploads/Cookbook/blueberry.jpg)
![Blue Freedom Blue Freedom](/pmwiki/uploads/Cookbook/scrbfreedom.png)
![BlueFur BlueFur](/pmwiki/uploads/Cookbook/bluefur.jpg)
![BlueHead BlueHead](/pmwiki/uploads/Cookbook/blueheadskin.jpg)
![Blues Blues](/pmwiki/uploads/Cookbook/blues.jpg)
![Boira Boira](/pmwiki/uploads/Cookbook/boira-screenshot01.jpg)
![Bonny Bonny](/pmwiki/uploads/Cookbook/bonny.jpg)
![Bs-001 Bs-001](/pmwiki/uploads/Cookbook/bs-001.jpg)
![Bs-002 Bs-002](/pmwiki/uploads/Cookbook/bs-002.png)
![Choice Choice](/pmwiki/uploads/Cookbook/choice_screenshot_thumbnail.jpg)
![Cinnamon Cinnamon](/pmwiki/uploads/Cookbook/cinnamonskin.jpg)
![Colorimetry Colorimetry](/pmwiki/uploads/Cookbook/colorimetry_screenshot_thumbnail.jpg)
![DropDown DropDown](/pmwiki/uploads/Cookbook/DropDown.jpg)
![Drop Shadow Drop Shadow](/pmwiki/uploads/Cookbook/dropshadow_screenshot_thumbnail.jpg)
![Enlighten Enlighten](/pmwiki/uploads/Cookbook/enlighten_screenshot_thumbnail.jpg)
![Equilibrium Equilibrium](/pmwiki/uploads/Cookbook/equilibrium_screenshot_thumbnail.jpg)
![Evolver Evolver](/pmwiki/uploads/Cookbook/evolver.jpg)
![FixFlow FixFlow](/pmwiki/uploads/Cookbook/FixFlow.jpg)
![Flexi Flexi](/pmwiki/uploads/Cookbook/flexi.jpg)
![Flckr Flckr](/pmwiki/uploads/Cookbook/flckrskin.jpg)
![Gemini Gemini](/pmwiki/uploads/Cookbook/GeminiTwo.jpg)
![GlossyHue GlossyHue](/pmwiki/uploads/Cookbook/glossyhue_screenshot_medium.jpg)
![Grayness Grayness](/pmwiki/uploads/Cookbook/grayness.jpg)
![Grease Grease](/pmwiki/uploads/Cookbook/grease.jpg)
![Green Green](/pmwiki/uploads/Cookbook/greenskin.jpg)
![IPMWiki IPMWiki](/pmwiki/uploads/Cookbook/ipmwikiskin.jpg)
![JHMP JHMP](/pmwiki/uploads/Cookbook/jhmp.jpg)
![JH JH](/pmwiki/uploads/Cookbook/JHSkin.jpg)
![Kaylen Kaylen](/pmwiki/uploads/Cookbook/scr-kaylen.png)
![Lean Lean](/pmwiki/uploads/Cookbook/LeanSkin.gif)
![Lens Lens](/pmwiki/uploads/Cookbook/lens.jpg)
![Light Light](/pmwiki/uploads/Cookbook/LightSkin.gif)
![Lines Lines](/pmwiki/uploads/Cookbook/LinesSkin.jpg)
![Maguila Maguila](/pmwiki/uploads/Cookbook/maguila_screenshot_thumb.png)
![Marathon Marathon](/pmwiki/uploads/Cookbook/Marathon.jpg)
![Marble Marble](/pmwiki/uploads/Cookbook/marble.jpg)
![Marinee Marinee](/pmwiki/uploads/Cookbook/marinee_screenshot_medium.jpg)
![Minimous Minimous](/pmwiki/uploads/Cookbook/minimous_screenshot_medium.jpg)
![Monobook Monobook](/pmwiki/uploads/Cookbook/monobook.jpg)
![Neat Neat](/pmwiki/uploads/Cookbook/neat.jpg)
![Netstreams Netstreams](/pmwiki/uploads/Cookbook/netstream.jpg)
![Neutral Neutral](/pmwiki/uploads/Cookbook/neutralskin.jpg)
![NewsPaper NewsPaper](/pmwiki/uploads/Cookbook/newspaper.jpg)
![Night Night](/pmwiki/uploads/Cookbook/Night.jpg)
![NotSoSimple NotSoSimple](/pmwiki/uploads/Cookbook/not_so_simple.jpg)
![Papyrus Papyrus](/pmwiki/uploads/Cookbook/papyrus_small.png)
![Parchment Parchment](/pmwiki/uploads/Cookbook/parchment.jpg)
![PhotoGallery PhotoGallery](/pmwiki/uploads/Cookbook/photogallery_screenshot_thumbnail.jpg)
![PhpNet PhpNet](/pmwiki/uploads/Cookbook/PhpNet.jpg)
![Plain Plain](/pmwiki/uploads/Cookbook/PlainSkin.gif)
![PmWiki2Bars PmWiki2Bars](/pmwiki/uploads/Cookbook/pmwiki2bars.jpg)
![PmWikiV1 PmWikiV1](/pmwiki/uploads/Cookbook/pmwikiv1.jpg)
![PukkaFloat PukkaFloat](/pmwiki/uploads/Cookbook/pukkafloat.jpg)
![Recurve Recurve](/pmwiki/uploads/Cookbook/Recurve.jpg)
![RoseTrellis RoseTrellis](/pmwiki/uploads/Cookbook/rosetrellis.jpg)
![Rounded Rounded](/pmwiki/uploads/Cookbook/rounded.jpg)
![Royale Royale](/pmwiki/uploads/Cookbook/royaleskin.png)
![SchlaeferTwo SchlaeferTwo](/pmwiki/uploads/Cookbook/SchlaeferTwo.jpg)
![Simpla Simpla](/pmwiki/uploads/Cookbook/simpla.jpg)
![Simple Simple](/pmwiki/uploads/Cookbook/Simple-2.jpg)
![SimpleTab SimpleTab](/pmwiki/uploads/Cookbook/simpletab.jpg)
![Sinorca Sinorca](/pmwiki/uploads/Cookbook/sinorca.jpg)
![Skidoo Skidoo](/pmwiki/uploads/Cookbook/skidoo_screenshot_medium.gif)
![Skittlish Skittlish](/pmwiki/uploads/Cookbook/skittlish_screenshot_medium.gif)
![Soma Soma](/pmwiki/uploads/Cookbook/soma.gif)
![SomethingCorporate SomethingCorporate](/pmwiki/uploads/Cookbook/something_corporate.jpg)
![SsofbJoomlaRhuk skin for PmWiki SsofbJoomlaRhuk skin for PmWiki](/pmwiki/uploads/Cookbook/ssofb.co.uk_joomla_rhuk.png)
![Steamport Steamport](/pmwiki/uploads/Cookbook/Steamport.jpg)
![Stripped Stripped](/pmwiki/uploads/Cookbook/stripped.jpg)
![Technobabble Technobabble](/pmwiki/uploads/Cookbook/technobabble.jpg)
![TextPattern TextPattern](/pmwiki/uploads/Cookbook/textpattern.jpg)
![Triad Triad](/pmwiki/uploads/Cookbook/triad.jpg)
![Trish Trish](/pmwiki/uploads/Cookbook/scr-trish.png)
![UselessTuesday UselessTuesday](/pmwiki/uploads/Cookbook/uselesstuesday.jpg)
![WikiLove WikiLove](/pmwiki/uploads/Cookbook/wikilove.jpg)
User notes +1: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.