Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. var box1 = document.getElementById("box1");
  5. var winHeight = window.innerHeight;
  6. var winWidth = window.innerWidth;
  7. var boxHeight = box1.offsetHeight;
  8. var boxWidth = box1.offsetWidth;
  9.  
  10. function printConsole() {
  11. console.log(winHeight);
  12. console.log(winWidth);
  13. console.log(boxHeight);
  14. console.log(boxWidth);
  15. console.log(window.getComputedStyle(box1).left);
  16. console.log(window.getComputedStyle(box1).top);
  17. }
  18.  
  19. function centerBox() {
  20. winHeight = window.innerHeight;
  21. winWidth = window.innerWidth;
  22. boxHeight = box1.offsetHeight;
  23. boxWidth = box1.offsetWidth;
  24.  
  25. box1.style.left = (winWidth / 2) - (boxWidth / 2) + "px";
  26. box1.style.top = (winHeight / 2) - (boxHeight / 2) + "px";
  27. }
  28.  
  29. function circle() {
  30. var all = document.getElementsByClassName("selected");
  31.  
  32. for (var i = 0; i < all.length; i++) {
  33. all[i].classList.toggle("circle");
  34. }
  35. }
  36.  
  37. function increaseSize() {
  38. var all = document.getElementsByClassName("selected");
  39.  
  40. for (var i = 0; i < all.length; i++) {
  41. winHeight = window.innerHeight;
  42. winWidth = window.innerWidth;
  43. boxHeight = box1.offsetHeight;
  44. boxWidth = box1.offsetWidth;
  45.  
  46. all[i].style.width = boxWidth + 5 + "px";
  47. all[i].style.height = boxHeight + 5 + "px";
  48. box1.style.left = (winWidth / 2) - (boxWidth / 2) + "px";
  49. box1.style.top = (winHeight / 2) - (boxHeight / 2) + "px";
  50. }
  51. }
  52.  
  53. function decreaseSize() {
  54. var all = document.getElementsByClassName("selected");
  55.  
  56. for (var i = 0; i < all.length; i++) {
  57. winHeight = window.innerHeight;
  58. winWidth = window.innerWidth;
  59. boxHeight = box1.offsetHeight;
  60. boxWidth = box1.offsetWidth;
  61.  
  62. all[i].style.width = boxWidth - 5 + "px";
  63. all[i].style.height = boxHeight - 5 + "px";
  64. box1.style.left = (winWidth / 2) - (boxWidth / 2) + "px";
  65. box1.style.top = (winHeight / 2) - (boxHeight / 2) + "px";
  66. }
  67. }
  68.  
  69. function switchColor() {
  70. var all = document.getElementsByClassName("selected");
  71.  
  72. for (var i = 0; i < all.length; i++) {
  73. var test1 = window.getComputedStyle(all[i]).getPropertyValue("background-color");
  74.  
  75. if (test1 == "rgb(0, 0, 255)") {
  76. all[i].style.backgroundColor = "rgb(0, 128, 0)";
  77. }
  78.  
  79. if (test1 == "rgb(255, 0, 0)") {
  80. all[i].style.backgroundColor = "rgb(0, 0, 255)";
  81. }
  82.  
  83. if (test1 == "rgb(255, 255, 0)") {
  84. all[i].style.backgroundColor = "rgb(255, 0, 0)";
  85. }
  86.  
  87. if (test1 == "rgb(0, 128, 0)") {
  88. all[i].style.backgroundColor = "rgb(255, 255, 0)";
  89. }
  90. }
  91. }
  92.  
  93. function cloneObj() {
  94. var all = document.querySelectorAll(".selected");
  95.  
  96. winHeight = window.innerHeight;
  97. winWidth = window.innerWidth;
  98.  
  99. for (var i = 0; i < all.length; i++) {
  100. var clone = all[i].cloneNode(true);
  101.  
  102.  
  103. clone.addEventListener("click", function(event) {
  104. event.target.classList.toggle("selected");
  105. });
  106.  
  107. clone.addEventListener("dblclick", function(event) {
  108. event.target.classList.toggle("animateSize");
  109.  
  110. setTimeout(function() {
  111. var all = document.querySelectorAll(".animateSize");
  112.  
  113. for (var i = 0; i < all.length; i++) {
  114. document.body.removeChild(all[i]);
  115. }
  116. }, 3000);
  117. });
  118.  
  119. function clonePosition() {
  120. clone.style.position = 'absolute';
  121. // Stick on the left
  122. clone.style.left = Math.round(Math.random() * winWidth) / 1.2 + 'px';
  123. clone.style.top = Math.round(Math.random() * winHeight) / 1.2 + 'px';
  124.  
  125. // Stick on the right
  126. clone.style.right = Math.round(Math.random() * winWidth) / 1.2 + 'px';
  127. clone.style.top = Math.round(Math.random() * winHeight) / 1.2 + 'px';
  128. }
  129.  
  130. window.addEventListener("resize", clonePosition);
  131.  
  132. clonePosition();
  133. // Add it to body
  134. var newOne = document.body.appendChild(clone);
  135.  
  136. console.log("Antal Kloner = " + all.length)
  137. }
  138. }
  139.  
  140. function indexMinus() {
  141. var all = document.getElementsByClassName("selected");
  142.  
  143.  
  144. for (var i = 0; i < all.length; i++) {
  145. var test1 = window.getComputedStyle(all[i]).getPropertyValue("z-index");
  146.  
  147. all[i].style.zIndex = test1 - 1;
  148. }
  149. }
  150.  
  151. function indexPlus() {
  152. var all = document.getElementsByClassName("selected");
  153.  
  154. for (var i = 0; i < all.length; i++) {
  155. var test2 = window.getComputedStyle(all[i]).getPropertyValue("z-index");
  156.  
  157. all[i].style.zIndex = parseInt(test2) + 1;
  158. }
  159. }
  160.  
  161. function removeObj() {
  162. var all = document.querySelectorAll(".selected");
  163.  
  164. for (var i = 0; i < all.length; i++) {
  165. document.body.removeChild(all[i]);
  166. }
  167. }
  168.  
  169. function arrowUp() {
  170. var all = document.getElementsByClassName("selected");
  171. var step = 10;
  172.  
  173. for (var i = 0; i < all.length; i++) {
  174. var top = all[i].offsetTop;
  175. var left = all[i].offsetLeft;
  176.  
  177. all[i].style.top = (top - step) + "px";
  178. }
  179. }
  180.  
  181. function arrowDown() {
  182. var all = document.getElementsByClassName("selected");
  183. var step = 10;
  184.  
  185. for (var i = 0; i < all.length; i++) {
  186. var top = all[i].offsetTop;
  187. var left = all[i].offsetLeft;
  188.  
  189. all[i].style.top = (top + step) + "px";
  190. }
  191. }
  192.  
  193. function arrowLeft() {
  194. var all = document.getElementsByClassName("selected");
  195. var step = 10;
  196.  
  197. for (var i = 0; i < all.length; i++) {
  198. var top = all[i].offsetTop;
  199. var left = all[i].offsetLeft;
  200.  
  201. all[i].style.left = (left - step) + "px";
  202. }
  203. }
  204.  
  205. function arrowRight() {
  206. var all = document.getElementsByClassName("selected");
  207. var step = 10;
  208.  
  209. for (var i = 0; i < all.length; i++) {
  210. var top = all[i].offsetTop;
  211. var left = all[i].offsetLeft;
  212.  
  213. all[i].style.left = (left + step) + "px";
  214. }
  215. }
  216.  
  217. function unTarget() {
  218. var els = document.getElementsByClassName("selected");
  219.  
  220. els[0].classList.remove("selected")
  221. if (els[0]) unTarget()
  222. }
  223.  
  224. function addClass() {
  225. var els = document.getElementsByClassName("box");
  226.  
  227. for (var i = 0; i < els.length; i++) {
  228. els[i].classList.add("selected");
  229. }
  230. console.log("Antal element med classen 'selected' = " + els.length)
  231. }
  232.  
  233. function slumpClone() {
  234. var all = document.querySelectorAll(".selected");
  235.  
  236. winHeight = window.innerHeight;
  237. winWidth = window.innerWidth;
  238.  
  239. for (var i = 0; i < all.length; i++) {
  240. var clone = all[i].cloneNode(true);
  241. var colors = ["rgb(0, 0, 255)", "rgb(0, 128, 0)", "rgb(255, 0, 0)", "rgb(255, 255, 0)"];
  242. var formType = ["0%", "50%"];
  243.  
  244. clone.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
  245. clone.style.borderRadius = formType[Math.floor(Math.random() * colors.length)];
  246. clone.style.position = 'absolute';
  247. clone.style.left = Math.round(Math.random() * winWidth) / 1.2 + 'px';
  248. clone.style.top = Math.round(Math.random() * winHeight) / 1.2 + 'px';
  249. clone.style.right = Math.round(Math.random() * winWidth) / 1.2 + 'px';
  250. clone.style.top = Math.round(Math.random() * winHeight) / 1.2 + 'px';
  251.  
  252. // Add it to body
  253. clone.addEventListener("click", function(event) {
  254. event.target.classList.toggle("selected");
  255. });
  256.  
  257. clone.addEventListener("dblclick", function(event) {
  258. event.target.classList.toggle("animateSize");
  259.  
  260. setTimeout(function() {
  261. var all = document.querySelectorAll(".animateSize");
  262.  
  263. for (var i = 0; i < all.length; i++) {
  264. document.body.removeChild(all[i]);
  265. }
  266. }, 3000);
  267. });
  268.  
  269. var newOne = document.body.appendChild(clone);
  270. }
  271. }
  272.  
  273. window.addEventListener("resize", centerBox);
  274. box1.addEventListener("click", function(event) {
  275. event.target.classList.toggle("selected");
  276. });
  277.  
  278. box1.addEventListener("dblclick", function(event) {
  279. event.target.classList.toggle("animateSize");
  280.  
  281. setTimeout(function() {
  282. var all = document.querySelectorAll(".animateSize");
  283.  
  284. for (var i = 0; i < all.length; i++) {
  285. document.body.removeChild(all[i]);
  286. }
  287. }, 3000);
  288. });
  289.  
  290. document.addEventListener("keydown", function(event) {
  291. var key = event.key;
  292.  
  293. switch(key) {
  294. case "e":
  295. circle();
  296. break;
  297. case "q":
  298. increaseSize();
  299. break;
  300. case "w":
  301. decreaseSize();
  302. break;
  303. case "r":
  304. switchColor();
  305. break;
  306. case "t":
  307. cloneObj();
  308. break;
  309. case "a":
  310. indexMinus();
  311. break;
  312. case "s":
  313. indexPlus();
  314. break;
  315. case "y":
  316. removeObj();
  317. break;
  318. case "ArrowUp":
  319. event.preventDefault();
  320. arrowUp();
  321. break;
  322. case "ArrowDown":
  323. event.preventDefault();
  324. arrowDown();
  325. break;
  326. case "ArrowLeft":
  327. event.preventDefault();
  328. arrowLeft();
  329. break;
  330. case "ArrowRight":
  331. event.preventDefault();
  332. arrowRight();
  333. break;
  334. case "u":
  335. unTarget();
  336. break;
  337. case "i":
  338. addClass();
  339. break;
  340. case "p":
  341. slumpClone();
  342. break;
  343. case "d":
  344. slumpClone();
  345. break;
  346. }
  347. });
  348.  
  349. printConsole();
  350. centerBox();
  351.  
  352. console.log('Kmom05 Done!');
  353. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement