Last active
May 2, 2018 22:06
-
-
Save nolanlawson/70d9bbb5887b540164aea8dfacbc3b23 to your computer and use it in GitHub Desktop.
Lazy loading images demo with IntersectionObserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Lazy image loading demo</title> | |
<style> | |
img { | |
display: block; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
body { | |
max-width: 600px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Lazy image loading demo</h1> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | |
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute | |
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | |
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> | |
<img data-src="amazon-internet_256x256.png" width="256" height="256" /> | |
<img data-src="android-webview-beta_256x256.png" width="256" height="256" /> | |
<img data-src="avant_256x256.png" width="256" height="256" /> | |
<img data-src="beaker_256x256.png" width="256" height="256" /> | |
<img data-src="blisk_256x256.png" width="256" height="256" /> | |
<img data-src="brave_256x256.png" width="256" height="256" /> | |
<img data-src="browser.html_256x256.png" width="256" height="256" /> | |
<img data-src="cent_256x256.png" width="256" height="256" /> | |
<img data-src="chrome_256x256.png" width="256" height="256" /> | |
<img data-src="chrome-beta_256x256.png" width="256" height="256" /> | |
<img data-src="chrome-canary_256x256.png" width="256" height="256" /> | |
<img data-src="chrome-dev_256x256.png" width="256" height="256" /> | |
<img data-src="chromium_256x256.png" width="256" height="256" /> | |
<img data-src="cm_256x256.png" width="256" height="256" /> | |
<img data-src="crusta_256x256.png" width="256" height="256" /> | |
<img data-src="dolphin_256x256.png" width="256" height="256" /> | |
<img data-src="dolphin-zero_256x256.png" width="256" height="256" /> | |
<img data-src="dooble_256x256.png" width="256" height="256" /> | |
<img data-src="edge_256x256.png" width="256" height="256" /> | |
<img data-src="edge-tile_256x256.png" width="256" height="256" /> | |
<img data-src="firefox_256x256.png" width="256" height="256" /> | |
<img data-src="firefox-beta_256x256.png" width="256" height="256" /> | |
<img data-src="firefox-developer-edition_256x256.png" width="256" height="256" /> | |
<img data-src="firefox-nightly_256x256.png" width="256" height="256" /> | |
<img data-src="icab-mobile_256x256.png" width="256" height="256" /> | |
<img data-src="icecat_256x256.png" width="256" height="256" /> | |
<img data-src="jsdom_256x256.png" width="256" height="256" /> | |
<img data-src="lightning_256x256.png" width="256" height="256" /> | |
<img data-src="maxthon_256x256.png" width="256" height="256" /> | |
<img data-src="mihtool_256x256.png" width="256" height="256" /> | |
<img data-src="min_256x256.png" width="256" height="256" /> | |
<img data-src="netsurf_256x256.png" width="256" height="256" /> | |
<img data-src="nw.js_256x256.png" width="256" height="256" /> | |
<img data-src="onion_256x256.png" width="256" height="256" /> | |
<img data-src="opera_256x256.png" width="256" height="256" /> | |
<img data-src="opera-beta_256x256.png" width="256" height="256" /> | |
<img data-src="opera-developer_256x256.png" width="256" height="256" /> | |
<img data-src="opera-mini_256x256.png" width="256" height="256" /> | |
<img data-src="opera-mini-beta_256x256.png" width="256" height="256" /> | |
<img data-src="opera-neon_256x256.png" width="256" height="256" /> | |
<img data-src="opera-touch_256x256.png" width="256" height="256" /> | |
<img data-src="orbitum_256x256.png" width="256" height="256" /> | |
<img data-src="otter_256x256.png" width="256" height="256" /> | |
<img data-src="pale-moon_256x256.png" width="256" height="256" /> | |
<img data-src="polarity_256x256.png" width="256" height="256" /> | |
<img data-src="puffin_256x256.png" width="256" height="256" /> | |
<img data-src="qupzilla_256x256.png" width="256" height="256" /> | |
<img data-src="qutebrowser_256x256.png" width="256" height="256" /> | |
<img data-src="safari_256x256.png" width="256" height="256" /> | |
<img data-src="safari-ios_256x256.png" width="256" height="256" /> | |
<img data-src="safari-technology-preview_256x256.png" width="256" height="256" /> | |
<img data-src="samsung-internet_256x256.png" width="256" height="256" /> | |
<img data-src="samsung-internet-beta_256x256.png" width="256" height="256" /> | |
<img data-src="seamonkey_256x256.png" width="256" height="256" /> | |
<img data-src="silk_256x256.png" width="256" height="256" /> | |
<img data-src="sogou-mobile_256x256.png" width="256" height="256" /> | |
<img data-src="tob_256x256.png" width="256" height="256" /> | |
<img data-src="tor_256x256.png" width="256" height="256" /> | |
<img data-src="uc_256x256.png" width="256" height="256" /> | |
<img data-src="uc-mini_256x256.png" width="256" height="256" /> | |
<img data-src="v8_256x256.png" width="256" height="256" /> | |
<img data-src="v8-ignition_256x256.png" width="256" height="256" /> | |
<img data-src="v8-turbofan_256x256.png" width="256" height="256" /> | |
<img data-src="yandex_256x256.png" width="256" height="256" /> | |
<img data-src="yandex-alpha_256x256.png" width="256" height="256" /> | |
<img data-src="yandex-beta_256x256.png" width="256" height="256" /> | |
<img data-src="yandex-lite_256x256.png" width="256" height="256" /> | |
<script> | |
(() => { | |
let images = document.querySelectorAll('img') | |
let observer = new IntersectionObserver(entries => { | |
for (let entry of entries) { | |
if (entry.isIntersecting) { | |
let src = entry.target.getAttribute('data-src') | |
entry.target.setAttribute('src', src) | |
} | |
} | |
}) | |
for (let image of images) { | |
observer.observe(image) | |
} | |
})() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment