Skip to content

Commit adc6470

Browse files
committed
拖拽的基本功能
1 parent 40d4fbf commit adc6470

File tree

4 files changed

+163
-5
lines changed

4 files changed

+163
-5
lines changed

src/assets/style/reset.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
@import "~normalize.css";
2+
3+
.body {
4+
width: 100%;
5+
height: 100vh;
6+
}

src/pages/Login/style.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export const LoginWrapper = styled.div`
77
text-align: center;
88
line-height: 50px;
99
margin-left: 50%;
10-
margin-top: 50vh;
11-
transform: translateX(-50%);
10+
margin-top: 50%;
11+
transform: translateX(-50%) translateY(-50%);
1212
cursor: pointer;
1313
1414
&:hover {

src/pages/Task/index.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,77 @@
1-
import React, { memo } from "react";
1+
import React, { memo, useRef, useState } from "react";
22
import { TaskWrapper } from "./style";
33

44
const Task = memo(() => {
5-
return <TaskWrapper>Task</TaskWrapper>;
5+
const [listIndex, setlistIndex] = useState(1);
6+
7+
const addInput = useRef();
8+
const studyRef = useRef();
9+
// 添加新的study
10+
const add = () => {
11+
//创建input元素(在input前创建一个空的div站位)
12+
const newInput = document.createElement("input");
13+
const blank = document.createElement("div");
14+
blank.className = "blank";
15+
addInput.current.before(newInput);
16+
newInput.before(blank);
17+
//给增加的input增加监听事件
18+
const currentInput = studyRef.current.childNodes[listIndex];
19+
let diffX;
20+
let diffY;
21+
let currentX;
22+
let currentY;
23+
//move监听函数
24+
const moveFn = (e) => {
25+
//还要减去margin的偏移量
26+
currentInput.style.left = e.clientX - 20 - diffX + "px";
27+
currentInput.style.top = e.clientY - diffY + "px";
28+
console.log(e);
29+
};
30+
//添加监听器
31+
currentInput.addEventListener(
32+
"mousedown",
33+
(e) => {
34+
//按住
35+
36+
currentInput.style.position = "absolute";
37+
38+
diffX = e.clientX - currentInput.offsetLeft;
39+
diffY = e.clientY - currentInput.offsetTop;
40+
currentX = currentInput.style.left;
41+
currentY = currentInput.style.top;
42+
43+
//moving
44+
currentInput.addEventListener("mousemove", moveFn);
45+
},
46+
false
47+
);
48+
49+
currentInput.addEventListener("mouseup", () => {
50+
//松开的逻辑
51+
// if(){
52+
53+
// }
54+
55+
currentInput.style.position = "";
56+
currentInput.style.left = currentX;
57+
currentInput.style.top = currentY;
58+
59+
currentInput.removeEventListener("mousemove", moveFn);
60+
});
61+
setlistIndex(listIndex + 2);
62+
};
63+
64+
return (
65+
<TaskWrapper>
66+
<div className="study" ref={studyRef}>
67+
<div className="add" ref={addInput} onClick={add}>
68+
+
69+
</div>
70+
</div>
71+
<div className="learning"></div>
72+
<div className="complete"></div>
73+
</TaskWrapper>
74+
);
675
});
776

877
export default Task;

src/pages/Task/style.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,87 @@
11
import styled from "styled-components";
22

3-
export const TaskWrapper = styled.div``;
3+
export const TaskWrapper = styled.div`
4+
width: 700px;
5+
min-height: 400px;
6+
display: flex;
7+
line-height: 50px;
8+
margin-left: 50%;
9+
margin-top: 30%;
10+
transform: translateX(-50%) translateY(-50%);
11+
justify-content: space-between;
12+
13+
.study,
14+
.learning,
15+
.complete {
16+
width: 200px;
17+
}
18+
19+
.study {
20+
min-height: 400px;
21+
background-color: #f5e0e1;
22+
&::before {
23+
content: "Prepare to study";
24+
background-color: #f5e0e1;
25+
display: block;
26+
margin-top: -50px;
27+
height: 30px;
28+
line-height: 30px;
29+
text-align: center;
30+
}
31+
32+
input {
33+
width: 150px;
34+
height: 30px;
35+
display: block;
36+
margin-left: 20px;
37+
}
38+
39+
.blank {
40+
width: 200px;
41+
height: 30px;
42+
}
43+
44+
.add {
45+
width: 40px;
46+
height: 40px;
47+
margin: 30px auto;
48+
text-align: center;
49+
line-height: 35px;
50+
font-size: 40px;
51+
border: 2px solid #c49b9b;
52+
border-radius: 50%;
53+
54+
& {
55+
cursor: pointer;
56+
}
57+
}
58+
}
59+
60+
.learning {
61+
background-color: #a3d09b;
62+
63+
&::before {
64+
content: "Learning";
65+
background-color: #a3d09b;
66+
display: block;
67+
margin-top: -50px;
68+
height: 30px;
69+
line-height: 30px;
70+
text-align: center;
71+
}
72+
}
73+
74+
.complete {
75+
background-color: #e2e2e2;
76+
77+
&::before {
78+
content: "Complete";
79+
background-color: #e2e2e2;
80+
display: block;
81+
margin-top: -50px;
82+
height: 30px;
83+
line-height: 30px;
84+
text-align: center;
85+
}
86+
}
87+
`;

0 commit comments

Comments
 (0)