en.javascript.info/2-ui/3-event-details/4-mouse-drag-and-drop/ball3.view/index.html
Ilya Kantor ab9ab64bd5 up
2017-03-21 14:41:49 +03:00

52 lines
1.1 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="height: 200px">
<p>Drag the ball.</p>
<img src="https://js.cx/clipart/ball.svg" style="cursor:pointer" width="40" height="40" id="ball">
<script>
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);
function moveAt(pageX, pageY) {
ball.style.left = pageX - shiftX + 'px';
ball.style.top = pageY - shiftY + 'px';
}
function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}
document.addEventListener('mousemove', onMouseMove);
ball.onmouseup = function() {
document.removeEventListener('mousemove', onMouseMove);
ball.onmouseup = null;
};
};
ball.ondragstart = function() {
return false;
};
</script>
</body>
</html>