简体   繁体   English

隐藏另一个 div 时移动一个 div

[英]Move a div when another div is hidden

 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>

Currently, when I hide the #geolocation block in javascript, the #recenter button does not move.目前,当我在 javascript 中隐藏#geolocation块时, #recenter按钮不会移动。

What I want is that when I run the following jQuery command: $('#geolocation').hide();我想要的是当我运行以下 jQuery 命令时: $('#geolocation').hide(); (or in js : document.getElementById('geolocation').style.display = 'none'; ) the #recenter button moves to the bottom (where the #geolocation block was located) (或在 js 中: document.getElementById('geolocation').style.display = 'none';#recenter按钮移动到底部( #geolocation块所在的位置)

How to do ?怎么做 ?

Don't position elements the absolutely, take advantage of flexbox layout and alignment options.不要绝对定位元素,利用 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>

When you click the geolocation, and hide it, you can also move the recenter button to the bottom.当您单击地理位置并将其隐藏时,您还可以将居中按钮移动到底部。 It's not a very nice way to do this, but it works.这不是一个很好的方法,但它有效。

 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';document.getElementById('recenter').style.bottom = '125px';">MENU (CLICK ME)</div> </div>

wrap the geolocation and recenter div within a div and give it to them a class.将地理定位和重定位 div 包装在一个 div 中,并给它们一个类。 then use calc and flex to achieve your requirement like below.然后使用 calc 和 flex 来实现您的要求,如下所示。 don't need to use position and bottom CSS it's a very bad practice for this kind of situation.不需要使用positionbottom CSS,对于这种情况,这是一个非常糟糕的做法。 Happy Solution :)快乐的解决方案:)

 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; } #recenter{ padding:10px; border:1px solid #000; max-width:120px; border-radius:30px; text-align:center; background-color: aliceblue; } #geolocation{ line-height:50px; text-align:center; border: 1px solid black; margin:10px; background-color: aliceblue; } .upper_div{ height: calc(100% - 125px); display: flex; flex-direction: column; justify-content: flex-end; }
 <div id="content"> <div class=upper_div> <div id="recenter">Re-center</div> <div id="geolocation">My address is : 3958 Heron Way - Oregon 97351</div> </div> <div id="menu" onclick="document.getElementById('geolocation').style.display = 'none';">MENU</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM