Lazy load container logos, for better performance

This commit is contained in:
Alicia Sykes
2023-04-22 21:07:02 +01:00
parent eb3e5e5ade
commit 025c8f4642
2 changed files with 54 additions and 6 deletions

34
src/lib/lazy-load.ts Normal file
View File

@@ -0,0 +1,34 @@
// See how the options work here: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
let options = {
root: null,
rootMargin: "0px",
threshold: 0
}
export const lazyLoad = (image: any, src: string) => {
const loaded = () => {
image.classList.remove("loading");
image.style.opacity = "1";
};
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
image.src = src;
if (image.complete) {
loaded();
} else {
image.addEventListener("load", loaded);
}
}
},
options
);
observer.observe(image);
return {
destroy() {
observer.disconnect();
image.removeEventListener("load", loaded);
},
};
};

View File

@@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import Hero from '$lib/Hero.svelte'; import Hero from '$lib/Hero.svelte';
import ListFilter from '$lib/ListFilter.svelte'; import ListFilter from '$lib/ListFilter.svelte';
import Categories from '$lib/Categories.svelte'; import Categories from '$lib/Categories.svelte';
@@ -12,6 +13,8 @@
let searchTerm = ''; let searchTerm = '';
let showCategories = false;
let selectedCategories: string[] = []; let selectedCategories: string[] = [];
$: filteredTemplates = data.templates.filter((template: Template) => { $: filteredTemplates = data.templates.filter((template: Template) => {
@@ -32,6 +35,10 @@
); );
}); });
const showHideCategoryList = () => {
showCategories = !showCategories;
};
const toggleCategory = (category: string) => { const toggleCategory = (category: string) => {
if (selectedCategories.includes(category)) { if (selectedCategories.includes(category)) {
selectedCategories = selectedCategories.filter((cat) => cat !== category); selectedCategories = selectedCategories.filter((cat) => cat !== category);
@@ -44,20 +51,27 @@
searchTerm = ''; searchTerm = '';
selectedCategories = []; selectedCategories = [];
} }
</script> </script>
<!-- Main title, and CTA buttons --> <!-- Main title, and CTA buttons -->
<Hero /> <Hero />
<!-- Search bar, and Templates sub-title --> <!-- Search bar, and Templates sub-title -->
<ListFilter bind:searchTerm={searchTerm} /> <ListFilter
bind:searchTerm={searchTerm}
toggleCategories={showHideCategoryList}
isCategoriesVisible={showCategories}
/>
<!-- List of categories to filter by --> <!-- List of categories to filter by -->
<Categories {#if showCategories}
categories={data.categories} <Categories
selectedCategories={selectedCategories} categories={data.categories}
toggleCategory={toggleCategory} selectedCategories={selectedCategories}
/> toggleCategory={toggleCategory}
/>
{/if}
<!-- Text showing num results, and users search term + filters --> <!-- Text showing num results, and users search term + filters -->
<SearchSummary <SearchSummary