gpt4 book ai didi

html - 转换容器内的内容

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:28 29 4
gpt4 key购买 nike

问题是,当使用该属性时,transform 父元素忽略了子元素大小的增加。

示例1:使用宽度和高度的子容器

.main {
display: inline-flex;
background: green;
padding: 10px;
}

.element {
display: block;
background: red;
width: 100px;
height: 100px;
}
<div class="main">
<div class="element"></div>
</div>

示例 2:使用转换的子容器

.main {
display: inline-flex;
background: green;
padding: 10px;
}

.element {
display: block;
background: red;
width: 100px;
height: 100px;
transform: scale(1, 2);
}
<div class="main">
<div class="element"></div>
</div>

如何让它transform考虑父容器?

最佳答案

这是一个使用js的解决方案。不过,使用 jq 会使代码更小。

您可以对所有转换后的 div 运行此函数

 document.querySelectorAll(".element").forEach((x)=>{
var elPos = x.getBoundingClientRect();
// make sure that the parent is at least as big as child
x.parentElement.style.minHeight = elPos.height +"px";
x.parentElement.style.minWidth = elPos.width + "px";
// make sure that the top of the child start with the parent
var elParentPos = x.parentElement.getBoundingClientRect();
if (elPos.top < elParentPos.top)
// Math.abs((elPos.height / elPos.top) is the scale diffrent
x.style.top = (elParentPos.top * Math.abs((elPos.height / elPos.top))) +"px"

})
.main {
display: inline-flex;
background: green;
padding: 10px;
}

.element {
display: block;
background: red;
width: 100px;
height: 100px;
transform: scale(1, 2);
position:relative;
}
<div class="main">
<div class="element"></div>
</div>

关于html - 转换容器内的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54808593/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com