简体   繁体   English

在我 SlideUp 另一个 div 后,如何阻止我的中心 div 更改 position?

[英]How can I stop my center div from changing position after I SlideUp another divs?

I'm creating simple page with a <header> and a <section> .我正在创建带有<header><section>的简单页面。 In the section I have 3 divs and I am positioning them with display: flex;在该section中,我有 3 个 div,我用display: flex; and justify-content: space-between .justify-content: space-between

The problem is that I also use JS slideToggle() on two of them (extreme ones).问题是我还在其中两个(极端的)上使用了 JS slideToggle() )。 It is changing the layout of my center div after they are going up.在它们上升后,它正在改变我的中心 div 的布局。 How can I do it so that my center div doesn't change position after one of the others is slid up?我该怎么做才能使我的中心 div 在其中一个向上滑动后不会更改 position?

 $(document).ready(function() { $('#playlist').click(function() { $('#nav').slideToggle(); }); }); $(document).ready(function() { $('#songs').click(function() { $('#listSongs').slideToggle(); }); });
 section { display: flex; justify-content: space-between; } #listSongs { margin-top: 50px; height: 550px; background-color: black; border-radius: 5px; font-size: 25px; width: 200px; } #listSongs p { margin-top: 5px; width: 200px; text-align: center; overflow: hidden; height: 35px; line-height: 50px; color: white; } #player { color: red; } #nav { margin-top: 50px; height: 550px; background-color: black; border-radius: 20px; width: 200px; }.hidden { margin-top: 50px; height: 550px; background-color: black; border-radius: 20px; width: 200px; visibility: hidden; } #nav p { text-align: center; } #nav ul { list-style-type: none; text-align: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section> <div id="listSongs"> <p>Authors:</p> <div class="after"></div> </div> <div id="player"> <p>something</p> </div> <div id="nav"> <p>something</p> </div> </section>

The issue is because when the slideUp / slideDown / slideToggle methods complete, they set display: none on the target element.问题是因为当slideUp / slideDown / slideToggle方法完成时,它们在目标元素上设置了display: none This is what causes the layout of your page to shift.这就是导致页面布局发生变化的原因。

To workaround, and improve the animation, you can use CSS instead.要解决并改进 animation,您可以改用 CSS。 Use the transition property to animate the height setting.使用transition属性为height设置设置动画。 Then you can toggle a class which sets height: 0 on the target element.然后你可以切换一个 class ,它在目标元素上设置height: 0 Try this:尝试这个:

 $(document).ready(function() { $('#playlist').click(function() { $('#nav').toggleClass('hide'); }); $('#songs').click(function() { $('#listSongs').toggleClass('hide'); }); });
 body { background-color: #CCC; } section { display: flex; justify-content: space-between; } section > div.panel { margin-top: 50px; height: 550px; background-color: black; border-radius: 5px; font-size: 25px; width: 200px; transition: height 0.4s; overflow: hidden; } section > div.panel.hide { height: 0; } section > div.panel p { margin-top: 5px; width: 200px; text-align: center; overflow: hidden; height: 35px; line-height: 50px; color: white; } #player { color: red; } #nav { border-radius: 20px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="playlist">Playlist</button> <button id="songs">Songs</button> <section> <div id="listSongs" class="panel"> <p>Authors:</p> <p>lorem ipsum</p> <div class="after"></div> </div> <div id="player"> <p>something</p> </div> <div id="nav" class="panel"> <p>something</p> </div> </section>

Note that I also rearranged some of the CSS to make it more generic with less repetition.请注意,我还重新排列了一些 CSS 以使其更通用且重复更少。

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

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