Open Design:用自然语言做设计

Open Design: The open-source Claude Design alternative

以下内容包含AIGC

最近试了一个开源工具 Open Design,本地优先,支持 BYOK,可以理解为 Claude Design 的对标产品和开源平替。

用法很直接:接入模型API后,在聊天框描述你想要的界面,右侧大面积区域就是画布,实时预览效果和源码,所见即所得。每次修改都保留历史版本,可以随时回退对比。

虽然你用 Claude Code + impeccable 之类的技能也能达成近似的效果,但这种“对话栏 + 画布”的交互形式(相比冷冰冰的命令行和代码块)显然更符合人类直觉和习惯。

我用它做了一个画廊页面,全程只在聊天框里写几句话的需求,看着它一版一版迭代,最后导出单个 HTML 文件。(成品 HTML 后附)

这个体验让我觉得有点微妙——它不是“帮设计师提效”,更像是把 UI 设计和前端开发这件事的准入门槛直接拆掉了。不懂 Figma、不会写 CSS,只要会描述自己想要什么,就能把东西做出来。


对独立开发者或小团队来说,它的价值主要在以下几点:

  • 支持本地模型,金点子不过别人的服务器,不用担心想法还没落地就先泄出去
  • BYOK,用自己的 API Key,不被那些热衷筑高墙、锁用户的巨头们裹挟(点名 A÷)
  • 所见即所得,聊天框右侧就是画布,实时预览,改到满意再导出——格式支持 HTML / PDF / PPTX / MP4 / ZIP,网页、幻灯片、视频一套流程全包了
  • 不止做界面,100+ 内置技能覆盖原型、Dashboard、PPT、图片、动效、音视频;HyperFrames 能把 HTML+CSS+动画直接渲染成 MP4
  • 设计体系,内置 150 套品牌级设计系统(Linear、Stripe、Notion、Apple……),每套都是一个 Markdown 文件,可以直接调用或者自己构建一套
  • 留在自己的工具里,支持一行命令把它装进 Claude Code、Cursor、Copilot 等 21 款主流 IDE 和 Agent,不用再开一个新应用,不打断现有工作流
  • 插件生态,261 个官方插件,150 套品牌级设计系统(Linear、Stripe、Notion……),直接调用

目前从版本号(0.9.0)来看,这个项目还比较早期,生成质量和工作流接入都值得继续观察。感兴趣的话先 star 跟着看看。
https://github.com/nexu-io/open-design

👉 HTML 成品
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
<!doctype html>
<html lang="zh-Hans">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>绘梦物语</title>
<link rel="preconnect" href="https://fonts.loli.net" />
<link
href="https://fonts.loli.net/css2?family=Noto+Serif+SC:wght@400;700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/photoswipe@5.4.4/dist/photoswipe.css"
/>
<style>
:root {
--bg: #faf9f6;
--surface: #ffffff;
--fg: #1e1a15;
--muted: #9a8e80;
--soft: #e8e0d4;
--coral: #e8644a;
--font-serif: 'Noto Serif SC', 'Georgia', serif;
--font-sans:
-apple-system, 'PingFang SC', 'Hiragino Sans GB', system-ui,
sans-serif;
}
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background: var(--bg);
color: var(--fg);
font-family: var(--font-sans);
-webkit-font-smoothing: antialiased;
overflow-x: hidden;
}

/* ── 背景漂移光斑 ── */
.bg-deco {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 0;
overflow: hidden;
}
.bg-deco span {
position: absolute;
border-radius: 50%;
filter: blur(8px);
will-change: transform;
}
.bg-deco .d1 {
width: 38vmax;
height: 38vmax;
background: var(--coral);
opacity: 0.056;
top: -12vmax;
right: -8vmax;
animation: drift1 14s ease-in-out infinite alternate;
}
.bg-deco .d2 {
width: 28vmax;
height: 28vmax;
background: #7dd9b0;
opacity: 0.1;
bottom: -8vmax;
left: -6vmax;
animation: drift2 18s ease-in-out infinite alternate;
}
.bg-deco .d3 {
width: 20vmax;
height: 20vmax;
background: #f9c846;
opacity: 0.14;
top: 55%;
right: -6vmax;
animation: drift3 11s ease-in-out infinite alternate;
}
@keyframes drift1 {
from {
transform: translate(0, 0) scale(1);
}
to {
transform: translate(-12vw, 14vh) scale(1.1);
}
}
@keyframes drift2 {
from {
transform: translate(0, 0) scale(1);
}
to {
transform: translate(14vw, -16vh) scale(1.15);
}
}
@keyframes drift3 {
from {
transform: translate(0, 0) scale(1);
}
to {
transform: translate(-10vw, -18vh) scale(0.88);
}
}

