ivana_andreevska

Kolokviumska

Jan 11th, 2022
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6. </head>
  7.  
  8. <body>
  9. <input type="button" value="Odi na levo" onclick="moveLeft()">
  10. <input type="button" value="Odi na desno" onclick="moveRight()">
  11.  
  12.  
  13. <script>
  14.     var body = document.getElementsByTagName('body')[0];
  15.     index = 0;
  16.     body.onload = function () {
  17.         data = '[{"background-color":"red","width": "100px","height" : "100px"},{"background-color":"green","width" : "50px","height" : "100px"},{"background-color":"blue","width" : "100px","height" : "50px"}]';
  18.         data = JSON.parse(data);
  19.  
  20.         var element = document.createElement('div');
  21.         element.style.backgroundColor = data[0]['background-color'];
  22.         element.style.width = data[0]['width'];
  23.         element.style.height = data[0]['height'];
  24.         element.style.position = 'absolute';
  25.         element['left_position'] = 0;
  26.         document.body.appendChild(element);
  27.     }
  28.  
  29.     function moveLeft() {
  30.         inter = setInterval(function move() {
  31.             var object = document.getElementsByTagName('div')[0];
  32.             var position = -100;
  33.  
  34.             object['left_position'] -= 10;
  35.             object.style.left = object['left_position'] + "px";
  36.  
  37.             if (object['left_position'] < position) {
  38.                 clearInterval(inter);
  39.                 index++;
  40.                 if(index>=data.length)
  41.                 {
  42.                     index=0;
  43.                 }
  44.  
  45.                 object['left_position'] = 1000;
  46.                 object.style.backgroundColor = data[index]['background-color'];
  47.                 object.style.width = data[index]['width'];
  48.                 object.style.height = data[index]['height'];
  49.  
  50.                 inter2=setInterval(function ()
  51.                 {
  52.                     object['left_position'] -= 10;
  53.                     object.style.left = object['left_position'] + "px";
  54.  
  55.                     if (object['left_position'] < 0)
  56.                         clearInterval(inter2);
  57.                 },20)
  58.             }
  59.         }, 100);
  60.     }
  61.  
  62.     function moveRight() {
  63.         inter = setInterval(function move() {
  64.             var object = document.getElementsByTagName('div')[0];
  65.             var position = 100;
  66.  
  67.             object['left_position'] += 10;
  68.             object.style.left = object['left_position'] + "px";
  69.  
  70.             if (object['left_position'] > position) {
  71.                 clearInterval(inter);
  72.                 index--;
  73.                 if(index<0)
  74.                 {
  75.                     index=data.length-1;
  76.                 }
  77.  
  78.                 object['left_position'] = -100;
  79.                 object.style.backgroundColor = data[index]['background-color'];
  80.                 object.style.width = data[index]['width'];
  81.                 object.style.height = data[index]['height'];
  82.  
  83.                 inter2=setInterval(function ()
  84.                 {
  85.                     object['left_position'] += 10;
  86.                     object.style.left = object['left_position'] + "px";
  87.  
  88.                     if (object['left_position'] >0)
  89.                         clearInterval(inter2);
  90.                 },100)
  91.             }
  92.         }, 20);
  93.     }
  94. </script>
  95.  
  96.  
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment