Skip to content

Commit a0ab3be

Browse files
1 parent b933778 commit a0ab3be

File tree

6 files changed

+97
-137
lines changed

6 files changed

+97
-137
lines changed

Source/Background.astro

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,92 @@
1-
<div id="Background"></div>
1+
---
2+
import { Image } from "astro:assets";
3+
---
4+
5+
<div id="Layout">
6+
<div id="Background" class="Container">
7+
<Image
8+
src="/Asset/Background.svg"
9+
alt=""
10+
role="presentation"
11+
class="Image"
12+
loading="eager"
13+
decoding="async"
14+
width={1920}
15+
height={1080}
16+
/>
17+
</div>
18+
19+
<div id="Rock" class="Container">
20+
<picture>
21+
<source
22+
media="(min-width: 1920px)"
23+
srcset="/Asset/DesktopLargeRock.webp"
24+
/>
25+
26+
<source
27+
media="(min-width: 1280px)"
28+
srcset="/Asset/DesktopRock.webp"
29+
/>
30+
31+
<source
32+
media="(min-width: 768px)"
33+
srcset="/Asset/TabletRock.webp"
34+
/>
35+
36+
<img
37+
src="/Asset/MobileRock.webp"
38+
alt=""
39+
role="presentation"
40+
class="Image"
41+
loading="eager"
42+
decoding="async"
43+
width="2400"
44+
height="1309"
45+
/>
46+
</picture>
47+
</div>
48+
</div>
249

350
<style>
451
@import "./Background/Stylesheet.css";
552
</style>
653

7-
<script is:inline>
8-
document.addEventListener("DOMContentLoaded", () => {
9-
const Container = document.getElementById("Background");
54+
<script>
55+
(() => {
56+
const Layout = document.querySelector<HTMLDivElement>("#Layout");
1057

11-
if (!Container) {
58+
const _Image = Array.from(
59+
Layout?.querySelectorAll<HTMLImageElement>(".Image") ?? [],
60+
);
61+
62+
if (!Layout || _Image.length === 0) {
1263
return;
1364
}
1465

15-
let Rock;
66+
const Load = performance.now();
1667

17-
if (window.matchMedia("(min-width: 1920px)").matches) {
18-
Rock = "/Asset/DesktopLargeRock.webp";
19-
} else if (window.matchMedia("(min-width: 1280px)").matches) {
20-
Rock = "/Asset/DesktopRock.webp";
21-
} else if (window.matchMedia("(min-width: 768px)").matches) {
22-
Rock = "/Asset/TabletRock.webp";
23-
} else {
24-
Rock = "/Asset/MobileRock.webp";
25-
}
68+
Promise.all(
69+
_Image.map((_Image) => {
70+
if (_Image.complete) {
71+
return Promise.resolve();
72+
}
73+
74+
return new Promise((resolve, reject) => {
75+
_Image.addEventListener("load", resolve);
2676

27-
const Element = [Rock, "/Asset/Background.svg"].map((src) => {
28-
const _Image = new Image();
29-
30-
_Image.src = src;
31-
32-
return _Image;
33-
});
34-
35-
if (Element.every((Image) => Image.complete)) {
36-
Container.classList.add("Load");
37-
} else {
38-
Promise.all(
39-
Element.map((Image) => {
40-
return new Promise((Resolve, Reject) => {
41-
if (Image.complete) {
42-
// @ts-expect-error
43-
Resolve();
44-
} else {
45-
Image.onload = Resolve;
46-
47-
Image.onerror = Reject;
48-
}
49-
});
50-
}),
51-
).then(() => {
52-
Container.classList.add("WithTransition");
53-
54-
setTimeout(() => {
55-
Container.classList.add("Load");
56-
}, 0);
77+
_Image.addEventListener("error", reject);
78+
});
79+
}),
80+
)
81+
.then(() => {
82+
if (performance.now() - Load > 50) {
83+
Layout.classList.add("Transition");
84+
}
85+
86+
Layout.classList.add("Load");
87+
})
88+
.catch(() => {
89+
Layout.classList.add("Load");
5790
});
58-
}
59-
});
91+
})();
6092
</script>

Source/Background/Stylesheet.css

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,33 @@
1-
#Background {
2-
position: fixed;
3-
4-
left: 0;
5-
6-
top: 0;
7-
8-
height: 100vh;
9-
10-
width: 100vw;
1+
#Layout {
2+
@apply fixed inset-0 h-screen w-screen overflow-hidden opacity-0;
113

124
z-index: -1;
135

14-
opacity: 0;
15-
166
&.Load {
17-
opacity: 1;
7+
@apply opacity-100;
188
}
199

20-
&.WithTransition {
21-
transition: opacity 0.8s ease-in-out;
10+
&.Transition {
11+
transition: opacity 0.8s ease-out;
2212
}
2313

24-
&:before {
25-
content: "";
26-
27-
position: fixed;
28-
29-
left: 50%;
30-
31-
top: 50%;
32-
33-
height: 100vh;
14+
.Container {
15+
@apply absolute left-1/2 top-1/2 h-screen;
3416

3517
width: 160vw;
3618

37-
transform: translateX(-50%) translateY(-50%);
38-
39-
background-size: cover;
40-
41-
background-position: center;
42-
43-
background-repeat: no-repeat;
19+
transform: translate(-50%, -50%);
20+
}
4421

45-
image-rendering: pixelated;
22+
.Image {
23+
@apply h-full w-full object-cover object-center;
24+
}
4625

26+
#Rock {
4727
z-index: -3;
48-
49-
background-image: image-set(
50-
url("/Asset/MobileRock.webp") 1x,
51-
52-
url("/Asset/TabletRock.webp") 2x
53-
);
54-
55-
@media (min-width: 768px) {
56-
& {
57-
background-image: image-set(
58-
url("/Asset/TabletRock.webp") 1x,
59-
60-
url("/Asset/DesktopRock.webp") 2x
61-
);
62-
}
63-
}
64-
@media (min-width: 1280px) {
65-
& {
66-
background-image: image-set(
67-
url("/Asset/DesktopRock.webp") 1x,
68-
69-
url("/Asset/DesktopLargeRock.webp") 2x
70-
);
71-
}
72-
}
73-
@media (min-width: 1920px) {
74-
& {
75-
background-image: url("/Asset/DesktopLargeRock.webp");
76-
}
77-
}
7828
}
7929

80-
&:after {
81-
content: "";
82-
83-
position: fixed;
84-
85-
left: 50%;
86-
87-
top: 50%;
88-
89-
height: 100vh;
90-
91-
width: 160vw;
92-
93-
transform: translateX(-50%) translateY(-50%);
94-
95-
background-size: cover;
96-
97-
background-position: center;
98-
99-
background-repeat: no-repeat;
100-
101-
background-image: url("/Asset/Background.svg");
102-
30+
#Background {
10331
z-index: -2;
10432
}
10533
}

0 commit comments

Comments
 (0)