do not display images out of viewport
This commit is contained in:
parent
5732daf32e
commit
b2f2420df9
1 changed files with 16 additions and 6 deletions
|
@ -36,12 +36,17 @@ export default defineComponent({
|
|||
onMounted(() => {
|
||||
const observer = new IntersectionObserver((entries, observer) => {
|
||||
entries.forEach((entry) => {
|
||||
const element = entry.target as HTMLImageElement;
|
||||
if (entry.isIntersecting) {
|
||||
const element = entry.target as HTMLImageElement;
|
||||
element.src = element.dataset.src!;
|
||||
element.onload = () => {
|
||||
element.classList.add("visible");
|
||||
};
|
||||
element.classList.add("visible");
|
||||
if (!element.src) {
|
||||
element.src = element.dataset.src!;
|
||||
element.onload = () => {
|
||||
element.classList.add("loaded");
|
||||
};
|
||||
}
|
||||
} else {
|
||||
element.classList.remove("visible");
|
||||
}
|
||||
});
|
||||
}, {rootMargin: "100px"});
|
||||
|
@ -90,9 +95,14 @@ function getMeta(url: string): Promise<HTMLImageElement> {
|
|||
image-rendering: optimizeSpeed;
|
||||
opacity: 0;
|
||||
transition: opacity .5s;
|
||||
visibility: hidden;
|
||||
}
|
||||
|
||||
.loaded {
|
||||
opacity: 1 !important;
|
||||
}
|
||||
|
||||
.visible {
|
||||
opacity: 1 !important;
|
||||
visibility: visible !important;
|
||||
}
|
||||
</style>
|
||||
|
|
Loading…
Reference in a new issue