Skip to content

Commit 4c3cae4

Browse files
committed
🎈 perf: optimize image loading
1 parent 57197af commit 4c3cae4

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

public/images/640x360.webp

1.39 KB
Binary file not shown.

src/components/Work/Project.astro

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,34 @@ const { id, title, description, anchorLabel, githubLink, imageSrc, website } =
1515
Astro.props;
1616
---
1717

18-
<!-- TODO: optimize image loaging -->
18+
<script>
19+
document.addEventListener('DOMContentLoaded', () => {
20+
var lazyBackgrounds = [].slice.call(
21+
document.querySelectorAll('.lazy-background')
22+
);
23+
if ('IntersectionObserver' in window) {
24+
let lazyBackgroundObserver = new IntersectionObserver(
25+
(entries, observer) => {
26+
entries.forEach((entry) => {
27+
if (entry.isIntersecting) {
28+
console.log(entry.target);
29+
entry.target.classList.add('visibleImg');
30+
lazyBackgroundObserver.unobserve(entry.target);
31+
}
32+
});
33+
}
34+
);
35+
36+
lazyBackgrounds.forEach((lazyBackground: HTMLElement) => {
37+
lazyBackgroundObserver.observe(lazyBackground);
38+
});
39+
}
40+
});
41+
</script>
1942
<article
2043
id={id}
2144
class:list={[imageSrc]}
22-
class="reveal my-2 flex w-full flex-col self-start rounded border-[0.1rem] border-solid border-[var(--default-secundary-font-color)] bg-[#322644] bg-cover bg-center bg-no-repeat font-lato shadow-lg shadow-black"
45+
class="reveal bg-placeholder lazy-background my-2 flex w-full flex-col self-start rounded border-[0.1rem] border-solid border-[var(--default-secundary-font-color)] bg-[#322644] bg-cover bg-center bg-no-repeat font-lato shadow-lg shadow-black"
2346
>
2447
<div
2548
class="h-72 bg-[#5c497fc5] p-1 backdrop-brightness-[0.2] backdrop-grayscale transition-all duration-300 ease-in-out hover:bg-teal-600/0 hover:backdrop-grayscale-0 max-lg:h-80 max-md:h-64 max-sm:h-52 [&>*:first-child]:hover:scale-110"

src/components/Work/Work.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Subtitle from '../shared/Subtitle.astro';
1818
title="Cuvimaker"
1919
anchorLabel="See my online cv maker github project"
2020
githubLink="https://github.com/carlosjorger/cuvimaker"
21-
imageSrc="bg-cuvimakerExample"
21+
imageSrc="[&.visibleImg]:bg-cuvimakerExample"
2222
website="https://cuvimaker.netlify.app/"
2323
/>
2424
<Project
@@ -27,15 +27,15 @@ import Subtitle from '../shared/Subtitle.astro';
2727
title="Rusty Copier"
2828
anchorLabel="See my rusty copier github project"
2929
githubLink="https://github.com/carlosjorger/rustycopier"
30-
imageSrc="bg-rustycopier"
30+
imageSrc="[&.visibleImg]:bg-rustycopier"
3131
/>
3232
<Project
3333
id="angular-portfolio"
3434
description="It is a personal portfolio builded with angular using really cool animation (Angular animations)."
3535
title="Angular Portfolio"
3636
anchorLabel="See my portfolio github project"
3737
githubLink="https://github.com/carlosjorger/portfolio"
38-
imageSrc="bg-portfolio"
38+
imageSrc="[&.visibleImg]:bg-portfolio"
3939
website="https://carlosjorger.github.io/portfolio/"
4040
/>
4141
</div>

tailwind.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
portfolio: "url('/images/Portfolio.webp')",
88
rustycopier: "url('/images/Rustycopier.webp')",
99
cuvimakerExample: "url('/images/Cuvimaker-example.webp')",
10+
placeholder: "url('/images/640x360.webp')",
1011
},
1112
},
1213
fontFamily: {

0 commit comments

Comments
 (0)