-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag-2.html
43 lines (42 loc) · 982 Bytes
/
drag-2.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.box {
margin: 200px;
width: 200px;
height: 200px;
background-color: skyblue;
position: absolute;
}
</style>
</head>
<body>
<div class="box">
</div>
<script type="text/javascript">
let box = document.getElementsByClassName('box')[0];
let x = 0 , y = 0;
box.onmousedown = function(e) {
let { clientX, clientY } = e;
let beginX = clientX - x;
let beginY = clientY - y;
let offsetX = 0, offsetY = 0;
document.onmousemove = function(e) {
let { clientX:moveX, clientY:moveY } = e;
offsetX = moveX - beginX;
offsetY = moveY - beginY;
box.style.WebkitTransform = `translate(${offsetX}px,${offsetY}px)`;
}
document.onmouseup = function() {
console.log(offsetX,offsetY)
x = offsetX;
y = offsetY;
document.onmousemove = null;
document.onmouseup = null;
}
}
</script>
</body>
</html>