<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
background-color: pink;
}
div{
position: absolute;
top:100px;
left:100px;
width: 50px;
height: 50px;
background: radial-gradient(white 0%, red 100%);
border-radius: 50%;
}
</style>
</head>
<body>
<div></div>
<script>
var oDiv = document.getElementsByTagName('div')[0];
var h, w;
window.onresize = (function s() {
w = window.innerWidth - oDiv.clientWidth;
h = window.innerHeight - oDiv.clientHeight;
return s;
})();
var vx = 3;
var vy = 3;
var Lx, Ly;
function ani(){
Lx = oDiv.offsetLeft + vx;
Ly = oDiv.offsetTop + vy;
if(Lx>w){
Lx = w;
vx = -vx;
}
if(Lx<=0){
Lx = 0;
vx = -vx;
}
if(Ly>h){
Ly = h;
vy = -vy;
}
if(Ly<0){
Ly = 0;
vy = -vy;
}
oDiv.style.left = Lx + vx + 'px';
oDiv.style.top = Ly + vy + 'px';
}
setInterval(ani, 1000/60);
</script>
</body>
</html>