一个随鼠标点击移动的小球

一个随鼠标点击移动的小球

<p>随便点击某处来移动球</p>
<div id="foo" class="ball"></div>
const f = document.getElementById("foo");
document.addEventListener(
  "click",
  (ev) => {
    f.style.transform = `translateY(${ev.clientY - 25}px)`;
    f.style.transform += `translateX(${ev.clientX - 25}px)`;
  },
  false,
);
.ball {
  border-radius: 25px;
  width: 50px;
  height: 50px;
  background: #c00;
  position: absolute;
  top: 0;
  left: 0;
  transition: transform 1s;
}

 

posted @ 2023-08-25 15:57  上官靖宇  阅读(12)  评论(0编辑  收藏  举报