关于随机色片放置按钮的开发

gcuhipw9  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(239)

开发“色卡随机放置按钮”时出现问题,这是我正在开发的网站的主要功能。
“色卡随机放置按钮”的功能是显示或隐藏放置在随机位置的小方块。
为移动屏幕应用媒体查询代码时出现问题。出于手机设计的原因,我创建了另一个只能在手机屏幕上看到的按钮。由于这个原因,存在一个问题,即小型方形色卡不会放置在随机位置。
仅当移动屏幕比率为0时,按钮功能正常。而当桌面比率为0时,该功能不起作用。如果我按下桌面比率上的按钮,小正方形位于顶部。
在你给我答案之前,我先感谢你的帮助。
因为我不是一个编码Maven,如果你能用一个特定的代码给我一个特定的答案,我会非常感激。
我在按下色卡随机放置按钮前后附上设计草图。如果您能参考图片并帮助我编码,我将不胜感激。

  1. const btn = document.querySelector("button");
  2. const height = document.documentElement.clientHeight;
  3. const width = document.documentElement.clientWidth;
  4. const boxes = document.querySelectorAll(".random");
  5. btn.addEventListener("click", () => {
  6. Array.from(boxes).forEach(box => {
  7. const top = Math.floor(Math.random() * (height - 30) + 1) + "px";
  8. const right = Math.floor(Math.random() * (width - 30) + 1) + "px";
  9. box.style.top = top;
  10. box.style.right = right;
  11. })
  12. });
  13. function showhide() {
  14. var x = document.querySelectorAll(".random");
  15. var i;
  16. for (i = 0; i < x.length; i++) {
  17. if (x[i].style.display === "block") {
  18. x[i].style.display = "none";
  19. } else {
  20. x[i].style.display =
  21. "block";
  22. }
  23. }
  24. }
  25. //draggable
  26. function mouseDown(downEvent) {
  27. var box = downEvent.srcElement;
  28. var offX = box.getBoundingClientRect().left - downEvent.x;
  29. var offY = box.getBoundingClientRect().top - downEvent.y;
  30. document.onmousemove = e => {
  31. box.style.top = Math.min(Math.max(e.y + offY, 0), height) + "px";
  32. box.style.left = Math.min(Math.max(e.x + offX, 0), width) + "px";
  33. }
  34. document.onmouseup = e => {
  35. document.onmousemove = document.onmouseup = null;
  36. }
  37. return false;
  38. }
  39. Array.from(boxes).forEach(box => {
  40. box.onmousedown = mouseDown;
  41. })
  42. //호버 혹은 클릭 시, div 컬러 변화
  43. $(".music").hover(
  44. function() {
  45. $(this).addClass("hover");
  46. }, function() {
  47. $(this).removeClass("hover");
  48. }
  49. );
  50. $(".music").click(function() {
  51. $('#result').load('album_list_index.php');
  52. $(".music").removeClass("active");
  53. $(this).removeClass("hover").addClass("active");
  54. });
  1. body{margin-top: 0px; margin-right: 0px; margin-left: 0px;}
  2. /*호버 메뉴 컬러 변화*/
  3. .active { background: #edd6bc }
  4. .hover { background: #edd6bc }
  5. button {position: relative; width: 30px; height: 30px; background: #edd6bc; border: none; }
  6. .random {position: absolute; width: 30px; height: 30px; cursor: move;}
  7. .container {
  8. display: grid;
  9. grid-template-columns: 1fr;
  10. grid-template-rows: 1fr;
  11. }
  12. header {
  13. display: grid;
  14. grid-template-columns: 1fr 7fr;
  15. grid-template-rows: 1fr 1fr;
  16. grid-template-areas: "logo nav" "logo notice";
  17. }
  18. .logo {
  19. grid-area: logo;
  20. padding: 10px;
  21. text-align: center;
  22. border-bottom: 1px solid black;
  23. }
  24. .notice {
  25. grid-area: notice;
  26. padding: 10px;
  27. }
  28. main {
  29. display: grid;
  30. grid-template-columns: repeat(8, 1fr);
  31. grid-template-rows: repeat(8, 1fr);
  32. }
  33. main>div {
  34. padding: 10px;
  35. text-align: center;
  36. }
  37. nav {
  38. display: grid;
  39. grid-template-columns: repeat(7, 1fr);
  40. grid-area: nav;
  41. }
  42. nav a {
  43. padding: 10px;
  44. text-align: center;
  45. }
  46. /*shop과 서브메뉴 드랍*/
  47. li {
  48. border-right: 1px solid black;
  49. text-align: center;
  50. background-color: white;
  51. padding: 37px;
  52. }
  53. nav ul{
  54. list-style: none;
  55. }
  56. nav ul li {
  57. display: inline-block;
  58. list-style-type: none;
  59. }
  60. nav li > ul{
  61. display : none;
  62. }
  63. nav li > ul li{
  64. display: block;
  65. }
  66. nav li > ul li a {
  67. color: #111;
  68. display: block;
  69. line-height: 2em;
  70. padding: 0.5em 2em;
  71. text-decoration: none;
  72. }
  73. nav li:hover {
  74. background-color: #edd6bc;
  75. }
  76. nav li:hover > ul{
  77. position: absolute;
  78. display : block;
  79. }
  80. li.music {
  81. list-style: none;
  82. }
  83. .sub_nav{
  84. width: 100%;
  85. border: 1px solid black;
  86. }
  87. .replat_mobile {
  88. display: none;
  89. }
  90. @media screen and (max-width: 800px) {
  91. .gone{display: none;}
  92. nav{width: 100vw;}
  93. .replat_mobile {
  94. clear: both;
  95. display: block;
  96. float: left;
  97. margin-left: 0px;
  98. width: 10px;
  99. }
  100. .container {
  101. display: grid;
  102. grid-template-columns: 1fr;
  103. grid-template-rows: 1fr;
  104. }
  105. header {
  106. display: grid;
  107. grid-template-columns: 1fr 3fr;
  108. grid-template-rows: 1fr 1fr;
  109. grid-template-areas: "logo nav" "logo notice";
  110. }
  111. nav {
  112. display: grid;
  113. grid-template-columns: repeat(3, 1fr);
  114. grid-area: nav;
  115. }
  116. .notice {
  117. grid-area: notice;
  118. padding: 10px;
  119. }
  120. nav a {
  121. padding: 10px;
  122. text-align: center;
  123. }
  124. main {
  125. display: grid;
  126. grid-template-columns: repeat(4, 1fr);
  127. grid-template-rows: repeat(4, 1fr);
  128. }
  129. main>div {
  130. padding: 10px;
  131. text-align: center;
  132. }
  133. }
  1. <div class="container">
  2. <div class="replat_mobile" >
  3. <nav>
  4. <a style="background-color: red;">
  5. <button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button"></button>
  6. </a>
  7. <a style="background-color: yellow;">
  8. <img style="height: calc(20vw - 50px);" src="https://cdn.imweb.me/upload/S202106158a019d8d01cb9/2b23eb2d2e178.jpg">
  9. </a>
  10. <a style="background-color: blue;"> Eng </a>
  11. </nav>
  12. </div>
  13. <header>
  14. <div class="logo gone" style="border-right: 1px solid black;">Logo</div>
  15. <nav>
  16. <li class="music" style="border-right: 1px solid black;">
  17. <a href="#" class="music" style="background-color: yellow;">About</a>
  18. </li>
  19. <li class="music" style="border-right: 1px solid black;">
  20. <a href="#" class="music" style="background-color: yellow;">Exibition</a>
  21. </li>
  22. <li class="music">
  23. <a href="#" class="music" style="background-color: yellow;">Shop</a>
  24. <ul>
  25. <li a href="#" class="sub_nav" style="margin-top: 35px; ">리플랫</li>
  26. <li a href="#" class="sub_nav" style="margin-top: 13px; ">작품</li>
  27. <li a href="#" class="sub_nav" style="margin-top: 13px; ">MD</li>
  28. </ul>
  29. </li>
  30. <a class="gone"></a>
  31. <a class="gone"></a>
  32. <a href="#" class="gone" style="border-left: 1px solid black;" >
  33. <button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button" class="gone"></button>
  34. </a>
  35. <a href="#" class="gone music" style="border-left: 1px solid black;">Eng</a>
  36. </nav>
  37. <div class="notice" style="border-top: 1px solid black; border-bottom: 1px solid black;" >Space for Notice</div>
  38. </header>
  39. <!--전시 컬러칩-->
  40. <div a href="#" style="display: none; background: #6d97b4;" class="random" ></div>
  41. <div a href="#" style="display: none; background: #c9656f;" class="random" ></div>
  42. <div a href="#" style="display: none; background: #f1eb40;" class="random" ></div>
  43. <div a href="#" style="display: none; background: #00825c;" class="random" ></div>
  44. <div a href="#" style="display: none; background: #009ce0;" class="random" ></div>
  45. <div a href="#" style="display: none; background: #cee4a6;" class="random" ></div>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题