Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. let containerAllSquareDivs = document.querySelector(".container-all-square-divs");// Select the container
  2. let userInput; // Squares number choice
  3.  
  4. // Bouton Reset
  5. document.querySelector("#squares-number-button").addEventListener("click", resetGrid)
  6.  
  7.  
  8. // Game beginning : Create a 16 squares grid
  9. createSquaresGrid(16);
  10.  
  11. // Create squares grid
  12. function createSquaresGrid(userInput) {
  13. // Fix the grid's columns and rows number inside containerAllSquareDivs
  14. containerAllSquareDivs.style.setProperty("grid-template-columns", "repeat(" + userInput + ", auto)");
  15. containerAllSquareDivs.style.setProperty("grid-template-row", "repeat(" + userInput + ", auto)");
  16.  
  17. // Create new squares and append them to the container
  18. for (let i = 0; i < userInput * userInput; i++) {
  19. let newSquareDiv = document.createElement("div");
  20. newSquareDiv.classList.add("new-square-divs");
  21. containerAllSquareDivs.appendChild(newSquareDiv);
  22. }
  23.  
  24. // Select all created divs (array)
  25. let allSquareDivs = document.querySelectorAll(".new-square-divs");
  26. // "Greyshade effect" for all created divs
  27.  
  28. for (let i = 0; i < userInput * userInput; i++) {
  29. let opacity = 0.1;
  30.  
  31. allSquareDivs[i].addEventListener("mouseenter", function(){
  32. allSquareDivs[i].style.backgroundColor = "black";
  33. allSquareDivs[i].style.opacity = opacity;
  34.  
  35. if (opacity < 1) {
  36. opacity += 0.1
  37. }
  38. })
  39. }
  40.  
  41. // "Random colors effect" for all created divs when Random Colors Button clicked
  42. document.querySelector("#random-colors-button").addEventListener("click", function() {
  43.  
  44. for (let i = 0; i < userInput * userInput; i++) {
  45. allSquareDivs[i].style.backgroundColor = "white";
  46. allSquareDivs[i].style.opacity = "1";
  47. allSquareDivs[i].classList.add("nopacity")
  48. allSquareDivs[i].addEventListener("mouseenter", getRandomColor);
  49.  
  50. };
  51. })
  52.  
  53. // Clear the grid from all colors
  54. let clearButton = document.querySelector("#clear-button")
  55. clearButton.addEventListener("click", function() {
  56. for (let i = 0; i < userInput * userInput; i++) {
  57. allSquareDivs[i].style.backgroundColor = "";
  58. allSquareDivs[i].style.opacity = "1";
  59. }
  60. })
  61. }
  62.  
  63. // Fix a random color code (HEX)
  64. function getRandomColor() {
  65. var letters = '0123456789ABCDEF';
  66. var color = '#';
  67. for (var i = 0; i < 6; i++) {
  68. color += letters[Math.floor(Math.random() * 16)];
  69. }
  70. this.style.backgroundColor = color;
  71. }
  72.  
  73.  
  74. // Reset the grid when Reset Button clicked
  75. document.querySelector("#reset-button").addEventListener("click", function() {
  76. resetGrid()
  77. createSquaresGrid(16)
  78. })
  79.  
  80. // Reset the grid
  81. function resetGrid () {
  82. let containerAllSquareDivs = document.querySelector(".container-all-square-divs"); // Pourquoi ne pas pouvoir accéder à containerAllSquareDivs(1) et avoir à le redéfinir dans la fonction?
  83.  
  84. // Remove all created divs inside the container
  85. while (containerAllSquareDivs.hasChildNodes()) {
  86. containerAllSquareDivs.removeChild(containerAllSquareDivs.lastChild);
  87. }
  88. //let userInput = prompt("Combien de carrés ?")
  89. let userInput = 5;
  90. createSquaresGrid(userInput)
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement