Skip to content

Commit e5e8f0e

Browse files
committed
restyle gallery
1 parent be48c08 commit e5e8f0e

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

src/components/Gallery.astro

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type { ImageMetadata } from "astro";
33
import { Image, getImage } from "astro:assets";
44
const { imageFolder }= Astro.props;
55
6-
const imagesImportFns = import.meta.glob<{default: ImageMetadata}>("/src/assets/gallery/**/*.{jpeg,jpg,png,gif}");
6+
const imagesImportFns = import.meta.glob<{default: ImageMetadata}>(
7+
"/src/assets/gallery/**/*.{jpeg,jpg,png,gif}",
8+
{ eager: true }
9+
);
710
const imagePaths = Object.keys(imagesImportFns).filter((imagePath) => {
811
return imagePath.startsWith(`/src/assets/gallery/${imageFolder}`)
912
}).sort((a, b) => {
@@ -18,18 +21,17 @@ const imagePaths = Object.keys(imagesImportFns).filter((imagePath) => {
1821
<section class="gallery" id="gallery">
1922
{
2023
imagePaths.map(async (imagePath: any) => {
21-
let image = imagesImportFns[imagePath]();
22-
let optimizedImage = await getImage({
23-
src: image,
24-
});
24+
const imageModule = imagesImportFns[imagePath];
25+
const image = imageModule.default;
2526
return (
2627
<a
27-
href={optimizedImage.src}
28-
data-pswp-width="1080"
29-
data-pswp-height="720"
28+
href={image.src}
29+
data-pswp-width={image.width}
30+
data-pswp-height={image.height}
3031
data-cropped="true"
3132
target="_blank"
32-
class="gallery__item"><Image src={image} alt=""></Image>
33+
class="gallery__item">
34+
<Image src={image} alt="" loading="lazy"></Image>
3335
</a>
3436
)
3537
})
@@ -50,42 +52,59 @@ const imagePaths = Object.keys(imagesImportFns).filter((imagePath) => {
5052
lightbox.init();
5153
</script>
5254

55+
<script>
56+
document.addEventListener("DOMContentLoaded", () => {
57+
document.querySelectorAll(".gallery__item img").forEach((img) => {
58+
if (img.complete) {
59+
img.classList.add("is-loaded");
60+
} else {
61+
img.addEventListener("load", () => {
62+
img.classList.add("is-loaded");
63+
});
64+
}
65+
});
66+
});
67+
</script>
68+
5369
<style>
5470
.gallery {
55-
display: grid;
56-
grid-template-columns: repeat(auto-fill, 170px);
57-
gap: var(--spacing-sm);
58-
margin: auto;
59-
margin-bottom: var(--spacing-xl);
60-
padding: var(--spacing-sm);
61-
justify-content: center;
71+
column-count: 3;
72+
column-gap: var(--spacing-xxs);
73+
margin: 0 auto 80px auto;
74+
}
75+
76+
/* Tablet */
77+
@media (max-width: 900px) {
78+
.gallery {
79+
column-count: 2;
80+
}
81+
}
82+
83+
/* Mobile */
84+
@media (max-width: 600px) {
85+
.gallery {
86+
column-count: 1;
87+
}
6288
}
6389

6490
.gallery__item {
6591
display: block;
66-
height: 125px;
67-
width: 100%;
68-
border: 1px solid #ddd;
69-
border-radius: 4px;
70-
padding: 5px;
71-
background-color: rgb(255, 255, 255);
72-
overflow: hidden;
73-
transition: 0.3s;
74-
}
75-
76-
.gallery__item:hover {
77-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
92+
margin-bottom: var(--spacing-xxs);
93+
break-inside: avoid;
7894
}
7995

8096
.gallery__item img {
81-
filter: brightness(95%);
82-
display: block;
8397
width: 100%;
84-
height: 100%;
85-
}
98+
height: auto;
99+
display: block;
86100

87-
.gallery__item img:hover {
88-
filter: brightness(100%);
101+
filter: grayscale(100%) blur(6px);
102+
opacity: 0.7;
103+
transition: all 0.8s ease;
89104
}
90105

106+
.gallery__item img.is-loaded {
107+
filter: none;
108+
opacity: 1;
109+
}
91110
</style>

src/pages/photos/heaven.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import Gallery from "../../components/Gallery.astro";
55

66
<BaseLayout>
77
<div class="content">
8-
<h2 class="title">Photos</h2>
9-
<h3 class="desc">~ Heaven on Earth ~</h3>
8+
<h2 class="title">Gallery |
9+
<span class="desc">Heaven on Earth</span>
10+
</h2>
1011
<Gallery imageFolder="heaven" />
1112
</div>
1213
</BaseLayout>

0 commit comments

Comments
 (0)