-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag.js
36 lines (27 loc) · 1008 Bytes
/
drag.js
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
ball.onmousedown = function(event) {
let shiftX = event.clientX - ball.getBoundingClientRect().left;
let shiftY = event.clientY - ball.getBoundingClientRect().top;
ball.style.position = 'absolute';
ball.style.zIndex = 1000;
document.body.append(ball);
moveAt(event.pageX, event.pageY);
// 초기 이동을 고려한 좌표 (pageX, pageY)에서
// 공을 이동합니다.
function moveAt(pageX, pageY) {
ball.style.left = pageX - shiftX + 'px';
ball.style.top = pageY - shiftY + 'px';
}
function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}
// mousemove로 공을 움직입니다.
document.addEventListener('mousemove', onMouseMove);
// 공을 드롭하고, 불필요한 핸들러를 제거합니다.
ball.onmouseup = function() {
document.removeEventListener('mousemove', onMouseMove);
ball.onmouseup = null;
};
};
ball.ondragstart = function() {
return false;
};