将div高度设置为另一个div的高度

时间:2020-08-12 22:12:09

标签: javascript html css

我使用以下各项制作了时间轴:https://www.w3schools.com/howto/howto_css_timeline.asp,并将其设置为位置:粘性。这在一个名为div.timeContainer的容器中。旁边是单独的div中的一些文本。其想法是,用户可以向下滚动,阅读右侧的文本,而查看左侧的时间轴/概述。

现在的问题是,如果我设置div.timeContainer的高度,则调整窗口的大小意味着由于右侧的div变长了,时间轴将在半途中停止显示/处于粘滞状态。

这(和变体)是我到目前为止尝试过的:

const historyContainer = document.querySelector("div.history").style.height

document.querySelector("div.timeContainer").style.height = historyContainer

1 个答案:

答案 0 :(得分:1)

我为您准备了一个简单的示例,为孩子分配父母身高。香草js中的示例。

let parent_div = document.querySelector('.parent');
let child_div = document.querySelector('.child');
let click_button = document.querySelector('input');

click_button.onclick = function(){
  child_div.style.height = parent_div.offsetHeight + 'px';
};
.parent {
  width: 100%;
  height: 300px;
  background-color: yellow;
}

.child {
  width: 50%;
  background-color: green;
}
<input type="button" value="click me to get the height of the child div">
<div class="parent">
 <div class="child">
 </div>
</div>