52 lines
1.1 KiB
HTML
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>
|