javascript - 要求使 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>
解决方案
在生成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>
推荐阅读
- c - C语言中的位数据类型
- php - JSON 解析错误:警告:尝试读取字符串上的属性
- javascript - 向 websocket 服务器 React Native 应用程序发送/写入值
- python - 从左到右产生
- influxdb - Influxdb 2.0 测量、标签、字段
- haskell - Haskell 将 RoseTree 限制在深度 n
- docker - docker push 因未授权而失败:需要身份验证,仅适用于大图像
- python - 在以下情况下无法删除 Python 列表中多次出现的元素(尽管使用了 for 循环)(关闭前请完整阅读!)
- react-native - 使用 Redux Thunk 测试 AsyncStorage
- arrays - 视图函数未返回有效响应。该函数要么返回 None 要么在没有 return 语句的情况下结束