header,
main,
footer {
position: relative;
z-index: 1;
}

/* ── Hero ── */
.hero {
padding: clamp(48px, 10vw, 96px) 24px clamp(32px, 6vw, 56px);
text-align: center;
border-bottom: 1px solid var(--soft);
}
.hero h1 {
font-family: var(--font-serif);
font-size: clamp(38px, 8vw, 72px);
font-weight: 700;
letter-spacing: 0.12em;
color: var(--fg);
margin-bottom: 16px;
display: inline-flex;
align-items: center;
gap: 10px;
}
.hero-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--coral);
flex-shrink: 0;
animation: hero-dot-breathe 2.6s ease-in-out infinite;
}
@keyframes hero-dot-breathe {
0%,
100% {
transform: scale(1);
opacity: 1;
}
50% {
transform: scale(1.6);
opacity: 0.5;
}
}
.hero-sub {
font-size: clamp(13px, 2.5vw, 16px);
color: var(--muted);
line-height: 2;
max-width: 380px;
margin: 0 auto 32px;
}

/* ── 年龄导航 ── */
.age-nav-wrap {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
scrollbar-width: none;
padding-bottom: 2px;
}
.age-nav-wrap::-webkit-scrollbar {
display: none;
}
.age-nav {
display: inline-flex;
gap: 2px;
white-space: nowrap;
padding: 0 4px;
}
.age-nav a {
display: inline-flex;
flex-direction: column;
align-items: center;
padding: 8px 14px;
border-radius: 8px;
text-decoration: none;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
transition:
color 0.15s,
background 0.15s;
flex-shrink: 0;
}
.age-nav a:hover {
color: var(--coral);
background: rgba(255, 255, 255, 0.7);
}
.age-nav a .num {
font-family: var(--font-serif);
font-size: 22px;
font-weight: 700;
color: var(--fg);
line-height: 1;
}
.age-nav a:hover .num {
color: var(--coral);
}

/* ── Gallery ── */
.gallery-wrap {
max-width: 1140px;
margin: 0 auto;
padding: 0 16px 80px;
}
.age-section {
padding-top: clamp(48px, 8vw, 72px);
scroll-margin-top: 24px;
}
.age-header {
margin-bottom: 24px;
padding-bottom: 16px;
border-bottom: 1px solid var(--soft);
display: flex;
align-items: baseline;
gap: 16px;
}
.age-header .year-num {
font-family: var(--font-serif);
font-size: clamp(40px, 7vw, 60px);
font-weight: 700;
color: var(--soft);
line-height: 1;
flex-shrink: 0;
letter-spacing: -0.02em;
}
.age-header .year-info h2 {
font-family: var(--font-serif);
font-size: clamp(17px, 3vw, 22px);
font-weight: 700;
margin-bottom: 4px;
}
.age-header .year-info p {
font-size: 13px;
color: var(--muted);
}

.photo-grid {
column-count: 2;
column-gap: 10px;
}
@media (min-width: 560px) {
.photo-grid {
column-count: 3;
column-gap: 12px;
}
}
@media (min-width: 860px) {
.photo-grid {
column-count: 4;
column-gap: 14px;
}
}
@media (min-width: 1060px) {
.photo-grid {
column-count: 5;
column-gap: 14px;
}
}

.photo-card {
break-inside: avoid;
margin-bottom: 10px;
display: block;
text-decoration: none;
color: inherit;
cursor: pointer;
background: var(--surface);
border-radius: 10px;
overflow: hidden;
transition:
transform 0.2s ease,
box-shadow 0.2s ease;
}
@media (min-width: 560px) {
.photo-card:hover {
transform: translateY(-3px);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}
}
.photo-card img {
width: 100%;
display: block;
object-fit: cover;
}
.photo-caption {
padding: 9px 11px 11px;
font-size: 11.5px;
color: var(--muted);
line-height: 1.6;
}

footer {
text-align: center;
padding: 32px 16px 56px;
border-top: 1px solid var(--soft);
font-size: 13px;
color: var(--muted);
line-height: 2;
}
footer strong {
font-family: var(--font-serif);
color: var(--fg);
letter-spacing: 0.06em;
}
</style>
</head>
<body>
<div class="bg-deco" aria-hidden="true">
<span class="d1"></span><span class="d2"></span><span class="d3"></span>
</div>

