微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

要求使 div 可拖动而不是放置在屏幕外

如何解决要求使 div 可拖动而不是放置在屏幕外

我很难写代码,所以我在这里问了一个问题。

如果按下左上角的按钮,可以使颜色框不可见。同时,我写下了按钮可以让颜色框随机放置的代码

我想确保颜色框不会出现在屏幕之外。即使将 body 上的宽度和高度设置为 100%,颜色框也被放置在屏幕外。

而且我想更正颜色框,以便它们可以通过可拖动功能移动,但它们不能一起工作。我也想寻求帮助。

而且我不是编码专家,所以我需要对编码示例进行评论


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

    
    <style type="text/css">
    body {margin:0; padding:0;}
    button {position: absolute; width: 30px; height: 30px; background: #edd6bc;}
    .random {position: absolute; width: 30px; height: 30px;}

    </style>

    <script>

    </script>

</head>


<body >

    <button onclick="showhide()" value="Zeige Features" id="button"></button>

    <div style="display: none; background: #6d97b4;" class="random" ></div>
    
    <div style="display: none; background: #c9656f;" class="random" ></div>
    
    <div style="display: none; background: #f1eb40;" class="random" ></div>
    
    <div style="display: none; background: #00825c;" class="random" ></div>
    
    <div style="display: none; background: #009ce0;" class="random" ></div>
    
    <div style="display: none; background: #cee4a6;" class="random" ></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 => {
    Box.style.top = Math.floor((Math.random() * height) + 1) + "px";
    Box.style.right = Math.floor((Math.random() * width) + 1) + "px";
  })
});

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";
    }
  }
}
</script>

</body>
</html>


解决方法

如果你想要一个解释告诉我。

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

    
    <style type="text/css">
    body {margin:0; padding:0;}
    button {position: absolute; width: 30px; height: 30px; background: #edd6bc;}
    .random {position: absolute; width: 30px; height: 30px;}

    </style>

    <script>

    </script>

</head>


<body >

    <button onclick="showhide()" value="Zeige Features" id="button"></button>

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



<script type="text/javascript">
const btn = document.querySelector("button");
const height = document.documentElement.clientHeight - 30;
const width = document.documentElement.clientWidth - 30;
const boxes = document.querySelectorAll(".random");

btn.addEventListener("click",() => {
  Array.from(boxes).forEach(box => {
    box.style.top = Math.floor(Math.random() * height) + "px";
    box.style.left = Math.floor(Math.random() * width) + "px";
  })
});

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";
    }
  }
}




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,width) + "px";
  }
  document.onmouseup = e => {
    document.onmousemove = document.onmouseup = null;
  }
  return false;
}

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

</script>

</body>
</html>

,

生成top * right时减去div的高度和宽度

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";
    }
  }
}
body {
  margin: 0;
  padding: 0;
}

button {
  position: relative;
  width: 30px;
  height: 30px;
  background: #edd6bc;
}

.random {
  position: absolute;
  width: 30px;
  height: 30px;
}
<button onclick="showhide()" value="Zeige Features" id="button"></button>

<div style="display: none; background: #6d97b4;" class="random"></div>

<div style="display: none; background: #c9656f;" class="random"></div>

<div style="display: none; background: #f1eb40;" class="random"></div>

<div style="display: none; background: #00825c;" class="random"></div>

<div style="display: none; background: #009ce0;" class="random"></div>

<div style="display: none; background: #cee4a6;" class="random"></div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。