Advertisement
SquirrelInBox

15

Nov 5th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Пятнашки</title>
  6.     <style type="text/css">
  7.        
  8.         .cell {
  9.             width: 100px;
  10.             height: 100px;
  11.             position: absolute;
  12.             background: #585858;
  13.             margin: 0;
  14.             padding-top: 0;
  15.             border: 10px;
  16.             border-color: white;
  17.             font-size: 30px;
  18.             color: white;
  19.             text-align: center;
  20.             vertical-align: middle;
  21.         }
  22.  
  23.         p {
  24.             border-color: white;
  25.             border:10px;
  26.             margin: 1px;
  27.             padding: 30px;
  28.             font-size: 30px;
  29.             color: white;
  30.             text-align: center;
  31.             vertical-align: middle;
  32.         }
  33.     </style>
  34.     <script type="text/javascript">
  35.     var first_id;
  36.     function createField() {
  37.         var variants = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].sort(function() {return 0.5 - Math.random();});
  38.  
  39.         var top = -100;
  40.         var left = 0;
  41.         for(var i = 0; i < 15; i++) {
  42.             var div = document.createElement("div");
  43.             div.className = "cell number";
  44.             div.id = i;
  45.  
  46.             if (i % 4 == 0) {
  47.                 left = 0;
  48.                 top += 102;
  49.             } else {
  50.  
  51.                 left += 102;
  52.             }
  53.             div.style.top = top + "px";
  54.             div.style.left = left + 'px';
  55.             var num = variants[i];
  56.             div.innerHTML = num;
  57.             document.body.appendChild(div);
  58.         }
  59.         var div = document.createElement("div");
  60.         div.className = "cell";
  61.         div.id = "empty";
  62.         left += 102;
  63.         div.style.top = top + "px";
  64.         div.style.left = left + "px";
  65.         div.innerHTML = '';
  66.  
  67.         document.body.appendChild(div);
  68.     }
  69.  
  70.     function moveCells(first_id, second_id) {
  71.         var first = document.getElementById(first_id);
  72.         var second = document.getElementById(second_id);
  73.         if (((first.style.top != second.style.top)  && (first.style.left == second.style.left)) ||
  74.             ((first.style.top == second.style.top)  && (first.style.left != second.style.left))) {
  75.            
  76.             var tempTop = first.style.top;
  77.             first.style.top = second.style.top;
  78.             second.style.top = tempTop;
  79.  
  80.             var tempLeft = first.style.left;
  81.             first.style.left = second.style.left;
  82.             second.style.left = tempLeft;
  83.         }
  84.     }
  85.  
  86.  
  87.     function getCell(e) {
  88.         console.log(e);
  89.         var target = event.target || event.srcElement;
  90.         first_id = target.id;
  91.         // alert("first id: " + first_id);
  92.     }
  93.  
  94.     function changeCells(e) {
  95.         var target = event.target || event.srcElement;
  96.         var second_id = target.id;
  97.         if (first_id == "empty" || second_id == "empty") {
  98.             moveCells(first_id, second_id);
  99.         }
  100.         first_id = '';
  101.     }
  102.  
  103.     function dbChangeCells(e) {
  104.         var target = event.target || event.srcElement;
  105.         var second_id = target.id;
  106.         if (second_id != "empty") {
  107.             moveCells("empty", second_id);
  108.         }
  109.     }
  110.     </script>
  111. </head>
  112. <body onload="createField()", onmousedown="getCell(evt=event)", onmouseup="changeCells(evt=event)", ondblclick="dbChangeCells(evt=event)", onselectstart="return false">
  113.  
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement