In: Computer Science
Write an HTML which will create two moving balls (use div with border-radius to create these balls). The red ball starts its movement from top left corner and the green ball starts its movement from top right corner. Assuming the red ball take diagonal direction and the red ball takes anti-diagonal direction downward. When ball meets in the middle of the screen, the red ball will on top of the green ball (hint: use z-inedx). Make sure these two balls will meet in the center of your screen when during their movements. .
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Some title </title>
<style>
.balls{
border-radius: 50%;
position: fixed;
width: 50px;
height: 50px;
margin-top: 50px;
animation-iteration-count: infinite;
}
#ball1 {
left: 0%;
background : red;
animation-name: redball;
animation-duration: 8s;
animation-iteration-count: infinite;
}
#ball2 {
left: 100%;
background:green;
animation-name: greenball;
animation-duration: 8s;
animation-iteration-count: infinite;
z-index:-1;
}
@keyframes redball {
0% {
top: 0%;
left: 0%;
}
100% {
top : 90%;
left : 90%;
}
}
@keyframes greenball {
0% {
top : 0%;
left : 90%;
}
100% {
top : 90%;
left : 0%;
}
}
</style>
</head>
<body>
<div class="balls" id="ball1"></div>
<div class="balls" id="ball2"></div>
</body>
</html>
EXPLANATION:
As asked in the question above, the red ball starts its movement from the top left in the diagonal direction and the green ball starts its movement from the top right in the anti-diagonal direction.In the centre of the screen when both the balls meet and they overlap each other, red ball overlaps and be on top of the green ball.
This process is done by the following procedure:-
- open <!DOCTYPE html> , <html> , <head> , <style> tags as we do in our html codes.
- created a class named balls and added the properties that should be included in both the balls.(properties which are common for both).
NOTE : class name is prefixed by " . " by convention.
Added some common properties like width of the ball,height of the ball , radius of the ball
- created two different id's for ball1 and ball2 .
Added some specific properties for both the balls in their id's which shall be given separately like colour of the ball,animation name for the balls.
Note: id's are prefixed by "#" by convention.
- In the #ball2, the z-index is assigned to -1 so that #ball2 is overlaped by #ball1.
An element with greater z-index is always in front of an element with a lower z-index.(so we gave our #ball2 the z-index of -1 which is lower)
-Here is the main thing due to which our balls are moving on the screen: KEYFRAMES
In keyframes we add the position of the ball i.e where should it be on the the screen.
At starting( 0% ) the red ball should be on the top left corner so we made it left:0%; top:0%;
At the end(100%), the red ball should reach the bottom right corner so we made it left:90%; top: 90%;
-Likewise we design our keyframes for green ball also.
-In between the two balls pass by each other and the green ball is overlaped by red ball(as we gave z-index for green ball as -1).
- We open our body tags and div elements which results in the presence of the balls.
Thank you, have a great day!