Advertisement
Guest User

Untitled

a guest
May 24th, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // code voor bewegen van auto
  2. // gebruik: getElementById, setInterval, setAttribute of element.style
  3. // geavanceerd: gebruik window.innerWidth
  4.  
  5. window.onload = init;
  6.  
  7.     var afstand1 = 0;
  8.     var afstand2 = 0;
  9.     var afstand3 = 0;
  10.  
  11. var animate, left=0, imgObj=null;
  12. var auto1Status = false;
  13. var auto2Status = false;
  14. var auto3Status = false;
  15. var i = 1;
  16.  
  17.  
  18. function init(){
  19.  
  20.     auto1 = document.getElementById('auto1');
  21.     auto1.style.position= 'absolute';
  22.     auto1.style.top = '50px';
  23.     auto1.style.left = '0px';
  24.     auto1.style.visibility='hidden';
  25.  
  26.     auto2 = document.getElementById('auto2');
  27.     auto2.style.position= 'absolute';
  28.     auto2.style.top = '200px';
  29.     auto2.style.left = '0px';
  30.     auto2.style.visibility='hidden';
  31.  
  32.     auto3 = document.getElementById('auto3');
  33.     auto3.style.position= 'absolute';
  34.     auto3.style.top = '350px';
  35.     auto3.style.left = '0px';
  36.     auto3.style.visibility='hidden';
  37.  
  38.     race = document.getElementById('racebaan');
  39.     race.style.position = 'absolute';
  40.     race.style.left = 0;
  41.     race.style.backgroundRepeat = "repeat-x";
  42.  
  43.     moveRight1();
  44.     moveRight2();
  45.     moveRight3();
  46.  
  47.     moveRace();
  48.    
  49.  
  50. }
  51.  
  52.  
  53. function moveRight1(){
  54.     left1 = parseInt(auto1.style.left, 10);
  55.     var snelheid1 = Math.random()*10;
  56.    
  57.     if (left1 < window.innerWidth - 350) {
  58.         auto1.style.left = (left1 + snelheid1) + 'px';
  59.         auto1.style.visibility='visible';
  60.  
  61.        
  62.         animate1 = setTimeout(function(){moveRight1();},20); // call moveRight in 20msec
  63.  
  64.        
  65.     } else {
  66.         stop1();
  67.         console.log("auto 1 is " + i + "e gefinished!");
  68.         i++;
  69.         return;
  70.     }
  71.    
  72.     //f();
  73. }
  74.  
  75. function moveRight2(){
  76.     left2 = parseInt(auto2.style.left, 10);
  77.     var snelheid2 = Math.random()*10;
  78.  
  79.     if (left2 < window.innerWidth - 350) {
  80.         auto2.style.left = (left2 + snelheid2) + 'px';
  81.         auto2.style.visibility='visible';
  82.  
  83.         animate2 = setTimeout(function(){moveRight2();},20);
  84.     }
  85.     else
  86.     {
  87.         stop2();
  88.         console.log("auto 2 is " + i + "e gefinished!");
  89.         i++;
  90.         return;
  91.     }
  92. }
  93.  
  94. function moveRight3(){
  95.     left3 = parseInt(auto3.style.left, 10);
  96.     var snelheid3 = Math.random()*10;
  97.  
  98.     if (left3 < window.innerWidth - 350) {
  99.         auto3.style.left = (left3 + snelheid3) + 'px';
  100.         auto3.style.visibility='visible';
  101.  
  102.         animate3 = setTimeout(function(){moveRight3();},20);
  103.  
  104.     }
  105.     else
  106.     {
  107.         stop3();
  108.         console.log("auto 3 is " + i + "e gefinished!");
  109.         i++;
  110.         return;
  111.     }
  112. }
  113.  
  114.  
  115.  
  116. function moveRace(){
  117.     left = parseInt(race.style.left, 10);
  118.     var snelheidRace = 5;
  119.    
  120.     if (left < window.innerWidth) {
  121.         race.style.left = (left - snelheidRace) + 'px';
  122.         race.style.visibility='visible';
  123.  
  124.        
  125.         animateRace = setTimeout(function(){moveRace();},20); // call moveRight in 20msec
  126.  
  127.         //stopanimate = setTimeout(moveRight,20);
  128.     } else {
  129.        
  130.         return;
  131.     }
  132.    
  133.     //f();
  134. }
  135.  
  136.  
  137.  
  138.  
  139. function stop1(){
  140.    clearTimeout(animate1);
  141. }
  142.  
  143. function stop2(){
  144.    clearTimeout(animate2);
  145. }
  146.  
  147. function stop3(){
  148.    clearTimeout(animate3);
  149. }
  150.  
  151. window.onload = function() {init();};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement