Advertisement
ZazoTazo

Ball

Dec 7th, 2020
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Task 2</title>
  5.         <style>
  6.             body{
  7.                 overflow: hidden;
  8.                 margin: 0px;
  9.             }
  10.         </style>
  11.     </head>
  12.     <body>
  13.         <img id = "ball" src = "pics/ball.png" width = 100 height = 100>
  14.             <img id = "field" src = "pics/field.jpg" width = 1366 height = 768/>
  15.         <script type="text/javascript">
  16.         var ball = document.getElementById("ball");
  17.         ball.style.position = "absolute";
  18.         ball.style.left = "30px";
  19.         ball.style.top = "30px";
  20.         //var h = 25;
  21.         //var w = 25;
  22.         var h = Math.floor((Math.random()*26)+20); // choose random numbers
  23.         var w = Math.floor((Math.random()*26)+20); // choose random numbers
  24.             setTimeout(ballMove, 50);
  25.             function ballMove(){
  26.                 ball.style.left = parseInt(ball.style.left) + w + "px";
  27.                 ball.style.top = parseInt(ball.style.top) + h + "px";
  28.                 if(parseInt(ball.style.left) > window.innerWidth || parseInt(ball.style.left) < 0){
  29.                    w *= -1;
  30.                }
  31.                if(parseInt(ball.style.top) > window.innerHeight || parseInt(ball.style.top) < 0){
  32.                    h *= -1;
  33.                }
  34.                setTimeout(ballMove, 50);
  35.            }
  36.        </script>
  37.     </body>
  38. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement