Создать HTML-страницу с футбольным полем, которое занимает всю ширину и высоту экрана, и мячом размером 100px на 100px. Сделать так, чтобы при щелчке мышкой по полю, мяч плавно перемещался на место щелчка.
Учтите: необходимо, чтобы центр мяча останавливался именно там, где был совершен щелчок мышкой. Также предусмотрите, чтобы мяч не выходил за границы поля.
document.body.onclick = function(e) {
mydiv.style.left = (e.pageX - mydiv.offsetWidth / 2) + "px";
mydiv.style.top = (e.pageY - mydiv.offsetHeight / 2) + "px";
}
<div id=mypole style="background: no-repeat" "background-size: 100px">
<img src="http://evergreen.org.ua/wp-content/uploads/2014/03/kak-gazon-delayut-polosatum.jpg" />
</div>
<div id=mydiv style="position: absolute">
<img src="http://yoway.ru/wp-content/uploads/2015/02/street_black.jpg" />
</div>
За плавность элемента отвечает правило css transition.
Еще в примере автора не хватает кавычек в ID
Есть
id=mypole
А должно быть
id="mypole"
const lea = document.querySelector("#lea");
const ball = document.querySelector("#ball");
ball.style.left = 0 + "px";
ball.style.top = 0 + "px";
lea.onclick = function(e) {
ball.style.left = e.pageX - ball.offsetWidth / 2 + "px";
ball.style.top = e.pageY - ball.offsetHeight / 2 + "px";
if (lea.offsetHeight - 25 < e.pageY) {
ball.style.top = lea.offsetHeight - 25 + "px";
}
if (lea.offsetWidth - 25 < e.pageX) {
ball.style.left = lea.offsetWidth - 25 + "px";
}
if (e.pageY < ball.offsetHeight / 2) {
ball.style.top = 0 + "px";
}
if (e.pageX < ball.offsetWidth / 2) {
ball.style.left = 0 + "px";
}
};
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100vw;
height: 100vh;
}
#lea {
position: relative;
width: 100%;
height: 100%;
background: green;
}
#ball {
position: absolute;
width: 25px;
height: 25px;
border-radius: 50%;
background: red;
transition: all 1s;
}
<div id="lea">
<div id="ball"></div>
</div>
Продвижение своими сайтами как стратегия роста и независимости