<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#box{
height: 50px;
width: 50px;
background: pink;
border-radius: 50%;
position: absolute;
top:100px;
}
</style>
</head>
<body>
<button onclick="boxmove()">点击</button>
<div id="box">
</div>
</body>
</html>
let box = document.getElementById("box");
let time =null;
function boxmove(){
clearInterval(time);
let x=5;
let y=5;
time = setInterval(function(){
box.style.left = box.offsetLeft +x+"px";
box.style.top = box.offsetTop +y+"px";
if(box.offsetTop>=window.innerHeight-box.offsetHeight){
y*= -1;
}
if(box.offsetTop<=0){
y*= -1;
}
if(box.offsetLeft<=0){
x*= -1;
}
if(box.offsetLeft>=window.innerWidth-box.offsetWidth){
x*= -1;
}
},10)
}