创建两个按钮,使div随机放置

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

我留下了一个问题,因为编码时出现了问题。当按下按钮时,方形div将被放置在一个随机位置。
按钮的位置取决于屏幕的比例。▾ 请检查与图像的设计。▾


通过多次编码,我考虑了使用哪种结构,并继续使用下面的代码。
在手机屏幕上,我隐藏了桌面上的按钮。我制作了另一个只能在手机屏幕上看到的导航条,并制作了我前面提到的按钮。因此,当我在桌面上观看屏幕比率时,正方形div不是处于随机位置,而是始终位于顶部。
我真诚地希望你能让我知道问题出在哪里。
我不是一个编码Maven,所以如果你能用具体的代码来解释,我会非常感激。
提前谢谢你!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <style type="text/css">
  6. body{margin-top: 0px; margin-right: 0px; margin-left: 0px;}
  7. button {position: relative; width: 30px; height: 30px; background: #edd6bc; border: none; }
  8. .random {position: absolute; width: 30px; height: 30px; cursor: move;}
  9. .container {
  10. display: grid;
  11. grid-template-columns: 1fr;
  12. grid-template-rows: 1fr;
  13. }
  14. header {
  15. display: grid;
  16. grid-template-columns: 1fr 7fr;
  17. grid-template-rows: 1fr 1fr;
  18. grid-template-areas: "logo nav" "logo notice";
  19. }
  20. .logo {
  21. grid-area: logo;
  22. padding: 10px;
  23. text-align: center;
  24. border-bottom: 1px solid black;
  25. }
  26. nav {
  27. display: grid;
  28. grid-template-columns: repeat(7, 1fr);
  29. grid-area: nav;
  30. }
  31. .notice {
  32. grid-area: notice;
  33. padding: 10px;
  34. }
  35. nav a {
  36. padding: 10px;
  37. text-align: center;
  38. }
  39. main {
  40. display: grid;
  41. grid-template-columns: repeat(8, 1fr);
  42. grid-template-rows: repeat(8, 1fr);
  43. }
  44. main>div {
  45. padding: 10px;
  46. text-align: center;
  47. }
  48. .replat_mobile {
  49. display: none;
  50. }
  51. @media screen and (max-width: 800px) {
  52. .gone{display: none;}
  53. nav{width: 100vw;}
  54. .replat_mobile {
  55. clear: both;
  56. display: block;
  57. float: left;
  58. margin-left: 0px;
  59. width: 10px;
  60. }
  61. .container {
  62. display: grid;
  63. grid-template-columns: 1fr;
  64. grid-template-rows: 1fr;
  65. }
  66. header {
  67. display: grid;
  68. grid-template-columns: 1fr 3fr;
  69. grid-template-rows: 1fr 1fr;
  70. grid-template-areas: "logo nav" "logo notice";
  71. }
  72. nav {
  73. display: grid;
  74. grid-template-columns: repeat(3, 1fr);
  75. grid-area: nav;
  76. }
  77. .notice {
  78. grid-area: notice;
  79. padding: 10px;
  80. }
  81. nav a {
  82. padding: 10px;
  83. text-align: center;
  84. }
  85. main {
  86. display: grid;
  87. grid-template-columns: repeat(4, 1fr);
  88. grid-template-rows: repeat(4, 1fr);
  89. }
  90. main>div {
  91. padding: 10px;
  92. text-align: center;
  93. }
  94. }
  95. </style>
  96. <!--전시 컬러칩 드레그 이동 z-index 변경-->
  97. <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script>
  98. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>
  99. <script type="text/javascript">
  100. $(window).load(function(){
  101. $(document).ready(function() {
  102. var a = 3;
  103. $('.random').draggable({
  104. start: function(event, ui) { $(this).css("z-index", a++); }
  105. });
  106. $('body div').click(function() {
  107. $(this).addClass('top').removeClass('bottom');
  108. $(this).siblings().removeClass('top').addClass('bottom');
  109. $(this).css("z-index", a++);
  110. });
  111. });
  112. });
  113. </script>
  114. </head>
  115. <body>
  116. <div class="container">
  117. <div class="replat_mobile" >
  118. <nav>
  119. <a style="background-color: red;">
  120. <button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button"></button></a>
  121. <a style="background-color: yellow;"> <img style="height: calc(20vw - 50px);" src="https://cdn.imweb.me/upload/S202106158a019d8d01cb9/2b23eb2d2e178.jpg"> </a>
  122. <a style="background-color: blue;"> Eng </a>
  123. </nav>
  124. </div>
  125. <header>
  126. <div class="logo gone" style="border-right: 1px solid black;">Logo</div>
  127. <nav>
  128. <a href="#" style="border-right: 1px solid black;" >About</a>
  129. <a href="#" style="border-right: 1px solid black;" >Exibition</a>
  130. <a href="#" style="border-right: 1px solid black;" >Shop</a>
  131. <a class="gone"></a>
  132. <a class="gone"></a>
  133. <a href="#" class="gone" style="border-left: 1px solid black;" >
  134. <button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button" class="gone"></button>
  135. </a>
  136. <a href="#" class="gone" style="border-left: 1px solid black;" >Eng</a>
  137. </nav>
  138. <div class="notice" style="border-top: 1px solid black; border-bottom: 1px solid black;" >Space for Notice</div>
  139. </header>
  140. <!--전시 컬러칩-->
  141. <div a href="#" style="display: none; background: #6d97b4;" class="random" ></div>
  142. <div a href="#" style="display: none; background: #c9656f;" class="random" ></div>
  143. <div a href="#" style="display: none; background: #f1eb40;" class="random" ></div>
  144. <div a href="#" style="display: none; background: #00825c;" class="random" ></div>
  145. <div a href="#" style="display: none; background: #009ce0;" class="random" ></div>
  146. <div a href="#" style="display: none; background: #cee4a6;" class="random" ></div>
  147. </div>
  148. <script type="text/javascript">
  149. const btn = document.querySelector("button");
  150. const height = document.documentElement.clientHeight;
  151. const width = document.documentElement.clientWidth;
  152. const boxes = document.querySelectorAll(".random");
  153. btn.addEventListener("click", () => {
  154. Array.from(boxes).forEach(box => {
  155. const top = Math.floor(Math.random() * (height - 30) + 1) + "px";
  156. const right = Math.floor(Math.random() * (width - 30) + 1) + "px";
  157. box.style.top = top;
  158. box.style.right = right;
  159. })
  160. });
  161. function showhide() {
  162. var x = document.querySelectorAll(".random");
  163. var i;
  164. for (i = 0; i < x.length; i++) {
  165. if (x[i].style.display === "block") {
  166. x[i].style.display = "none";
  167. } else {
  168. x[i].style.display =
  169. "block";
  170. }
  171. }
  172. }
  173. //draggable
  174. function mouseDown(downEvent) {
  175. var box = downEvent.srcElement;
  176. var offX = box.getBoundingClientRect().left - downEvent.x;
  177. var offY = box.getBoundingClientRect().top - downEvent.y;
  178. document.onmousemove = e => {
  179. box.style.top = Math.min(Math.max(e.y + offY, 0), height) + "px";
  180. box.style.left = Math.min(Math.max(e.x + offX, 0), width) + "px";
  181. }
  182. document.onmouseup = e => {
  183. document.onmousemove = document.onmouseup = null;
  184. }
  185. return false;
  186. }
  187. Array.from(boxes).forEach(box => {
  188. box.onmousedown = mouseDown;
  189. })
  190. //호버 혹은 클릭 시, div 컬러 변화
  191. $(".music").hover(
  192. function() {
  193. $(this).addClass("hover");
  194. }, function() {
  195. $(this).removeClass("hover");
  196. }
  197. );
  198. $(".music").click(function() {
  199. $('#result').load('album_list_index.php');
  200. $(".music").removeClass("active");
  201. $(this).removeClass("hover").addClass("active");
  202. });
  203. </script>
  204. </body>
  205. </html>

暂无答案!

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

相关问题