Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (107 loc) · 3.72 KB
/
Copy pathscript.js
File metadata and controls
130 lines (107 loc) · 3.72 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
// 生成30x30网格
function generateGrid() {
const gridContainer = document.getElementById('grid-container');
gridContainer.innerHTML = '';
// 生成900个格子
for (let i = 0; i < 900; i++) {
const cell = document.createElement('div');
cell.className = 'grid-cell';
cell.dataset.index = i;
gridContainer.appendChild(cell);
}
}
// 计算从生日到现在的月数
function calculateMonthsPassed(birthday) {
const birthDate = new Date(birthday);
const now = new Date();
// 计算年差和月差
let monthsPassed = (now.getFullYear() - birthDate.getFullYear()) * 12;
monthsPassed += now.getMonth() - birthDate.getMonth();
// 如果今天还没过生日当天,减去1个月
if (now.getDate() < birthDate.getDate()) {
monthsPassed--;
}
return Math.max(0, monthsPassed);
}
// 更新网格状态
function updateGrid(monthsPassed) {
const cells = document.querySelectorAll('.grid-cell');
cells.forEach((cell, index) => {
if (index < monthsPassed) {
cell.className = 'grid-cell past';
} else if (index === monthsPassed) {
cell.className = 'grid-cell present';
} else {
cell.className = 'grid-cell';
}
});
}
// 处理表单提交
function handleFormSubmit(e) {
e.preventDefault();
const birthday = document.getElementById('birthday').value;
const monthsPassed = calculateMonthsPassed(birthday);
updateGrid(monthsPassed);
// 保存生日到本地存储
localStorage.setItem('birthday', birthday);
// 全屏显示网格
showFullscreenGrid();
}
// 全屏显示网格
function showFullscreenGrid() {
// 添加全屏类到body
document.body.classList.add('fullscreen');
// 创建返回按钮
const backButton = document.createElement('button');
backButton.textContent = '返回';
backButton.className = 'back-btn';
backButton.onclick = hideFullscreenGrid;
document.body.appendChild(backButton);
}
// 退出全屏显示
function hideFullscreenGrid() {
// 移除全屏类
document.body.classList.remove('fullscreen');
// 移除返回按钮
const backButton = document.querySelector('.back-btn');
if (backButton) {
backButton.remove();
}
}
// 初始化函数
function init() {
// 生成初始网格
generateGrid();
// 添加表单提交事件监听
const form = document.getElementById('birthday-form');
form.addEventListener('submit', handleFormSubmit);
// 检查本地存储是否有保存的生日
const savedBirthday = localStorage.getItem('birthday');
if (savedBirthday) {
document.getElementById('birthday').value = savedBirthday;
const monthsPassed = calculateMonthsPassed(savedBirthday);
updateGrid(monthsPassed);
}
}
// 添加格子悬停效果
function addCellHoverEffects() {
const cells = document.querySelectorAll('.grid-cell');
cells.forEach((cell, index) => {
cell.addEventListener('mouseenter', () => {
// 显示格子对应的年龄和月份信息
const years = Math.floor(index / 12);
const months = index % 12;
cell.title = `${years}岁${months}个月`;
});
});
}
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
init();
// 等网格生成后添加悬停效果
setTimeout(addCellHoverEffects, 100);
});
// 监听窗口大小变化,重新添加悬停效果
window.addEventListener('resize', () => {
addCellHoverEffects();
});