en.javascript.info/02-ui/02-events-and-interfaces/03-obtaining-event-object/01-move-ball-field/solution/ball-move-1/index.html
Ilya Kantor f301cb744d init
2014-10-26 22:10:13 +03:00

56 lines
1.4 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<style>
#field {
width: 200px;
height: 150px;
border: 10px groove black;
background-color: #00FF00;
position: relative;
overflow: hidden;
cursor: pointer;
}
#ball {
position: absolute;
background: gray;
}
</style>
</head>
<body>
Кликните на любое место поля, чтобы мяч перелетел туда.<br>
<div id="field">
<img src="http://js.cx/clipart/ball.gif" id="ball">
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
</div>
<script>
var field = document.getElementById('field');
var ball = document.getElementById('ball');
field.onclick = function(e) {
var fieldCoords = field.getBoundingClientRect();
var fieldInnerCoords = {
top: fieldCoords.top + field.clientTop,
left: fieldCoords.left + field.clientLeft
};
ball.style.left = e.clientX - fieldInnerCoords.left + 'px';
ball.style.top = e.clientY - fieldInnerCoords.top + 'px';
};
</script>
</body>
</html>