<header class="hero">
<h1>绘梦物语<span class="hero-dot"></span></h1>
<p class="hero-sub">
从你第一声啼哭,到如今满眼光亮<br />每一帧,都是爸爸妈妈最珍贵的收藏
</p>
<div class="age-nav-wrap"><nav class="age-nav" id="age-nav"></nav></div>
</header>

<main class="gallery-wrap" id="gallery-wrap"></main>

<footer>
<strong>绘梦物语</strong><br />愿你永远好奇,永远闪光<br />
<span style="font-size: 11px; opacity: 0.5">2026</span>
</footer>

<!-- PhotoSwipe 5 -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"></div>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@5.4.4/dist/umd/photoswipe.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@5.4.4/dist/umd/photoswipe-lightbox.umd.min.js"></script>

<script>
const BIRTH_YEAR = 2019;

const SECTIONS = [
{ age: 0, label: '初来乍到', desc: '世界上最小最软的你' },
{ age: 1, label: '蹒跚学步', desc: '第一次站起来,第一次叫妈妈' },
{ age: 2, label: '小小探险家', desc: '好奇心比天还大' },
{ age: 3, label: '问不完的为什么', desc: '话多得像小收音机' },
{
age: 4,
label: '每张纸都是宇宙',
desc: '你的第一幅"杰作"画的是全家福'
},
{ age: 5, label: '台上最亮那颗星', desc: '六一演出,舞台属于你' },
{ age: 6, label: '书包里的阳光', desc: '第一天上学,书包比你还重' },
{
age: 7,
label: '就是现在的你',
desc: '越来越大,永远是我们的小宝贝 ♥'
}
];

