-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (245 loc) · 8.33 KB
/
script.js
File metadata and controls
299 lines (245 loc) · 8.33 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
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
// 勇气罐子管理类
class CourageJarManager {
constructor() {
this.notes = this.loadNotes();
this.init();
}
// 从localStorage加载勇气纸条
loadNotes() {
const notes = localStorage.getItem("courageJarNotes");
return notes ? JSON.parse(notes) : [];
}
// 保存勇气纸条到localStorage
saveNotes() {
localStorage.setItem("courageJarNotes", JSON.stringify(this.notes));
}
// 初始化应用
init() {
this.renderPaperBalls();
this.updatePaperCount();
this.setupEventListeners();
}
// 设置事件监听器
setupEventListeners() {
// 摇晃罐子按钮
const shakeBtn = document.getElementById("shakeBtn");
shakeBtn.addEventListener("click", () => this.shakeJar());
// 添加纸条按钮
const addNoteBtn = document.getElementById("addNoteBtn");
addNoteBtn.addEventListener("click", () => this.showEditModal());
// 玻璃罐点击事件 - 直接打开编辑模态框
const glassJar = document.querySelector(".glass-jar");
glassJar.addEventListener("click", () => this.showEditModal());
// 保存纸条按钮
const saveNoteBtn = document.getElementById("saveNoteBtn");
saveNoteBtn.addEventListener("click", () => this.saveEditedNote());
// 取消编辑按钮
const cancelEditBtn = document.getElementById("cancelEditBtn");
cancelEditBtn.addEventListener("click", () => this.hideModal());
// 模态框关闭事件
const closeBtn = document.querySelector(".close");
const modal = document.getElementById("noteModal");
closeBtn.addEventListener("click", () => {
this.hideModal();
});
// 点击模态框外部关闭
window.addEventListener("click", (e) => {
if (e.target === modal) {
this.hideModal();
}
});
// 键盘事件(ESC关闭模态框)
window.addEventListener("keydown", (e) => {
if (e.key === "Escape" && modal.style.display === "block") {
this.hideModal();
}
});
}
// 添加勇气纸条
addNote(content) {
const note = {
id: Date.now(),
content: content,
date: new Date().toISOString(),
};
this.notes.push(note);
this.saveNotes();
this.animateNewPaperBall(note);
this.updatePaperCount();
this.showNotification("勇气纸条已成功存入罐子!", "success");
return true;
}
// 从罐子中随机抽取纸条
getRandomNote() {
if (this.notes.length === 0) {
return null;
}
const randomIndex = Math.floor(Math.random() * this.notes.length);
return this.notes[randomIndex];
}
// 渲染纸团
renderPaperBalls() {
const container = document.getElementById("paperBallsContainer");
container.innerHTML = "";
this.notes.forEach((note, index) => {
const paperBall = this.createPaperBallElement(note, index);
container.appendChild(paperBall);
});
}
// 创建纸团元素
createPaperBallElement(note, index) {
const paperBall = document.createElement("div");
paperBall.className = "paper-ball";
paperBall.dataset.noteId = note.id;
// 设置纸团的随机位置和大小
const randomRotation = Math.floor(Math.random() * 360);
const randomSize = 25 + Math.floor(Math.random() * 10);
paperBall.style.transform = `rotate(${randomRotation}deg)`;
paperBall.style.width = `${randomSize}px`;
paperBall.style.height = `${randomSize}px`;
// 设置纸团的堆积效果
const row = Math.floor(index / 6);
const col = index % 6;
paperBall.style.marginLeft = `${col * 15}px`;
paperBall.style.marginBottom = `${row * 10}px`;
return paperBall;
}
// 更新纸团数量显示
updatePaperCount() {
const paperCount = document.getElementById("paperCount");
paperCount.textContent = this.notes.length;
}
// 显示编辑模态框
showEditModal() {
const modal = document.getElementById("noteModal");
const noteContent = document.getElementById("noteContentDisplay");
const notePaper = document.getElementById("notePaper");
const selectedPaperBall = document.getElementById("selectedPaperBall");
const modalActions = document.getElementById("modalActions");
// 清空内容并设置为编辑模式
noteContent.value = "";
noteContent.focus();
// 显示模态框
modal.style.display = "block";
// 重置动画
notePaper.style.display = "none";
selectedPaperBall.style.display = "block";
modalActions.style.display = "flex";
// 纸团打开动画
setTimeout(() => {
selectedPaperBall.style.display = "none";
notePaper.style.display = "block";
notePaper.style.animation = "paperUnfold 1s ease-out forwards";
}, 500);
}
// 保存编辑的笔记
saveEditedNote() {
const noteContent = document
.getElementById("noteContentDisplay")
.value.trim();
if (!noteContent) {
this.showNotification("请填写纸条内容!", "error");
return;
}
this.addNote(noteContent);
// 关闭模态框
this.hideModal();
}
// 隐藏模态框
hideModal() {
const modal = document.getElementById("noteModal");
const notePaper = document.getElementById("notePaper");
const selectedPaperBall = document.getElementById("selectedPaperBall");
const modalActions = document.getElementById("modalActions");
// 隐藏操作按钮
modalActions.style.display = "none";
// 应用纸张收起动画
notePaper.style.animation = "paperFold 1s ease-in forwards";
// 动画完成后显示纸团并关闭模态框
setTimeout(() => {
selectedPaperBall.style.display = "block";
notePaper.style.display = "none";
// 关闭模态框
setTimeout(() => {
modal.style.display = "none";
// 重置动画状态
notePaper.style.animation = "";
}, 500);
}, 1000);
}
// 新纸团放入动画
animateNewPaperBall(note) {
// 创建临时纸团用于动画
const tempPaperBall = document.createElement("div");
tempPaperBall.className = "paper-ball new-paper-ball";
document.body.appendChild(tempPaperBall);
// 纸团下落动画
tempPaperBall.style.animation = "paperDrop 1s ease-in forwards";
// 动画结束后添加到罐子里
setTimeout(() => {
this.renderPaperBalls();
tempPaperBall.remove();
}, 1000);
}
// 摇晃罐子动画
shakeJar() {
if (this.notes.length === 0) {
this.showNotification("勇气罐子还是空的,快去添加纸条吧!", "error");
return;
}
const glassJar = document.getElementById("glassJar");
glassJar.classList.add("shaking");
// 摇晃结束后显示随机纸条
setTimeout(() => {
glassJar.classList.remove("shaking");
this.showRandomNote();
}, 1500);
}
// 显示随机纸条
showRandomNote() {
const note = this.getRandomNote();
if (!note) return;
const modal = document.getElementById("noteModal");
const noteContent = document.getElementById("noteContentDisplay");
const notePaper = document.getElementById("notePaper");
const selectedPaperBall = document.getElementById("selectedPaperBall");
const modalActions = document.getElementById("modalActions");
// 显示现有内容
noteContent.value = note.content;
// 显示模态框(查看模式)
modal.style.display = "block";
// 重置动画
notePaper.style.display = "none";
selectedPaperBall.style.display = "block";
modalActions.style.display = "none";
// 纸团打开动画
setTimeout(() => {
selectedPaperBall.style.display = "none";
notePaper.style.display = "block";
notePaper.style.animation = "paperUnfold 1s ease-out forwards";
}, 500);
}
// 显示通知
showNotification(message, type = "success") {
// 创建通知元素
const notification = document.createElement("div");
notification.className = `notification ${type}`;
notification.textContent = message;
// 添加到页面
document.body.appendChild(notification);
// 3秒后自动移除
setTimeout(() => {
notification.style.animation = "slideOutRight 0.3s ease";
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 300);
}, 3000);
}
}
// 页面加载完成后初始化
window.addEventListener("DOMContentLoaded", () => {
// 初始化勇气罐子应用
new CourageJarManager();
});