[TASK] progressive loaded images
This commit is contained in:
parent
2cd70552b0
commit
03ebd97cd0
@ -17,6 +17,64 @@ main {
|
|||||||
flex: 1 0 auto;
|
flex: 1 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.progressive {
|
||||||
|
position: relative;
|
||||||
|
display: block;
|
||||||
|
overflow: hidden;
|
||||||
|
outline: none
|
||||||
|
}
|
||||||
|
|
||||||
|
.progressive img {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
max-width: none;
|
||||||
|
height: auto;
|
||||||
|
border: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
.progressive img.preview {
|
||||||
|
filter: blur(2vw);
|
||||||
|
transform: scale(1.05)
|
||||||
|
}
|
||||||
|
|
||||||
|
.progressive img.reveal {
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
top: 0;
|
||||||
|
will-change: transform, opacity;
|
||||||
|
animation: progressiveReveal 1s ease-out
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes progressiveReveal {
|
||||||
|
0% {
|
||||||
|
transform: scale(1.05);
|
||||||
|
filter: blur(20px);
|
||||||
|
opacity: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
transform: scale(1);
|
||||||
|
filter: none;
|
||||||
|
opacity: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
picture {
|
||||||
|
width: 100%;
|
||||||
|
/* stretch to fit its containing element */
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
picture img {
|
||||||
|
width: 100%;
|
||||||
|
/* stretch to fill the picture element */
|
||||||
|
transition: filter .5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
picture.lazy img {
|
||||||
|
filter: blur(20px);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
header,
|
header,
|
||||||
main,
|
main,
|
||||||
|
|||||||
@ -1,37 +1,50 @@
|
|||||||
M.AutoInit();
|
M.AutoInit();
|
||||||
|
|
||||||
// Set up an intersection observer with some options
|
window.addEventListener && window.requestAnimationFrame && document.getElementsByClassName && window.addEventListener('load', function () {
|
||||||
var observer = new IntersectionObserver(lazyLoad, {
|
'use strict';
|
||||||
|
var e, t, n = document.getElementsByClassName('progressive replace');
|
||||||
|
|
||||||
// where in relation to the edge of the viewport, we are observing
|
function i() {
|
||||||
rootMargin: "100px",
|
t = t || setTimeout(function () {
|
||||||
|
t = null, r()
|
||||||
|
}, 300)
|
||||||
|
}
|
||||||
|
|
||||||
// how much of the element needs to have intersected
|
function r() {
|
||||||
// in order to fire our loading function
|
n.length && requestAnimationFrame(function () {
|
||||||
threshold: 1.0
|
for (var t, i, r = window.innerHeight, a = 0; a < n.length;) 0 < (i = (t = n[a].getBoundingClientRect()).top) + t.height && r > i ? (s(n[a]), n[a].classList.remove('replace')) : a++;
|
||||||
|
e = n.length
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
});
|
function s(e, t) {
|
||||||
|
var n = e && (e.getAttribute('data-href') || e.href);
|
||||||
function lazyLoad(elements) {
|
if (n) {
|
||||||
elements.forEach(image => {
|
var i = new Image,
|
||||||
if (image.intersectionRatio > 0) {
|
r = e.dataset;
|
||||||
|
r && (r.srcset && (i.srcset = r.srcset), r.sizes && (i.sizes = r.sizes)), i.onload = function () {
|
||||||
// set the src attribute to trigger a load
|
requestAnimationFrame(function () {
|
||||||
image.src = image.dataset.src;
|
n === e.href && (e.style.cursor = 'default', e.addEventListener('click', function (e) {
|
||||||
|
e.preventDefault()
|
||||||
// stop observing this element. Our work here is done!
|
}, !1));
|
||||||
observer.unobserve(item.target);
|
var t = e.querySelector && e.querySelector('img.preview');
|
||||||
};
|
e.insertBefore(i, t && t.nextSibling).addEventListener('animationend', function () {
|
||||||
});
|
t && (t.alt && (i.alt = t.alt), e.removeChild(t)), i.classList.remove('reveal')
|
||||||
};
|
})
|
||||||
|
})
|
||||||
// If the browser supports lazy loading, we can safely assign the src
|
}, (t = 1 + (t || 0)) < 3 && (i.onerror = function () {
|
||||||
// attributes without instantly triggering an eager image load.
|
setTimeout(function () {
|
||||||
if ("loading" in HTMLImageElement.prototype) {
|
s(e, t)
|
||||||
const lazyImages = document.querySelectorAll("img.lazy");
|
}, 3e3 * t)
|
||||||
lazyImages.forEach(img => {
|
}), i.className = 'reveal', i.src = n
|
||||||
img.src = img.dataset.src;
|
}
|
||||||
});
|
}
|
||||||
} else {
|
window.addEventListener('scroll', i, !1), window.addEventListener('resize', i, !1), MutationObserver && new MutationObserver(function () {
|
||||||
// Use our own lazyLoading with Intersection Observers and all that jazz
|
n.length !== e && r()
|
||||||
}
|
}).observe(document.body, {
|
||||||
|
subtree: !0,
|
||||||
|
childList: !0,
|
||||||
|
attributes: !0,
|
||||||
|
characterData: !0
|
||||||
|
}), r()
|
||||||
|
}, !1);
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
{% import "partials/macros.twig" as macros %}
|
||||||
|
|
||||||
{% block images %}
|
{% block images %}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{% if images | length > 1 %}
|
{% if images | length > 1 %}
|
||||||
@ -5,19 +7,13 @@
|
|||||||
{% for image in images %}
|
{% for image in images %}
|
||||||
<a href="{{ image.url }}"
|
<a href="{{ image.url }}"
|
||||||
class="carousel-item">
|
class="carousel-item">
|
||||||
<img class="lazy"
|
{{ macros.variants(image) }}
|
||||||
loading="lazy"
|
|
||||||
data-src="{{ image.url }}"
|
|
||||||
alt="{{ image.alt }}">
|
|
||||||
</a>
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<img data-src="{{ images.0.url }}"
|
{{ macros.variants(images.0, "materialboxed" ) }}
|
||||||
alt="{{ images.0.alt }}"
|
|
||||||
class="materialboxed depth-1 lazy"
|
|
||||||
loading="lazy"
|
|
||||||
{{ images.0.size }}>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
8
Packages/chrosey_materialize/partials/macros.twig
Normal file
8
Packages/chrosey_materialize/partials/macros.twig
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{% macro variants(image,class="") %}
|
||||||
|
<div data-href="{{image.url}}"
|
||||||
|
class="progressive replace {{class}}">
|
||||||
|
<img src="{{ image.mypath }}/thumbs/{{ image.basename }}"
|
||||||
|
alt="{{ image.alt}}"
|
||||||
|
class="preview">
|
||||||
|
</div>
|
||||||
|
{% endmacro %}
|
||||||
Loading…
x
Reference in New Issue
Block a user