Skip to content

Commit a6bc7c0

Browse files
committed
feat: 瀑布流
1 parent 9aced1b commit a6bc7c0

File tree

4 files changed

+131
-18
lines changed

4 files changed

+131
-18
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 简介
2+
3+
对上传的多张图片做组合拼接的
4+
5+
# 技术栈
6+
7+
Vite + React

eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default tseslint.config(
2020
rules: {
2121
...reactHooks.configs.recommended.rules,
2222
"@typescript-eslint/ban-ts-comment": "off",
23+
"@typescript-eslint/no-unused-expressions": "off",
2324
"react-refresh/only-export-components": [
2425
"warn",
2526
{ allowConstantExport: true },

src/App.tsx

+64-18
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,86 @@ import a from "./assets/testImage/006bAzwAgy1hpi6dijal4j35ie2q2b2c.jpg";
55
import b from "./assets/testImage/0074J156ly1hplq1w0odrj31vw3pce82.jpg";
66
import c from "./assets/testImage/008rZrLhgy1hr0jn3g09aj31mz17r7wh.jpg";
77
import d from "./assets/testImage/20240613-121606.jpeg";
8+
import Drawer from "./components/Drawer.js";
89

9-
const IMG_LIST = [a, b, c, d, a, b, c, d, a, b, c, d] as string[];
10+
const IMG_LIST = [
11+
a,
12+
b,
13+
c,
14+
d,
15+
a,
16+
b,
17+
c,
18+
d,
19+
a,
20+
b,
21+
c,
22+
d,
23+
a,
24+
b,
25+
d,
26+
c,
27+
d,
28+
c,
29+
d,
30+
a,
31+
b,
32+
c,
33+
a,
34+
b,
35+
36+
b,
37+
b,
38+
c,
39+
a,
40+
b,
41+
b,
42+
43+
b,
44+
b,
45+
c,
46+
a,
47+
b,
48+
b,
49+
c,
50+
a,
51+
d,
52+
] as string[];
1053

1154
function App() {
1255
const imgContainer = useRef(null);
56+
let timer: null;
1357
useEffect(() => {
1458
if (imgContainer.current) {
1559
setTimeout(() => {
1660
// @ts-ignore
1761
new Packery(imgContainer.current, {
1862
itemSelector: ".imgItem",
19-
gutter: 0,
2063
percentPosition: true,
64+
horizontal: true,
2165
});
66+
2267
}, 100);
2368
}
69+
return () => {
70+
timer && clearTimeout(timer);
71+
};
2472
}, [imgContainer.current]);
2573
return (
26-
<div className="w-screen h-screen bg-red-50 flex justify-center items-center overflow-hidden">
27-
<div className="card bg-white w-10/12 h-5/6 shadow-xl">
28-
<div
29-
className="card-body relative w-full h-auto overflow-hidden imageContainer"
30-
ref={imgContainer}
31-
>
32-
{IMG_LIST.map((item) => {
33-
return (
34-
<img
35-
src={item}
36-
key={item + Math.random() * 1000}
37-
className="absolute h-1/4 object-cover imgItem"
38-
/>
39-
);
40-
})}
41-
</div>
74+
<div className="w-screen h-screen flex justify-center items-center overflow-hidden">
75+
<div className="absolute top-2 left-2 z-50">
76+
<Drawer />
77+
</div>
78+
<div ref={imgContainer} className="w-full h-full">
79+
{IMG_LIST.map((item) => {
80+
return (
81+
<img
82+
src={item}
83+
key={item + Math.random() * 1000}
84+
className={`absolute object-cover h-1/5 w-auto imgItem`}
85+
/>
86+
);
87+
})}
4288
</div>
4389
</div>
4490
);

src/components/Drawer.tsx

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useEffect, useRef, useState } from "react";
2+
3+
const Drawer = () => {
4+
const [FileList, setFileList] = useState<File[]>([]);
5+
const FileRef = useRef(null);
6+
useEffect(() => {
7+
if (FileRef.current) {
8+
// @ts-ignore
9+
FileRef.current.value = "";
10+
}
11+
}, [FileList]);
12+
return (
13+
<div className="drawer">
14+
<input id="my-drawer" type="checkbox" className="drawer-toggle" />
15+
<div className="drawer-content">
16+
<label htmlFor="my-drawer" className="btn btn-ghost drawer-button">
17+
上传图片
18+
</label>
19+
</div>
20+
<div className="drawer-side">
21+
<label
22+
htmlFor="my-drawer"
23+
aria-label="close sidebar"
24+
className="drawer-overlay"
25+
/>
26+
<ul className="menu bg-base-200 text-base-content min-h-full w-80 p-4">
27+
<li>
28+
<input
29+
ref={FileRef}
30+
onChange={(e) => {
31+
e.target.files?.[0] &&
32+
setFileList((old) => {
33+
return e.target.files?.[0]
34+
? [...old, e.target.files?.[0]]
35+
: old;
36+
});
37+
}}
38+
type="file"
39+
className="file-input file-input-ghost w-full max-w-xs"
40+
/>
41+
</li>
42+
<li className="m-2 inline-flex justify-between">
43+
<div className="btn">生成</div>
44+
</li>
45+
{FileList.map((item) => {
46+
const url = URL.createObjectURL(item);
47+
return (
48+
<li key={url} className="m-2">
49+
<img src={url} />
50+
</li>
51+
);
52+
})}
53+
</ul>
54+
</div>
55+
</div>
56+
);
57+
};
58+
59+
export default Drawer;

0 commit comments

Comments
 (0)