瀏覽代碼

add zoom in zoom out button

master
azri 3 週之前
父節點
當前提交
4f899d0fd2
共有 1 個檔案被更改,包括 60 行新增5 行删除
  1. 60
    5
      src/components/ImageView/ImageView.jsx

+ 60
- 5
src/components/ImageView/ImageView.jsx 查看文件

@@ -9,6 +9,8 @@ import {
9 9
 } from "@mui/material";
10 10
 import Grid from '@mui/material/Grid2';
11 11
 import CloseIcon from "@mui/icons-material/Close";
12
+import ZoomInIcon from '@mui/icons-material/ZoomIn';
13
+import ZoomOutIcon from '@mui/icons-material/ZoomOut';
12 14
 import defaultImage from "../../assets/images/default.png";
13 15
 import { Carousel } from 'react-responsive-carousel';
14 16
 
@@ -21,6 +23,7 @@ const ImageView = () => {
21 23
 
22 24
   const [previewImage, setPreviewImage] = useState("");
23 25
   const [isZoomed, setIsZoomed] = useState(false);
26
+  const [zoomSize, setZoomSize] = useState(100)
24 27
   const [currentIndex, setCurrentIndex] = useState(0);
25 28
 
26 29
   useEffect(() => {
@@ -43,6 +46,17 @@ const ImageView = () => {
43 46
     setPreviewImage(product?.images[index]?.url)
44 47
   };
45 48
 
49
+  const onZoomIn = () => {
50
+    if (zoomSize > 310) return
51
+    setZoomSize(zoomSize + 20)
52
+  }
53
+
54
+  const onZoomOut = () => {
55
+    if (zoomSize < 40) return
56
+    setZoomSize(zoomSize - 20)
57
+  }
58
+
59
+
46 60
   return (
47 61
     <Box sx={{ flexGrow: 1 }}>
48 62
       <Grid container spacing={2}>
@@ -60,7 +74,15 @@ const ImageView = () => {
60 74
             }}
61 75
           >
62 76
 
63
-            <Carousel showStatus={false} showIndicators={false} onClickItem={() => { handleZoomToggle() }} onChange={(index) => handleSlideChange(index)} selectedItem={currentIndex}>
77
+            <Carousel
78
+              showStatus={false}
79
+              showIndicators={false}
80
+              onClickItem={() => {
81
+                handleZoomToggle()
82
+                setZoomSize(100)
83
+              }}
84
+              onChange={(index) => handleSlideChange(index)}
85
+              selectedItem={currentIndex}>
64 86
               {product?.images?.map(({ src, url }, index) => (
65 87
                 <Box
66 88
                   key={index}
@@ -106,7 +128,7 @@ const ImageView = () => {
106 128
                   setCurrentIndex(index)
107 129
                 }}
108 130
                 sx={{
109
-                  p:0.4,
131
+                  p: 0.4,
110 132
                   borderRadius: 1,
111 133
                   backgroundColor: (previewImage == url) ? "#95AAC5" : "none"
112 134
                 }}
@@ -128,12 +150,13 @@ const ImageView = () => {
128 150
       </Grid>
129 151
 
130 152
       {/* Zoom Dialog */}
131
-      <Dialog open={isZoomed} onClose={handleZoomToggle} maxWidth="lg">
153
+      <Dialog fullScreen open={isZoomed} onClose={handleZoomToggle} maxWidth="lg">
132 154
         <DialogContent
133 155
           sx={{
134 156
             position: "relative",
135 157
             overflow: "hidden", // Prevent scrolling on DialogContent
136 158
             height: "90vh", // Adjust height as needed
159
+            p: 0
137 160
           }}
138 161
         >
139 162
           {/* Fixed position close button */}
@@ -143,7 +166,11 @@ const ImageView = () => {
143 166
               height: 50,
144 167
               position: "absolute",
145 168
               top: 10,
146
-              right: 1.5,
169
+              right: {
170
+                xs: 1.5,
171
+                sm: 1.5,
172
+                md: 10
173
+              },
147 174
               zIndex: 2, // Ensure it stays above other content
148 175
             }}
149 176
           >
@@ -159,6 +186,32 @@ const ImageView = () => {
159 186
             >
160 187
               <CloseIcon />
161 188
             </IconButton>
189
+            <IconButton
190
+              onClick={onZoomIn}
191
+              sx={{
192
+                backgroundColor: "white",
193
+                color: "black",
194
+                mt: 1,
195
+                "&:hover": {
196
+                  backgroundColor: "lightgray",
197
+                },
198
+              }}
199
+            >
200
+              <ZoomInIcon />
201
+            </IconButton>
202
+            <IconButton
203
+              onClick={onZoomOut}
204
+              sx={{
205
+                backgroundColor: "white",
206
+                color: "black",
207
+                mt: 1,
208
+                "&:hover": {
209
+                  backgroundColor: "lightgray",
210
+                },
211
+              }}
212
+            >
213
+              <ZoomOutIcon />
214
+            </IconButton>
162 215
           </Box>
163 216
 
164 217
           {/* Scrollable content */}
@@ -166,13 +219,15 @@ const ImageView = () => {
166 219
             sx={{
167 220
               overflow: "auto", // Allow scrolling within this Box
168 221
               height: "100%", // Fill available height of DialogContent
222
+              display: "flex"
169 223
             }}
170 224
           >
171 225
             <img
172 226
               src={previewImage || defaultImage}
173 227
               alt="Zoomed Preview"
174 228
               style={{
175
-                width: "100%",
229
+                width: `${zoomSize}%`,
230
+                margin: "auto auto",
176 231
                 height: "auto",
177 232
               }}
178 233
             />

Loading…
取消
儲存