javascript - 使用脚本根据主面板设置左右面板宽度
问题描述
我的问题与 CSS 和 JavaScript 有关。
我有一个名为“ main-panel
”的 CSS 类(注意,它的类不是 ID)和两个左右面板“ left-panel
”和“ right-panel
”——都是类而不是 ID,我什至不能将类更改为 Id。
现在,我想要根据“主面板的”高度min-height
设置“ ”的“ left-panel
”和“ ” 。
如果加载内容后主面板的高度为 1000px;它应该是左右面板的最小高度。right-panel
解决方案
这是如何做到的。
const setPanelHeight = (className, height) => {
document.getElementsByClassName(className)[0].style.minHeight = height;
};
let desiredHeight = getMainPanelHeight();
setPanelHeight("left-panel", desiredHeight);
setPanelHeight("right-panel", desiredHeight);
function getMainPanelHeight() {
let element = document.getElementsByClassName("main-panel")[0];
let computedStyle = window.getComputedStyle(element, null);
return computedStyle["height"];
}
div {
display: inline-block;
width: 50px;
}
.left-panel,
.right-panel {
border: 1px solid red;
}
.main-panel {
border: 1px solid black;
height: 1000px;
}
<div class="left-panel"></div>
<div class="main-panel"></div>
<div class="right-panel"></div>
推荐阅读
- mysql - MySql WorkBench AES 256 解密
- vba - VBA打开Excel文件,刷新bloomberg数据,保存,关闭
- matrix - How do you get the mean of a Eigen::MatrixXd column?
- laravel - 如何修复 Laravel 上的 php7.1-fpm.sock 故障
- azure-active-directory - 使用自定义身份提供商注册的用户缺少用户名
- javascript - 如何以最少的代码行一次显示一个 div?
- react-native - 如何在一段时间后更新进度条,并在收到某些数据时重置?
- forms - 如何在 HTML 表单中正确添加文本区域字段
- javascript - 如何使用 JavaScript 从字符串中删除重复的 CSS 样式?
- html - CSS 表格“借用”另一个表格的样式