javascript - 即使在html,css,js中更改父级的宽度大小,如何防止坐标折叠
问题描述
div.area__box 使用 position: relative, absolute 放置在 div.area 中。
我的代码看起来像
<style type="text/css">
.area {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.2);
overflow: hidden;
position: relative;
}
.area__box {
position: absolute;
width: 200px;
height: 124px;
left: 50px;
top: 50px;
background-color: red;
cursor: move;
}
</style>
<div.area>
<div.area__box>
</div>
</div>
即使父项的宽度大小发生更改,我也想防止坐标折叠
我的代码不起作用。
例如,当div.area的width大小为676px时,我在div.area__box中设置了position:absolute,left:50px,top:50px。之后,如果宽度大小从 676px 变为 462px,
设置的“left: 50px, top: 50px”值变得毫无意义
屏幕短 676px 时
屏幕短 462px 时
我怎么解决这个问题?如果您知道任何提示,请告诉我。
我的完整代码是:
<!DOCTYPE html>
<html>
<head>
<!-- -->
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.parent {
width: 50vw;
height: 380px;
}
.area {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.2);
overflow: hidden;
position: relative;
}
.area__box {
position: absolute;
width: 200px;
height: 124px;
left: 50px;
top: 50px;
background-color: red;
cursor: move;
}
</style>
</head>
<body>
<div class="parent">
<div class="area">
<div class="area__box"></div>
</div>
</div>
<script type="text/javascript">
let containerGlobalWidth;
let containerGlobalHeight;
let containerGlobalLeft;
let containerGlobalTop;
let containerGlobalRight;
let containerGlobalBottom;
let boxGlobalLeft;
let boxGlobalTop;
let boxGlobalRight;
let boxGlobalBottom;
const container = document.querySelector(".area");
const box = container.querySelector(".area__box");
const resizeObserver = new ResizeObserver(entries => {
const {
left: containerLeft2,
top: containerTop2,
right: containerRight2,
bottom: containerBottom2
} = container.getBoundingClientRect();
const {
left: boxLeft2,
top: boxTop2,
right: boxRight2,
bottom: boxBottom2
} = box.getBoundingClientRect();
containerGlobalLeft = containerLeft2;
containerGlobalTop = containerTop2;
containerGlobalRight = containerRight2;
containerGlobalBottom = containerBottom2;
boxGlobalLeft = boxLeft2;
boxGlobalTop = boxTop2;
boxGlobalRight = boxRight2;
boxGlobalBottom = boxBottom2;
});
resizeObserver.observe(document.body);
const {
width: containerWidth,
height: containerHeight,
left: containerLeft,
top: containerTop,
right: containerRight,
bottom: containerBottom
} = container.getBoundingClientRect();
containerGlobalWidth = containerWidth;
containerGlobalHeight = containerHeight;
containerGlobalLeft = containerLeft;
containerGlobalTop = containerTop;
containerGlobalRight = containerRight;
containerGlobalBottom = containerBottom;
const {
width: boxWidth,
height: boxHeight
} = box.getBoundingClientRect();
let isDragging = null;
let originLeft = null;
let originTop = null;
let originX = null;
let originY = null;
</script>
</body>
</html>
感谢您阅读我的问题。
解决方案
推荐阅读
- python - Tensorflow API、Tensorflow hub 和 Keras 输入形状
- ag-grid - ag-grid 中的动态列配置
- python - pandas groupby 将相同的功能应用于多列
- html - 使用 VBA 在 Excel 中将当前选择另存为 HTML
- reactjs - ReactJs - 无法一次显示所有数据。获取数据的循环通过一个一个重新渲染自动运行,并在最后一个数据集停止
- javascript - 可以在一个或多个 Ajax 调用中显示来自不同 div 的多个 html 结果吗?
- javascript - p5 在鼠标移动时显示 div
- flutter - 如何制作中心更大的旋转木马?(扑)
- typescript - Action-Reducer 映射具有多种操作类型的类型错误
- jboss-eap-6 - 引起:java.lang.IllegalStateException:前缀ns3未绑定