models.blade.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="ru">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Пазл</title>
  6. <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap" rel="stylesheet">
  7. <style>
  8. body {
  9. font-family: Roboto;
  10. font-weight: 400;
  11. color: #202020;
  12. margin: 0px;
  13. }
  14. h2 {
  15. padding:0px 36px;
  16. }
  17. #board {
  18. margin-left: auto;
  19. margin-right: auto;
  20. border-collapse: collapse;
  21. box-sizing: border-box;
  22. outline: 1px lightgray solid;
  23. font-size: 0px;
  24. display: table;
  25. position: relative;
  26. border-radius: 0px;
  27. margin-top: 18px;
  28. width: auto;
  29. }
  30. tr,
  31. tbody {
  32. height: 50px;
  33. box-sizing: border-box;
  34. overflow: auto;
  35. width: 100%;
  36. padding: 0px;
  37. margin: 0px;
  38. border: 0px;
  39. }
  40. td {
  41. box-sizing: border-box;
  42. border: 1px solid gray;
  43. padding: 0px;
  44. margin: 0px;
  45. width: 50px;
  46. height: 50px;
  47. }
  48. .block {
  49. box-sizing: border-box;
  50. display: block;
  51. padding: 0px;
  52. margin: 0px;
  53. background-size: 450px;
  54. transition: box-shadow 0.1s ease-in-out;
  55. border: 0px white solid;
  56. position: absolute;
  57. z-index: 500;
  58. width: 50px;
  59. height: 50px;
  60. }
  61. #puzzle {
  62. width: 50%;
  63. float: right;
  64. background: #EEF;
  65. height: 100vh;
  66. position: relative;
  67. }
  68. #pieces {
  69. width: 50%;
  70. float: left;
  71. height: 100vh;
  72. position: relative;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div id="pieces">
  78. <div id="over"></div>
  79. </div>
  80. <div id="puzzle">
  81. <h2>{{ $data->title}} {{ $data->text}}</h2>
  82. <table id="board" cellspacing="0"></table>
  83. </div>
  84. <script type="text/javascript">
  85. var done = 0;
  86. var picture = "https://sun9-51.userapi.com/c204524/v204524368/1974/GIIiot2uoiw.jpg";
  87. function getCoords(elem) {
  88. let box = elem.getBoundingClientRect();
  89. return {
  90. top: box.top + pageYOffset,
  91. left: box.left + pageXOffset
  92. };
  93. }
  94. function startgame() {
  95. let index = 0;
  96. let over = document.querySelector("#over");
  97. let board = document.querySelector("#board");
  98. board.style.width = 50 * 9;
  99. for (let r = 0; r < 6; r++) {
  100. let tr = '<tr class="row" data-row="' + r + '"></tr>';
  101. board.insertAdjacentHTML('beforeend', tr);
  102. for (let c = 0; c < 9; c++) {
  103. index = c + (r * 9);
  104. let td = '<td class="tile" data-id="' + index + '"></td>';
  105. let ar = document.querySelector('tr[data-row="' + r + '"]');
  106. ar.insertAdjacentHTML('beforeend', td);
  107. let ac = document.querySelector('td[data-id="' + index + '"]');
  108. let block = '<div class="block" style="background-position: ' + c * -50 + "px " + r * -50 + 'px" data-id="' + index + '"></div>';
  109. over.insertAdjacentHTML('beforeend', block);
  110. let ab = over.querySelector('div[data-id="' + index + '"]');
  111. ab.style.backgroundImage = 'url("' + picture + '")';
  112. ab.addEventListener("mousedown", down);
  113. ab.style.left = Math.random() * (document.documentElement.clientWidth / 2 - 50) + "px";
  114. ab.style.top = Math.random() * (document.documentElement.clientHeight - 50) + "px";
  115. ab.dataset.left = getCoords(ab).left;
  116. ab.dataset.top = getCoords(ab).top;
  117. }
  118. }
  119. }
  120. function isCollide(orig, tile) {
  121. let b = getCoords(tile);
  122. let a = getCoords(orig);
  123. return !(
  124. ((a.top + 50) < (b.top)) ||
  125. (a.top > (b.top + 50)) ||
  126. ((a.left + 50) < b.left) ||
  127. (a.left > (b.left + 50))
  128. );
  129. }
  130. function down(e) {
  131. cell = e.target;
  132. if (cell.classList.contains("complete")) {
  133. return;
  134. }
  135. let coords = getCoords(cell);
  136. let shiftX = e.pageX - coords.left;
  137. let shiftY = e.pageY - coords.top;
  138. cell.style.position = 'absolute';
  139. moveAt(e);
  140. cell.style.zIndex = 1000;
  141. function moveAt(e) {
  142. cell.style.left = (e.pageX - shiftX) + 'px';
  143. cell.style.top = (e.pageY - shiftY) + 'px';
  144. }
  145. document.onmousemove = function(e) {
  146. moveAt(e);
  147. };
  148. cell.onmouseup = function() {
  149. document.onmousemove = null;
  150. cell.onmouseup = null;
  151. cell.style.zIndex = 500;
  152. field = document.querySelector("td[data-id='" + cell.dataset.id + "']");
  153. if (isCollide(cell, field)) {
  154. let temp = getCoords(field);
  155. cell.style.left = temp.left + "px";
  156. cell.style.top = temp.top + "px";
  157. cell.classList.add("complete");
  158. done++;
  159. if (done === 54) alert("You win!");
  160. } else {
  161. if (!isCollide(cell, document.querySelector("#pieces"))) {
  162. cell.style.left = cell.dataset.left + "px";
  163. cell.style.top = cell.dataset.top + "px";
  164. }
  165. }
  166. };
  167. cell.ondragstart = function() {
  168. return false;
  169. };
  170. }
  171. startgame();
  172. </script>
  173. </body>