javascript - 隐藏另一个 div 时移动一个 div
问题描述
div#content {
position: relative;
width: 100%;
border: 1px solid black;
height: 500px;
bottom: 0px;
}
div#menu {
position: absolute;
height: 125px;
width: 100%;
border: 1px solid black;
bottom: 0px;
line-height: 125px;
text-align: center;
}
div#recenter {
line-height: 50px;
text-align: center;
border: 1px solid black;
border-radius: 30px;
position: absolute;
margin: 10px;
padding: 0px 20px;
bottom: 180px;
background-color: aliceblue;
}
div#geolocation {
line-height: 50px;
text-align: center;
border: 1px solid black;
position: absolute;
margin: 10px;
bottom: 125px;
background-color: aliceblue;
}
<div id="content">
<div id="recenter">Re-center</div>
<div id="geolocation">My address is : 3958 Heron Way - Oregon 97351</div>
<div id="menu" onclick="document.getElementById('geolocation').style.display = 'none';">MENU (CLICK ME)</div>
</div>
#geolocation
目前,当我在 javascript 中隐藏块时,#recenter
按钮不会移动。
我想要的是,当我运行以下 jQuery 命令时:(
$('#geolocation').hide();
或在 js 中:)document.getElementById('geolocation').style.display = 'none';
按钮#recenter
移动到底部(#geolocation
块所在的位置)
怎么做 ?
解决方案
不要绝对定位元素,利用 flexbox 布局和对齐选项。
div#content {
position: relative;
width: 80%;
margin: 1em auto;
border: 1px solid black;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: flex-start;
}
div#menu {
height: 50px;
width: 80%;
border: 1px solid black;
text-align: center;
margin: 0 auto;
}
div#recenter {
line-height: 20px;
text-align: center;
border: 1px solid black;
border-radius: 30px;
margin: 10px;
padding: 0px 10px;
background-color: aliceblue;
}
div#geolocation {
line-height: 20px;
text-align: center;
border: 1px solid black;
margin: 10px;
background-color: aliceblue;
}
<div id="content">
<div id="recenter">Re-center</div>
<div id="geolocation">My address is : 3958 Heron Way - Oregon 97351</div>
<div id="menu" onclick="document.getElementById('geolocation').style.display = 'none';">MENU (CLICK ME)</div>
</div>
推荐阅读
- amazon-web-services - AWS EKS 添加用户受限于命名空间
- python - 如果列 X 具有特定值,则 matplotlib 上的箱线图与行
- time-complexity - 以下代码的大θ是多少?[我*我 <= n]
- reactjs - React Redux 等待状态改变
- google-tag-manager - 没有 gtm.linkClick 在谷歌标签管理器预览中为 CTRL 点击图片
- python - Keras IMDB 情绪分析
- reactjs - 将搜索过滤器移动到 MenuList
- android - 使用 Kotlin 的 MenuItem.SetOnActionExpandListener()
- python - 将 vtkResliceImageViewer 或 vtkImageViewer2 与 Python3+PyQt5 一起使用
- javascript - fabricjs - 将图像拖到画布上