首页 > 解决方案 > 制作两个按钮,使 div 随机放置

问题描述

我留下了一个问题,因为编码时出现问题。按下按钮时,方形 div 将被放置在随机位置。

按钮的位置取决于屏幕的比例。▾请与图片核对设计。▾</p>

在此处输入图像描述 在此处输入图像描述

通过多次编码,我考虑了使用哪种结构并继续编写下面的代码。

在手机屏幕上,我将桌面上的按钮隐藏起来。我制作了另一个只能在手机屏幕上看到的导航栏,并制作了我之前提到的按钮。因此,当我在桌面屏幕比例上观看时,方形 div 不是随机位置,而是始终位于顶部。

我真诚地希望你能告诉我问题是什么。

而且我不是编码专家,所以如果您能用特定的代码解释它,我将不胜感激。

先感谢您 !

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">

    
    <style type="text/css">


        body{margin-top: 0px; margin-right: 0px; margin-left: 0px;}

        button {position: relative; width: 30px; height: 30px; background: #edd6bc; border: none; }
        .random {position: absolute; width: 30px; height: 30px; cursor: move;}

        .container {
          display: grid;
          grid-template-columns: 1fr;
          grid-template-rows: 1fr;
        }

        header {
          display: grid;
          grid-template-columns: 1fr 7fr;
          grid-template-rows: 1fr 1fr;
          grid-template-areas: "logo nav" "logo notice";
        }

        .logo {
          grid-area: logo;
          padding: 10px;
          text-align: center;
          border-bottom: 1px solid black;
        }

        nav {
          display: grid;
          grid-template-columns: repeat(7, 1fr);
          grid-area: nav;
        }

        .notice {
          grid-area: notice;
          padding: 10px;
        }

        nav a {
          padding: 10px;
          text-align: center;
        }

        main {
          display: grid;
          grid-template-columns: repeat(8, 1fr);
          grid-template-rows: repeat(8, 1fr);
        }

        main>div {
          padding: 10px;
          text-align: center;
        }

        .replat_mobile {
            display: none;
        }



        @media screen and (max-width: 800px) {
        
            
              
            .gone{display: none;}
              
            nav{width: 100vw;}
              
            .replat_mobile {
                clear: both;
                display: block;
                float: left;
                margin-left: 0px;
                width: 10px;          
            }


            .container {
              display: grid;
              grid-template-columns: 1fr;
              grid-template-rows: 1fr;
            }

            header {
              display: grid;
              grid-template-columns: 1fr 3fr;
              grid-template-rows: 1fr 1fr;
              grid-template-areas: "logo nav" "logo notice";
            }

            nav {
              display: grid;
              grid-template-columns: repeat(3, 1fr);
              grid-area: nav;
            }

            .notice {
              grid-area: notice;
              padding: 10px;
            }

            nav a {
              padding: 10px;
              text-align: center;
            }

            main {
              display: grid;
              grid-template-columns: repeat(4, 1fr);
              grid-template-rows: repeat(4, 1fr);
            }

            main>div {
              padding: 10px;
              text-align: center;
            }


        }



    </style>

    <!--전시 컬러칩 드레그 이동 z-index 변경-->
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script>
          
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>

        <script type="text/javascript">
          $(window).load(function(){
          $(document).ready(function() {
            var a = 3;
            $('.random').draggable({
           start: function(event, ui) { $(this).css("z-index", a++); }
        });
          $('body div').click(function() {
                $(this).addClass('top').removeClass('bottom');
                $(this).siblings().removeClass('top').addClass('bottom');
                $(this).css("z-index", a++);

            });
        });
        });
    </script>

</head>


<body>

    <div class="container">


        <div class="replat_mobile" >
          <nav>
            <a style="background-color: red;">
<button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button"></button></a>
            <a style="background-color: yellow;"> <img style="height: calc(20vw - 50px);" src="https://cdn.imweb.me/upload/S202106158a019d8d01cb9/2b23eb2d2e178.jpg"> </a>
            <a style="background-color: blue;"> Eng </a>
          </nav>
        </div>

        <header>
          
          <div class="logo gone" style="border-right: 1px solid black;">Logo</div>
          
          <nav>
            <a href="#" style="border-right: 1px solid black;" >About</a>
            <a href="#" style="border-right: 1px solid black;" >Exibition</a>
            <a href="#" style="border-right: 1px solid black;" >Shop</a>
            
            <a class="gone"></a>
            <a class="gone"></a>
            
            <a href="#" class="gone" style="border-left: 1px solid black;" >
                <button style="margin: auto;" onclick="showhide()" value="Zeige Features" id="button" class="gone"></button>
            </a>
            <a href="#" class="gone" style="border-left: 1px solid black;" >Eng</a>
          </nav>
          
          <div class="notice" style="border-top: 1px solid black; border-bottom: 1px solid black;" >Space for Notice</div>
        
        </header>
        

        <!--전시 컬러칩-->

        <div a href="#" style="display: none; background: #6d97b4;" class="random" ></div>
            
        <div a href="#" style="display: none; background: #c9656f;" class="random" ></div>
            
        <div a href="#" style="display: none; background: #f1eb40;" class="random" ></div>
            
        <div a href="#" style="display: none; background: #00825c;" class="random" ></div>
            
        <div a href="#" style="display: none; background: #009ce0;" class="random" ></div>
            
        <div a href="#" style="display: none; background: #cee4a6;" class="random" ></div>
 
    </div>



    <script type="text/javascript">

            const btn = document.querySelector("button");
            const height = document.documentElement.clientHeight;
            const width = document.documentElement.clientWidth;
            const boxes = document.querySelectorAll(".random");

            btn.addEventListener("click", () => {
              Array.from(boxes).forEach(box => {
                const top = Math.floor(Math.random() * (height - 30) + 1) + "px";
                const right = Math.floor(Math.random() * (width - 30) + 1) + "px";

                box.style.top = top;
                box.style.right = right;
              })
            });

            function showhide() {
              var x = document.querySelectorAll(".random");
              var i;
              for (i = 0; i < x.length; i++) {
                if (x[i].style.display === "block") {
                  x[i].style.display = "none";
                } else {
                  x[i].style.display =
                    "block";
                }
              }
            }

            //draggable

            function mouseDown(downEvent) {
              var box = downEvent.srcElement;
              var offX = box.getBoundingClientRect().left - downEvent.x;
              var offY = box.getBoundingClientRect().top - downEvent.y;
              document.onmousemove = e => {
                box.style.top = Math.min(Math.max(e.y + offY, 0), height) + "px";
                box.style.left = Math.min(Math.max(e.x + offX, 0), width) + "px";
              }
              document.onmouseup = e => {
                document.onmousemove = document.onmouseup = null;
              }
              return false;
            }

            Array.from(boxes).forEach(box => {
              box.onmousedown = mouseDown;
            })


            //호버 혹은 클릭 시, div 컬러 변화
            
            $(".music").hover(
                function() {
                    $(this).addClass("hover");
                }, function() {
                    $(this).removeClass("hover");
                }
            );

            $(".music").click(function() {
                $('#result').load('album_list_index.php');
                $(".music").removeClass("active");
                $(this).removeClass("hover").addClass("active");
            });
    
    </script>

</body>
</html>

标签: javascriptcssbuttonrandom

解决方案


推荐阅读