// thumb 可选:缩略图URL;未填时缩略图使用 src。灯箱大图始终用 src。
// w/h 是大图尺寸,PhotoSwipe 需要此信息以正确布局(填写实际尺寸更佳)
const PHOTOS = [
{
age: 0,
src: 'https://picsum.photos/seed/b01/800/1040',
thumb: 'https://picsum.photos/seed/b01/400/520',
caption: '刚出生的小手,软软的,像棉花糖',
aspect: 'portrait'
},
{
age: 0,
src: 'https://picsum.photos/seed/b02/800/600',
thumb: 'https://picsum.photos/seed/b02/400/300',
caption: '第一次睁眼看这个世界',
aspect: 'landscape'
},
{
age: 0,
src: 'https://picsum.photos/seed/b03/800/800',
caption: '满月照,笑得像小天使',
aspect: 'square'
},
{
age: 0,
src: 'https://picsum.photos/seed/b04/800/1040',
thumb: 'https://picsum.photos/seed/b04/400/520',
caption: '第一次被抱出医院,阳光好大',
aspect: 'portrait'
},
{
age: 0,
src: 'https://picsum.photos/seed/b05/800/600',
caption: '小小的脚丫,比妈妈的手掌还短',
aspect: 'landscape'
},
{
age: 1,
src: 'https://picsum.photos/seed/a1a/800/1040',
thumb: 'https://picsum.photos/seed/a1a/400/520',
caption: '摇摇晃晃迈出第一步',
aspect: 'portrait'
},
{
age: 1,
src: 'https://picsum.photos/seed/a1b/800/600',
thumb: 'https://picsum.photos/seed/a1b/400/300',
caption: '生日蛋糕沾满了小脸蛋',
aspect: 'landscape'
},
{
age: 1,
src: 'https://picsum.photos/seed/a1c/800/920',
thumb: 'https://picsum.photos/seed/a1c/400/460',
caption: '第一次喊"妈妈",把我感动哭了',
aspect: 'portrait'
},
{
age: 1,
src: 'https://picsum.photos/seed/a1d/800/800',
caption: '坐在婴儿车里睡着了,嘴角带着笑',
aspect: 'square'
},
{
age: 2,
src: 'https://picsum.photos/seed/a2a/800/600',
thumb: 'https://picsum.photos/seed/a2a/400/300',
caption: '公园里追鸽子,追得气喘吁吁',
aspect: 'landscape'
},
{
age: 2,
src: 'https://picsum.photos/seed/a2b/800/1040',
thumb: 'https://picsum.photos/seed/a2b/400/520',
caption: '趴在地上看蚂蚁搬家,能看一下午',
aspect: 'portrait'
},
{
age: 2,
src: 'https://picsum.photos/seed/a2c/800/800',
caption: '第一次吃草莓,酸得皱起小鼻子',
aspect: 'square'
},
{
age: 2,
src: 'https://picsum.photos/seed/a2d/800/600',
caption: '雨天踩水坑,鞋子湿透了还不肯走',
aspect: 'landscape'
},
{
age: 2,
src: 'https://picsum.photos/seed/a2e/800/1040',
caption: '第一次堆沙堡,专注得忘记吃饭',
aspect: 'portrait'
},
{
age: 3,
src: 'https://picsum.photos/seed/a3a/800/960',
thumb: 'https://picsum.photos/seed/a3a/400/480',
caption: '幼儿园第一天,勇敢得让妈妈骄傲',
aspect: 'portrait'
},
{
age: 3,
src: 'https://picsum.photos/seed/a3b/800/600',
thumb: 'https://picsum.photos/seed/a3b/400/300',
caption: '问爸爸"云彩是棉花做的吗?"',
aspect: 'landscape'
},
{
age: 3,
src: 'https://picsum.photos/seed/a3c/800/800',
caption: '第一次骑三轮车,骑到不想下来',
aspect: 'square'
},
{
age: 3,
src: 'https://picsum.photos/seed/a3d/800/1040',
caption: '睡前要听三个故事才肯闭眼',
aspect: 'portrait'
},
{
age: 4,
src: 'https://picsum.photos/seed/a4a/800/1040',
thumb: 'https://picsum.photos/seed/a4a/400/520',
caption: '第一幅"杰作",画的是全家福',
aspect: 'portrait'
},
{
age: 4,
src: 'https://picsum.photos/seed/a4b/800/800',
caption: '最喜欢的彩笔用完了,哭得伤心极了',
aspect: 'square'
},
{
age: 4,
src: 'https://picsum.photos/seed/a4c/800/600',
thumb: 'https://picsum.photos/seed/a4c/400/300',
caption: '海边玩沙,快乐得忘记回家',
aspect: 'landscape'
},
{
age: 4,
src: 'https://picsum.photos/seed/a4d/800/1040',
caption: '给小熊玩偶喂饭,认真极了',
aspect: 'portrait'
},
{
age: 4,
src: 'https://picsum.photos/seed/a4e/800/600',
caption: '第一次做饼干,面粉沾到了头发上',
aspect: 'landscape'
},
{
age: 5,
src: 'https://picsum.photos/seed/a5a/800/1040',
thumb: 'https://picsum.photos/seed/a5a/400/520',
caption: '六一演出,台上最闪亮的小星星',
aspect: 'portrait'
},
{
age: 5,
src: 'https://picsum.photos/seed/a5b/800/600',
thumb: 'https://picsum.photos/seed/a5b/400/300',
caption: '第一次不扶人骑自行车!',
aspect: 'landscape'
},
{
age: 5,
src: 'https://picsum.photos/seed/a5c/800/800',
caption: '学会了跳绳,一口气跳了二十下',
aspect: 'square'
},
{
age: 5,
src: 'https://picsum.photos/seed/a5d/800/1040',
caption: '画了一整本的小人书送给奶奶',
aspect: 'portrait'
},
{
age: 6,
src: 'https://picsum.photos/seed/a6a/800/960',
thumb: 'https://picsum.photos/seed/a6a/400/480',
caption: '入学第一天,书包比你还重',
aspect: 'portrait'
},
{
age: 6,
src: 'https://picsum.photos/seed/a6b/800/800',
caption: '认真写作业的样子,超级可爱',
aspect: 'square'
},
{
age: 6,
src: 'https://picsum.photos/seed/a6c/800/600',
caption: '第一次考了满分,高兴得原地跳',
aspect: 'landscape'
},
{
age: 6,
src: 'https://picsum.photos/seed/a6d/800/1040',
caption: '秋游捡了一书包的叶子回来',
aspect: 'portrait'
},
{
age: 6,
src: 'https://picsum.photos/seed/a6e/800/600',
caption: '学校运动会,跑步得了第二名',
aspect: 'landscape'
},
{
age: 7,
src: 'https://picsum.photos/seed/a7a/800/1040',
thumb: 'https://picsum.photos/seed/a7a/400/520',
caption: '7岁生日,许了一个不肯说的愿望',
aspect: 'portrait'
},
{
age: 7,
src: 'https://picsum.photos/seed/a7b/800/600',
thumb: 'https://picsum.photos/seed/a7b/400/300',
caption: '就是现在的你,美好得让人心疼',
aspect: 'landscape'
},
{
age: 7,
src: 'https://picsum.photos/seed/a7c/800/800',
caption: '第一次自己做早饭,煎了个荷包蛋',
aspect: 'square'
},
{
age: 7,
src: 'https://picsum.photos/seed/a7d/800/1040',
caption: '写给爸爸妈妈的信,歪歪扭扭全是爱',
aspect: 'portrait'
}
];

