-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove ball.html
44 lines (41 loc) · 909 Bytes
/
move ball.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<h2><center>Moving Ball Diagonally</center></h2>
<div id="ball" style="
z-index:5;
position:absolute;
left:0px;
top:0px;
width:50px;
height:50px;
border-radius:50%;
background:rgb(18, 158, 18)"></div>
<script>
var ball = document.getElementById('ball');
var positionX = 0;
var positionY = 0;
var velocity = 100;
var reverse = true;
// YOUR CODE
function moveBall() {
let xmin =0;
let ymin =0;
let xmax =500;
let ymax =500;
if(reverse==true){
positionX = positionX + velocity;
ball.style.left = positionX+"px";
ball.style.top = positionX+"px"
if(positionX>=xmax) {
reverse = false;
}
}else{
positionX = positionX - velocity;
ball.style.left = positionX+"px";
ball.style.top = positionX+"px"
if(positionX===xmin) {
reverse = true
}
}
}
setInterval(moveBall, 1000);
// ----------------
</script>