-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCompare.html
More file actions
373 lines (329 loc) · 11.5 KB
/
Copy pathImageCompare.html
File metadata and controls
373 lines (329 loc) · 11.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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片对比工具</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #111;
overflow: hidden;
height: 100vh;
}
.comparison-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: #000;
}
.image-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.image-before, .image-after {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
}
.image-before {
z-index: 1;
clip-path: polygon(0 0, 50% 0, 50% 100%, 0 100%);
}
.slider {
position: absolute;
top: 0;
bottom: 0;
width: 4px;
background: white;
left: 50%;
transform: translateX(-50%);
z-index: 2;
cursor: ew-resize;
}
.slider-handle {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border-radius: 50%;
background: white;
display: flex;
align-items: center;
justify-content: center;
}
.controls {
position: fixed;
top: 20px;
right: 20px;
z-index: 10;
display: flex;
gap: 10px;
}
.btn {
width: 44px;
height: 44px;
border-radius: 50%;
background: rgba(0, 0, 0, 0.6);
border: 1px solid rgba(255, 255, 255, 0.2);
color: white;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.btn:hover {
background: rgba(0, 0, 0, 0.8);
}
.help-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.85);
z-index: 20;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
visibility: hidden;
transition: all 0.3s;
}
.help-modal.visible {
opacity: 1;
visibility: visible;
}
.help-content {
width: 90%;
max-width: 500px;
background: #1e1e1e;
border-radius: 10px;
padding: 30px;
color: #ddd;
}
.help-content h2 {
margin-bottom: 20px;
color: #fff;
}
.help-content ul {
list-style: none;
}
.help-content li {
margin-bottom: 12px;
padding-left: 10px;
border-left: 2px solid #3498db;
}
.close-help {
position: absolute;
top: 15px;
right: 15px;
background: none;
border: none;
color: #fff;
font-size: 24px;
cursor: pointer;
}
.upload-panel {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(30, 30, 30, 0.95);
border-radius: 10px;
padding: 30px;
z-index: 15;
text-align: center;
width: 90%;
max-width: 400px;
}
.upload-panel h2 {
margin-bottom: 20px;
color: #fff;
}
.upload-options {
display: flex;
gap: 15px;
justify-content: center;
}
.upload-btn {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
padding: 20px;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
cursor: pointer;
width: 130px;
}
.upload-btn:hover {
background: rgba(255, 255, 255, 0.1);
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="comparison-container">
<div class="image-wrapper">
<img id="image-before" class="image-before" alt="Before image">
<img id="image-after" class="image-after" alt="After image">
</div>
<div class="slider" id="slider">
<div class="slider-handle"></div>
</div>
<div class="controls">
<div class="btn" id="upload-left-btn">
←
</div>
<div class="btn" id="upload-right-btn">
→
</div>
<div class="btn" id="help-btn">
?
</div>
</div>
</div>
<div class="help-modal" id="help-modal">
<div class="help-content">
<h2>使用帮助</h2>
<ul>
<li>• 点击右上角按钮上传需要对比的图片</li>
<li>• 拖动中间的滑块左右移动进行对比</li>
<li>• 左侧显示"←"图片,右侧显示"→"图片</li>
</ul>
<button class="close-help" id="close-help">×</button>
</div>
</div>
<div class="upload-panel" id="upload-panel">
<h2>请上传对比图片</h2>
<div class="upload-options">
<div class="upload-btn" id="upload-left-panel">
<span>左侧图片</span>
</div>
<div class="upload-btn" id="upload-right-panel">
<span>右侧图片</span>
</div>
</div>
</div>
<input type="file" id="file-left" class="hidden" accept="image/*">
<input type="file" id="file-right" class="hidden" accept="image/*">
<script>
document.addEventListener('DOMContentLoaded', function() {
// 获取元素
const fileLeft = document.getElementById('file-left');
const fileRight = document.getElementById('file-right');
const uploadLeftBtn = document.getElementById('upload-left-btn');
const uploadRightBtn = document.getElementById('upload-right-btn');
const uploadLeftPanel = document.getElementById('upload-left-panel');
const uploadRightPanel = document.getElementById('upload-right-panel');
const uploadPanel = document.getElementById('upload-panel');
const comparisonContainer = document.querySelector('.comparison-container');
const imageBefore = document.getElementById('image-before');
const imageAfter = document.getElementById('image-after');
const slider = document.getElementById('slider');
const helpBtn = document.getElementById('help-btn');
const helpModal = document.getElementById('help-modal');
const closeHelp = document.getElementById('close-help');
// 状态变量
let isDragging = false;
let sliderPosition = 50;
// 初始化图片加载状态
let leftImageLoaded = false;
let rightImageLoaded = false;
// 设置上传按钮事件
uploadLeftBtn.addEventListener('click', () => fileLeft.click());
uploadRightBtn.addEventListener('click', () => fileRight.click());
uploadLeftPanel.addEventListener('click', () => fileLeft.click());
uploadRightPanel.addEventListener('click', () => fileRight.click());
// 文件输入变化
fileLeft.addEventListener('change', function(e) {
if (fileLeft.files && fileLeft.files[0]) {
loadImage(fileLeft.files[0], 'left');
}
});
fileRight.addEventListener('change', function(e) {
if (fileRight.files && fileRight.files[0]) {
loadImage(fileRight.files[0], 'right');
}
});
// 加载图片
function loadImage(file, side) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
if (side === 'left') {
imageBefore.src = e.target.result;
leftImageLoaded = true;
} else {
imageAfter.src = e.target.result;
rightImageLoaded = true;
}
if (leftImageLoaded && rightImageLoaded) {
uploadPanel.classList.add('hidden');
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
// 滑块拖动事件
slider.addEventListener('mousedown', function(e) {
e.preventDefault();
isDragging = true;
document.addEventListener('mousemove', moveSlider);
document.addEventListener('mouseup', stopSlider);
});
function moveSlider(e) {
if (!isDragging) return;
const containerRect = comparisonContainer.getBoundingClientRect();
const newPosition = ((e.clientX - containerRect.left) / containerRect.width) * 100;
sliderPosition = Math.max(0, Math.min(100, newPosition));
updateSliderPosition();
}
function stopSlider() {
isDragging = false;
document.removeEventListener('mousemove', moveSlider);
document.removeEventListener('mouseup', stopSlider);
}
// 更新滑块位置
function updateSliderPosition() {
slider.style.left = `${sliderPosition}%`;
imageBefore.style.clipPath = `polygon(0 0, ${sliderPosition}% 0, ${sliderPosition}% 100%, 0 100%)`;
}
// 帮助功能
helpBtn.addEventListener('click', function() {
helpModal.classList.add('visible');
});
closeHelp.addEventListener('click', function() {
helpModal.classList.remove('visible');
});
// 点击帮助模态框背景关闭
helpModal.addEventListener('click', function(e) {
if (e.target === helpModal) {
helpModal.classList.remove('visible');
}
});
});
</script>
</body>
</html>