cevilio

Animation using HTML, CSS and JS

Jul 6th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <title>Animation with Javascript, HMTL and CSS</title>
  4.  
  5.         <style>
  6.             .container {
  7.                 width: 90%;
  8.                 height: 90%;
  9.                 background-color: azure;
  10.                 margin: auto;
  11.                 margin-top: 5%;
  12.             }
  13.  
  14.             .object {
  15.                 width: 23px;
  16.                 height: 23px;
  17.                 background-color: coral;
  18.                 position: relative;
  19.                 left: 23;
  20.             }
  21.         </style>
  22.     </head>
  23.     <body>
  24.         <div class="container" id="container">
  25.             <div class="object" id="object" onclick="move()"></div>
  26.         </div>
  27.         <span id="tracker"></span>
  28.     </body>
  29.  
  30.     <script>
  31.         var pos = 23;
  32.         function animate() {
  33.                 right_edge = document.getElementById("container").offsetWidth;
  34.                 if (pos > right_edge) {
  35.                     pos -= 15;
  36.                 } else {
  37.                     pos += 15;
  38.                 }
  39.                 document.getElementById("object").style.left = pos;
  40.                 document.getElementById("tracker").innerHTML = pos;
  41.         }
  42.  
  43.        
  44.        
  45.         function move() {
  46.             setInterval(animate, 1000);
  47.            
  48.         }
  49.     </script>
  50.  
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment