-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
252 lines (217 loc) · 10.5 KB
/
index.html
File metadata and controls
252 lines (217 loc) · 10.5 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OneShot View Demo</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Essential CSS for View Transitions
This enables the custom 'hero-image' transition name we will use dynamically.
*/
::view-transition-old(hero-image),
::view-transition-new(hero-image) {
/* Prevent default cross-fade for the image, forcing a pure geometry morph */
animation: none;
mix-blend-mode: normal;
}
/* Standard fade for other elements (text, etc) */
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 0.4s;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f3f4f6;
overflow-y: scroll; /* Prevent layout shift when scrollbar appears/disappears */
}
/* Utilities for text truncation */
.line-clamp-2 {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Note: Removed 'scroll-behavior: smooth' to ensure accurate scroll restoration during transitions */
</style>
</head>
<body class="antialiased text-gray-800">
<div id="app" class="min-h-screen">
<!-- Content injected by JavaScript -->
</div>
<script>
// --- Configuration & State ---
const CONFIG = {
TOTAL_ITEMS: 30 // Fixed total of 30 items
};
const state = {
view: 'list', // 'list' or 'detail'
items: [],
selectedId: null,
scrollPosition: 0,
nextId: 1
};
// --- Mock Data Generator ---
function generateItems(count) {
const newItems = [];
// Fixed image URL for all cards
const fixedImage = "https://images.unsplash.com/photo-1472214103451-9374bd1c798e?ixlib=rb-4.0.3&auto=format&fit=crop&w=1000&q=80";
for (let i = 0; i < count; i++) {
const id = state.nextId++;
newItems.push({
id: id,
title: `Beautiful Destination #${id}`,
description: `Experience the serenity of nature in location #${id}. This card demonstrates a seamless transition.`,
image: fixedImage,
fullText: `This is the detailed content for Destination #${id}. \n\nThe View Transition API allows us to create seamless animations between DOM states. Notice how the image morphed from its grid position to this full-width header? That wasn't a transform animation on top of the page; strictly speaking, the browser took a snapshot of the old state and the new state, and interpolated the geometry of the element we tagged with 'view-transition-name'.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`
});
}
return newItems;
}
// Initialize with exactly 30 items
state.items = generateItems(CONFIG.TOTAL_ITEMS);
// --- Render Functions ---
const app = document.getElementById('app');
function render() {
if (state.view === 'list') {
renderList();
} else {
renderDetail();
}
}
function renderList() {
const gridHtml = state.items.map(item => `
<div
class="card bg-white rounded-xl shadow-md overflow-hidden cursor-pointer hover:shadow-xl transition-shadow duration-300 flex flex-col h-full"
onclick="handleCardClick(${item.id})"
>
<div class="relative overflow-hidden h-48 w-full bg-gray-200">
<img
id="img-${item.id}"
src="${item.image}"
alt="${item.title}"
class="w-full h-full object-cover"
loading="lazy"
>
</div>
<div class="p-4 flex-1 flex flex-col">
<h3 class="text-lg font-bold mb-2 text-gray-900">${item.title}</h3>
<p class="text-gray-600 text-sm line-clamp-2">${item.description}</p>
</div>
</div>
`).join('');
app.innerHTML = `
<header class="bg-white shadow-sm sticky top-0 z-10">
<div class="max-w-4xl mx-auto px-4 py-4 flex justify-between items-center">
<h1 class="text-xl font-bold text-indigo-600">OneShot View Gallery</h1>
<span class="text-xs text-gray-400">${state.items.length} Items</span>
</div>
</header>
<main class="max-w-4xl mx-auto px-4 py-6">
<div id="grid-container" class="grid grid-cols-1 sm:grid-cols-2 gap-6 pb-10">
${gridHtml}
</div>
</main>
`;
// Restore scroll position INSTANTLY to ensure View Transition captures the correct frame
window.scrollTo({
top: state.scrollPosition,
behavior: 'instant'
});
}
function renderDetail() {
const item = state.items.find(i => i.id === state.selectedId);
if (!item) return;
app.innerHTML = `
<div class="bg-white min-h-screen">
<div class="relative w-full h-[50vh] bg-gray-900">
<img
src="${item.image}"
class="w-full h-full object-cover"
style="view-transition-name: hero-image"
alt="${item.title}"
>
<button
onclick="handleBackClick()"
class="absolute top-4 left-4 bg-black/50 hover:bg-black/70 text-white rounded-full p-2 backdrop-blur-sm transition-colors z-20 flex items-center gap-2 pr-4"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
</svg>
<span>Back</span>
</button>
<div class="absolute bottom-0 left-0 w-full bg-gradient-to-t from-black/80 to-transparent p-6 pt-20">
<h1 class="text-3xl md:text-4xl font-bold text-white shadow-black drop-shadow-md">${item.title}</h1>
</div>
</div>
<main class="max-w-3xl mx-auto px-6 py-10 animate-fade-in-up">
<div class="prose prose-lg text-gray-700">
<p class="text-xl font-medium text-indigo-600 mb-6">${item.description}</p>
${item.fullText.split('\n\n').map(para => `<p class="mb-4 leading-relaxed">${para}</p>`).join('')}
<div class="h-64 bg-gray-100 rounded-lg mt-8 flex items-center justify-center text-gray-400">
Additional Content Area
</div>
</div>
</main>
</div>
`;
// Scroll to top INSTANTLY for the new detail page
window.scrollTo({
top: 0,
behavior: 'instant'
});
}
// --- Interaction Handlers ---
window.handleCardClick = async (id) => {
// 1. Save Scroll Position before transition starts
state.scrollPosition = window.scrollY;
state.selectedId = id;
// 2. Prepare Transition
const clickedImg = document.getElementById(`img-${id}`);
if (clickedImg) {
clickedImg.style.viewTransitionName = 'hero-image';
}
// 3. Execute Transition
if (document.startViewTransition) {
const transition = document.startViewTransition(() => {
state.view = 'detail';
render();
});
try {
await transition.finished;
} finally {
// Cleanup
}
} else {
state.view = 'detail';
render();
}
};
window.handleBackClick = async () => {
if (!document.startViewTransition) {
state.view = 'list';
render();
return;
}
const transition = document.startViewTransition(() => {
state.view = 'list';
render();
// Note: The renderList() function handles the scrolling to state.scrollPosition.
// It is critical this happens synchronously here so the 'new' snapshot is correct.
const targetImg = document.getElementById(`img-${state.selectedId}`);
if (targetImg) {
targetImg.style.viewTransitionName = 'hero-image';
}
});
await transition.finished;
// Cleanup after animation is done
const targetImg = document.getElementById(`img-${state.selectedId}`);
if (targetImg) {
targetImg.style.viewTransitionName = '';
}
};
// --- Init ---
render();
</script>
</body>
</html>