-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
194 lines (174 loc) · 6.57 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text Interception App</title>
<link rel="stylesheet" href="window/styles/trash.css">
<link rel="stylesheet" href="window/styles/main.css">
<link rel="stylesheet" href="window/styles/error.css">
</head>
<body>
<div class="app-container">
<div class="top-nav">
<div class="nav-item">文本</div>
<div class="nav-item">文档</div>
<div class="nav-item">图片</div>
</div>
<div class="drop-zone">
<label for="file-input">点击或拖拽图片到此区域</label>
<input type="file" id="file-input" accept="image/*">
<div id="error-message" class="error-message"></div>
<div id="image-container">
<img id="image-preview">
</div>
<button class="delete-button">
<div class="trash-lid"></div>
<div class="trash-container"></div>
<div class="trash-line"></div>
<div class="trash-line"></div>
<div class="trash-line"></div>
</button>
</div>
</div>
<script>
const dropZone = document.querySelector('.drop-zone');
const fileInput = document.getElementById('file-input');
const imageContainer = document.getElementById('image-container');
const imagePreview = document.getElementById('image-preview');
const deleteButton = document.querySelector('.delete-button');
const errorMessage = document.getElementById('error-message');
let errorTimeout; // 用于保存计时器ID的变量
// 缩放比例
let scale = 1;
let posX = 0;
let posY = 0;
let dragging = false;
// 图片缩放
imagePreview.addEventListener('wheel', (event) => {
event.preventDefault();
scale += event.deltaY * -0.0025;
// 限制缩放范围
scale = Math.min(Math.max(.1, scale), 10);
imagePreview.style.transform = `translate(${posX}px, ${posY}px) scale(${scale})`;
});
// 开始拖动
imagePreview.addEventListener('mousedown', (event) => {
event.preventDefault();
dragging = true;
imagePreview.style.cursor = 'grabbing'; // 鼠标按下时改为抓取光标
let startX = event.clientX - posX;
let startY = event.clientY - posY;
function onMouseMove(e) {
if (dragging) {
posX = e.clientX - startX;
posY = e.clientY - startY;
imagePreview.style.transform = `translate(${posX}px, ${posY}px) scale(${scale})`;
}
}
function onMouseUp() {
dragging = false;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
imagePreview.style.cursor = 'grab'; // 鼠标释放时恢复原样
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
});
// 文件选择事件
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
handleFileSelect(file);
});
// 处理文件选择
function handleFileSelect(file) {
if (file) {
// 在有error计时器清除
clearTimeout(errorTimeout)
if (!file.type.match('image/png|image/jpeg')) {
errorMessage.textContent = '文件类型不支持';
errorMessage.classList.add('show');
errorTimeout = setTimeout(() => { errorMessage.classList.remove('show'); }, 3000); // 3秒后消失
return;
}
if (file.size > 10 * 1024 * 1024) {
errorMessage.textContent = '文件过大,不能超过10MB';
errorMessage.classList.add('show');
errorTimeout = setTimeout(() => { errorMessage.classList.remove('show'); }, 3000); // 3秒后消失
return;
}
// 如果文件有效,清除任何现有的错误消息
errorMessage.classList.remove('show');
// 清除计时器
clearTimeout(errorTimeout)
const reader = new FileReader();
reader.addEventListener('load', () => {
imagePreview.src = reader.result;
imageContainer.style.display = 'block';
// 显示图片预览时隐藏 label
dropZone.querySelector('label').style.display = 'None'
});
reader.readAsDataURL(file);
}
}
// 以下是拖放事件的处理
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
handleFileSelect(file);
});
// 粘贴事件处理
document.addEventListener('paste', (e) => {
const items = e.clipboardData && e.clipboardData.items;
let image = null;
if (items) {
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') >= 0) {
image = items[i].getAsFile();
break;
}
}
if (image) {
handleFileSelect(image);
}
}
});
// 点击删除按钮
deleteButton.addEventListener('click', () => {
imagePreview.src = ''; // 移除图片源
imageContainer.style.display = 'none'; // 隐藏图片容器
deleteButton.style.display = 'none'; // 隐藏删除按钮
dropZone.querySelector('label').style.display = 'block'; // 显示上传标签
fileInput.value = ''; // 重置文件输入
scale = 1; // 重置缩放比例为1
posX = 0; // 重置X位置为0
posY = 0; // 重置Y位置为0
dragging = false; // 停止拖动状态
imagePreview.style.transform = 'translate(0px, 0px) scale(1)'; // 重置变换效果
});
// imagePreview 引入划入事件
imageContainer.addEventListener('mouseover', () => {
deleteButton.style.display = 'block'
})
imageContainer.addEventListener('mouseout', () => {
if (!dragging) { // 当不在拖动状态时才隐藏删除按钮
deleteButton.style.display = 'none';
}
})
// 需要更新删除按钮的事件监听器,确保它不会隐藏自己
deleteButton.addEventListener('mouseover', () => {
deleteButton.style.display = 'block';
});
deleteButton.addEventListener('mouseout', () => {
deleteButton.style.display = 'none';
});
</script>
</body>
</html>