const RATIO = { portrait: '4/5', landscape: '4/3', square: '1/1' };

// 平滑滚动:easeInOutCubic 缓动,duration 毫秒
function smoothScrollTo(targetY, duration) {
const startY = window.scrollY;
const diff = targetY - startY;
if (diff === 0) return;
let start = null;
function step(ts) {
if (!start) start = ts;
const elapsed = ts - start;
const progress = Math.min(elapsed / duration, 1);
// easeInOutCubic
const ease =
progress < 0.5
? 4 * progress ** 3
: 1 - (-2 * progress + 2) ** 3 / 2;
window.scrollTo(0, startY + diff * ease);
if (progress < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}

function yearStr(age) {
const y = BIRTH_YEAR + age;
return `${y}${y + 1}`;
}

const nav = document.getElementById('age-nav');
const wrap = document.getElementById('gallery-wrap');

// 建立每个年龄的 PhotoSwipe gallery 数据
const galleries = {}; // age -> [{ src, w, h, alt }]

SECTIONS.forEach(s => {
galleries[s.age] = [];
const pad = String(s.age).padStart(2, '0');

const a = document.createElement('a');
a.href = `#age-${s.age}`;
a.innerHTML = `<span class="num">${pad}</span>岁`;
// 拦截默认跳转,改用平滑滚动;目标定位到 .age-header 而非 section 顶部
// (section 有 clamp(48-72px) 的 padding-top,直接跳 section 顶部会偏高)
a.addEventListener('click', e => {
e.preventDefault();
const section = document.getElementById(`age-${s.age}`);
if (!section) return;
const header = section.querySelector('.age-header') || section;
smoothScrollTo(
header.getBoundingClientRect().top + window.scrollY - 24,
480
);
});
nav.appendChild(a);

const sec = document.createElement('section');
sec.className = 'age-section';
sec.id = `age-${s.age}`;
sec.innerHTML = `
<div class="age-header">
<div class="year-num">${pad}</div>
<div class="year-info">
<h2>${s.label}</h2>
<p>${yearStr(s.age)} · ${s.desc}</p>
</div>
</div>
<div class="photo-grid" id="grid-${s.age}"></div>`;
wrap.appendChild(sec);
});

PHOTOS.forEach((p, i) => {
const grid = document.getElementById(`grid-${p.age}`);
if (!grid) return;

// 在该年龄组中的索引
const idx = galleries[p.age].length;
// PhotoSwipe 需要 w/h 来预算容器尺寸,从 aspect 枚举自动推导,无需用户手动填写
const ASPECT_DIM = {
portrait: [800, 1040],
landscape: [800, 600],
square: [800, 800]
};
const [aw, ah] = ASPECT_DIM[p.aspect] || [800, 800];
galleries[p.age].push({ src: p.src, w: aw, h: ah, alt: p.caption });

const thumbSrc = p.thumb || p.src;
const card = document.createElement('div');
card.className = 'photo-card';
card.dataset.age = p.age;
card.dataset.idx = idx;
card.innerHTML = `
<img src="${thumbSrc}" alt="${p.caption}" loading="lazy" style="aspect-ratio:${RATIO[p.aspect]}">
<div class="photo-caption">${p.caption}</div>`;
grid.appendChild(card);
});

// 保存当前活跃的灯箱实例(用于 popstate 关闭)
let activeLightbox = null;

// 点击打开 PhotoSwipe
document.getElementById('gallery-wrap').addEventListener('click', e => {
const card = e.target.closest('.photo-card');
if (!card) return;
const age = Number(card.dataset.age);
const startIndex = Number(card.dataset.idx);
const items = galleries[age];

const lightbox = new PhotoSwipeLightbox({
dataSource: items,
pswpModule: PhotoSwipe,
bgOpacity: 0.92,
// 移动端双指缩放
pinchToClose: true,
closeOnVerticalDrag: true
});

// 灯箱销毁时:清除引用;若 hash 仍在则 back 清除锚点
lightbox.on('destroy', () => {
activeLightbox = null;
if (location.hash === '#view') {
history.back();
}
});

lightbox.init();
activeLightbox = lightbox;

// 推入 history 锚点,后退可关闭灯箱而不意外离开页面
if (!location.hash.includes('#view')) {
history.pushState({ lightboxOpen: true }, '', '#view');
}

lightbox.loadAndOpen(startIndex);
});

// 监听浏览器后退,自动关闭灯箱
window.addEventListener('popstate', () => {
if (activeLightbox && activeLightbox.pswp) {
activeLightbox.pswp.close();
}
});
</script>
</body>
</html>


Buy me a coffee ☕