Advertisement
Guest User

Updated

a guest
Nov 7th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.52 KB | None | 0 0
  1. <!DOCTYPE html>
  2.     <html>
  3.     <head>
  4.     <style>
  5.     body {
  6.         background-image: url("Images/desert.png");
  7.         background-repeat:no-repeat;
  8.         background-position:center;
  9.         background-attachment:fixed;
  10.          
  11.         -webkit-background-size: cover;
  12.         -moz-background-size: cover;
  13.         -o-background-size: cover;
  14.         background-size: cover;
  15.     }
  16.      
  17.     #cowboy {
  18.         position:absolute;
  19.         left:100px;
  20.         top: 700px;
  21.     }
  22.     </style>
  23.     <script>
  24.     var pic1=new Image()
  25.     pic1.src="./Images/run1.png"
  26.     var pic2=new Image()
  27.     pic2.src="./Images/run2.png"
  28.     var pic3=new Image()
  29.     pic3.src="./Images/run3.png"
  30.     var pic4=new Image()
  31.     pic4.src="./Images/run4.png"
  32.      
  33.     function getValue() {
  34.         var dest_x = document.getElementById('txtChar').value;
  35.         alert(dest_x);
  36.     }
  37.     </script>
  38.     </head>
  39.     <body>
  40.      
  41.     <form name="form" action="" method="GET">
  42.         <h2>Enter the the distance Joe the Cowboy shall move.</h2>
  43.         <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar" />
  44.         <input id="btnMove" type="button" value="GO!" onclick="var t=setInterval('moveRight()', 80); getValue();" />
  45.     </form>
  46.     <div id="cowboy" >
  47.         <img src="Images/run1.png" name="animation" />
  48.     </div>
  49.      
  50.     <script>
  51.     var counter=1
  52.     function animate() {
  53.         if (!document.images) return;
  54.         document.images.animation.src= "pic" + counter + ".src";
  55.         if (counter < 4) {
  56.             counter++;
  57.         } else {
  58.             counter=1;
  59.         }
  60.         setTimeout("animate()",100)
  61.     }
  62.                  
  63.    function isNumberKey(evt) {
  64.         var charCode = (evt.which) ? evt.which : event.keyCode
  65.        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
  66.               return false;
  67.         }
  68.      
  69.         return true;
  70.     }
  71.                    
  72.     var x = 100;
  73.     var y = 700;
  74.  
  75.     var dest_y = 700;  
  76.     var interval = 10; //Move 10px every initialization
  77.    
  78.     function moveRight() {
  79.         var dest_x = document.getElementById('txtChar').value;
  80.         //Keep on moving the image till the target is achieved
  81.         if(x<dest_x) x = x + interval;
  82.         if(y<dest_y) y = y + interval;
  83.  
  84.         //Move the image to the new location
  85.         document.getElementById("cowboy").style.top  = y+'px';
  86.         document.getElementById("cowboy").style.left = x+'px';
  87.  
  88.         if ((x+interval < dest_x) && (y+interval < dest_y)) {
  89.             //Keep on calling this function every 100 microsecond
  90.             //      till the target location is reached
  91.             window.setTimeout('moveRight()',100);
  92.         }
  93.    }
  94.    
  95.    animate()
  96.    </script>
  97.     </body>
  98.     </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement