-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCompare.jsx
More file actions
74 lines (67 loc) · 2.22 KB
/
ImageCompare.jsx
File metadata and controls
74 lines (67 loc) · 2.22 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
import React, { useState, useRef, useEffect } from 'react';
const ImageCompare = ({ beforeImage, afterImage }) => {
const [sliderPosition, setSliderPosition] = useState(50);
const containerRef = useRef(null);
const handleMouseMove = (e) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const position = ((e.clientX - rect.left) / rect.width) * 100;
setSliderPosition(Math.min(Math.max(position, 0), 100));
};
const handleTouchMove = (e) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const position = ((e.touches[0].clientX - rect.left) / rect.width) * 100;
setSliderPosition(Math.min(Math.max(position, 0), 100));
};
return (
<div
ref={containerRef}
className="relative w-full h-[600px] overflow-hidden cursor-ew-resize"
onMouseMove={handleMouseMove}
onTouchMove={handleTouchMove}
>
{/* 底层图片(图2) */}
<div className="absolute inset-0">
<img
src={afterImage}
alt="After"
className="w-full h-full object-cover"
/>
</div>
{/* 顶层图片(图1) */}
<div
className="absolute inset-0 overflow-hidden"
style={{ width: `${sliderPosition}%` }}
>
<img
src={beforeImage}
alt="Before"
className="w-full h-full object-cover"
/>
</div>
{/* 滑块 */}
<div
className="absolute top-0 bottom-0 w-1 bg-white cursor-ew-resize"
style={{ left: `${sliderPosition}%` }}
>
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-8 h-8 bg-white rounded-full shadow-lg flex items-center justify-center">
<svg
className="w-4 h-4 text-gray-600"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M8 7h8M8 12h8M8 17h8"
/>
</svg>
</div>
</div>
</div>
);
};
export default ImageCompare;