自动填充剩余宽度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>自适应宽</title>
<style>
.box {
position: absolute;
width: 300px;
height: 100px;
background-color: cyan;
}
.childbox1Attr {
position: absolute;
background-color: bisque;
width: 35px;
height: 35px;
}
.childbox2Attr {
position: absolute;
background-color: beige;
width: 35px;
height: 35px;
}
</style>
</head>
<body>
<div class="box">
<div class="childbox1Attr"></div>
<div class="childbox2Attr"></div>
</div>
<script>
const childbox1Attr = {
width: '100'
};
const childbox2Attr = {
width: '100',
flex: '1'
};
const box = document.getElementsByClassName('box')[0];
const childbox1Ele = document.getElementsByClassName('childbox1Attr')[0];
const childbox2Ele = document.getElementsByClassName('childbox2Attr')[0];
const boxcomputedStyle = window.getComputedStyle(box, null);
const child1ComputedStyle = window.getComputedStyle(childbox1Ele, null);
childbox1Ele.style.width = childbox1Attr.width+'px';
if(childbox2Attr.flex !== null || childbox2Attr.flex!==''){
if(childbox2Attr.flex === '1'){
// 利用一下不安全特性,实在不知道怎么写
const childbox2EleWidth = boxcomputedStyle["width"].substring(0,3) - child1ComputedStyle["width"].substring(0,3);
childbox2Ele.style.width = childbox2EleWidth + 'px';
childbox2Ele.style.left = child1ComputedStyle["width"]
}
}
</script>
</body>
</html>
展开