250 lines
8.7 KiB
HTML
250 lines
8.7 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head lang="en">
|
|
<meta charset="UTF-8" />
|
|
<title>Conditional native lazy loading - Lazyload demos</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<style>
|
|
ul,
|
|
li {
|
|
list-style-type: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
a,
|
|
img {
|
|
display: block;
|
|
}
|
|
|
|
img {
|
|
border: 0;
|
|
width: 220px;
|
|
height: 280px;
|
|
}
|
|
|
|
img:not([src]) {
|
|
visibility: hidden;
|
|
}
|
|
|
|
.warning {
|
|
padding: 1em;
|
|
color: black;
|
|
background: lightyellow;
|
|
border-radius: 1em;
|
|
border: 1px solid gray;
|
|
}
|
|
|
|
/* Fixes Firefox anomaly during image load */
|
|
@-moz-document url-prefix() {
|
|
img:-moz-loading {
|
|
visibility: hidden;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Conditional native lazy loading demo</h1>
|
|
<h2>Images</h2>
|
|
<div id="results1" class="results">
|
|
<ul>
|
|
<li>
|
|
<a href="#/it/donna/stivaletti_cod44721746jj.html"
|
|
><img
|
|
alt="Stivaletti"
|
|
loading="eager"
|
|
src="https://images.unsplash.com/photo-1555923637-8e28485258ca?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/open-toe_cod44740806jx.html"
|
|
><img
|
|
alt="Open toe"
|
|
loading="eager"
|
|
src="https://images.unsplash.com/photo-1543286386-2e659306cd6c?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44735977gr.html"
|
|
><img
|
|
alt="Sneakers & Tennis"
|
|
class="lazy"
|
|
data-src="https://images.unsplash.com/photo-1554080450-0cf441c6eb68?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44738717am.html"
|
|
><img
|
|
alt="Sneakers & Tennis shoes basse"
|
|
class="lazy"
|
|
data-src="https://images.unsplash.com/photo-1554194666-dd5e2510e694?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/sneakers-tennis-shoes-alte_cod44739940cb.html"
|
|
><img
|
|
alt="Sneakers & Tennis shoes alte"
|
|
class="lazy"
|
|
data-src="https://images.unsplash.com/photo-1555464735-4706a5ef4bf6?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/sneakers-tennis-shoes-alte_cod44740860xg.html"
|
|
><img
|
|
alt="Sneakers & Tennis shoes alte"
|
|
class="lazy"
|
|
data-src="https://images.unsplash.com/photo-1556068791-9e3a08d59470?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
<li>
|
|
<a href="#/it/donna/sneakers-tennis-shoes-basse_cod44738719vn.html"
|
|
><img
|
|
alt="Sneakers & Tennis shoes basse"
|
|
class="lazy"
|
|
data-src="https://images.unsplash.com/photo-1554089672-043830ebaed9?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=440&h=560&fit=crop&ixid=eyJhcHBfaWQiOjF9"
|
|
width="220"
|
|
height="280"
|
|
/></a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<h2>Iframes</h2>
|
|
<iframe class="lazy" data-src="iframes/i01.html" frameborder="0"></iframe>
|
|
<iframe class="lazy" data-src="iframes/i02.html" frameborder="0"></iframe>
|
|
<iframe class="lazy" data-src="iframes/i03.html" frameborder="0"></iframe>
|
|
|
|
<h2>Preload = none</h2>
|
|
<video
|
|
class="lazy"
|
|
controls
|
|
preload="none"
|
|
width="620"
|
|
data-src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
|
|
data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"
|
|
>
|
|
<source
|
|
type="video/mp4"
|
|
data-src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
|
|
/>
|
|
<source type="video/ogg" data-src="https://archive.org/download/ElephantsDream/ed_hd.ogv" />
|
|
<source type="video/avi" data-src="https://archive.org/download/ElephantsDream/ed_hd.avi" />
|
|
</video>
|
|
|
|
<h2>Preload = metadata</h2>
|
|
<video
|
|
class="lazy"
|
|
preload="metadata"
|
|
controls
|
|
width="620"
|
|
data-src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
|
|
data-poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217"
|
|
>
|
|
<source
|
|
type="video/mp4"
|
|
data-src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
|
|
/>
|
|
<source type="video/ogg" data-src="https://archive.org/download/ElephantsDream/ed_hd.ogv" />
|
|
<source type="video/avi" data-src="https://archive.org/download/ElephantsDream/ed_hd.avi" />
|
|
</video>
|
|
<script src="../dist/lazyload.min.js"></script>
|
|
<script>
|
|
(function () {
|
|
function logElementEvent(eventName, element) {
|
|
console.log(Date.now(), eventName, element.getAttribute("data-src"));
|
|
}
|
|
|
|
var callback_enter = function (element) {
|
|
logElementEvent("🔑 ENTERED", element);
|
|
};
|
|
var callback_exit = function (element) {
|
|
logElementEvent("🚪 EXITED", element);
|
|
};
|
|
var callback_loading = function (element) {
|
|
logElementEvent("⌚ LOADING", element);
|
|
};
|
|
var callback_loaded = function (element) {
|
|
logElementEvent("👍 LOADED", element);
|
|
};
|
|
var callback_error = function (element) {
|
|
logElementEvent("💀 ERROR", element);
|
|
element.src = "https://source.unsplash.com/random/440x560/?text=Error+Placeholder";
|
|
};
|
|
var callback_finish = function () {
|
|
logElementEvent("✔️ FINISHED", document.documentElement);
|
|
};
|
|
var callback_cancel = function (element) {
|
|
logElementEvent("🔥 CANCEL", element);
|
|
};
|
|
|
|
function supportsNativeLazyVideo() {
|
|
return "loading" in HTMLVideoElement.prototype;
|
|
}
|
|
|
|
// Single instance using native lazy loading on images, videos and iframes
|
|
function createSingleInstance() {
|
|
var lazyAll = new LazyLoad({
|
|
use_native: true,
|
|
// NOTICE: they won't be called since the loading will be native
|
|
callback_enter: callback_enter,
|
|
callback_exit: callback_exit,
|
|
callback_cancel: callback_cancel,
|
|
callback_loading: callback_loading,
|
|
callback_loaded: callback_loaded,
|
|
callback_error: callback_error,
|
|
callback_finish: callback_finish
|
|
});
|
|
}
|
|
|
|
// Double instance using native lazy loading on images and iframes,
|
|
// and JS driven lazy loading on videos
|
|
function createDoubleInstance() {
|
|
var lazyImgsIframes = new LazyLoad({
|
|
elements_selector: "img.lazy, iframe.lazy",
|
|
use_native: true,
|
|
// Assign the callbacks defined above
|
|
// NOTICE: they won't all called since the loading will be native
|
|
callback_enter: callback_enter,
|
|
callback_exit: callback_exit,
|
|
callback_cancel: callback_cancel,
|
|
callback_loading: callback_loading,
|
|
callback_loaded: callback_loaded,
|
|
callback_error: callback_error,
|
|
callback_finish: callback_finish
|
|
});
|
|
var lazyVideos = new LazyLoad({
|
|
elements_selector: "video.lazy",
|
|
// Assign the callbacks defined above
|
|
callback_enter: callback_enter,
|
|
callback_exit: callback_exit,
|
|
callback_cancel: callback_cancel,
|
|
callback_loading: callback_loading,
|
|
callback_loaded: callback_loaded,
|
|
callback_error: callback_error,
|
|
callback_finish: callback_finish
|
|
});
|
|
}
|
|
|
|
if (supportsNativeLazyVideo()) {
|
|
createSingleInstance();
|
|
} else {
|
|
createDoubleInstance();
|
|
}
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|