简体   繁体   English

使用 Flex 垂直和水平隐藏部分,无需手动计算百分比

[英]Hiding section vertically and horizontally using Flex without manually calculating percentage

I've small problem with hiding sections when they have titles placed on their left side.当标题放在左侧时,我在隐藏部分时遇到了小问题。

在此处输入图像描述

Once you close section (Hide) the top one behaves correctly, however the bottom one when hidden leaves everything in place.关闭部分(隐藏)后,顶部的行为正确,但底部的隐藏时会将所有内容留在原处。

在此处输入图像描述

And I would like it to behave in a way where it hides completely just leaving title header within section.而且我希望它的行为方式完全隐藏,只是将标题 header 留在部分中。

在此处输入图像描述

I can fix it by adding style="flex-basis: 1.4%;"我可以通过添加style="flex-basis: 1.4%;"来修复它to line 593 so code:到第 593 行,所以代码:

        <div class="flexParent flexElement overflowHidden">
            <div class="flexParent flexElement overflowHidden">
                <div class="defaultSection-79s53o1 overflowHidden">
                    <div class="defaultSectionHead-79s53o1">

changes to:更改为:

        <div class="flexParent flexElement overflowHidden">
            <div class="flexParent flexElement overflowHidden" style="flex-basis: 1.4%;">
                <div class="defaultSection-79s53o1 overflowHidden">
                    <div class="defaultSectionHead-79s53o1">

I'm basically overwriting flex-basis value in class FlexElement我基本上是在 class FlexElement 中覆盖 flex-basis 值

            .flexElement {
                flex-basis: 100%;
            }

While this works it's of course subject to not be really dynamic.虽然这可行,但它当然不是真正动态的。 If there's more text, the text is bigger or there are more sections next to each other that will be hidden I guess things will go sideways and my 1.4% will not work.如果有更多的文本,文本更大或者有更多相邻的部分将被隐藏我猜事情会 go 横向和我的 1.4% 将不起作用。 So what would be the proper way to do this so it works for both vertical and horizontal hiding of sections?那么这样做的正确方法是什么,以便它适用于垂直和水平隐藏部分?

Here's an example with 3 sections这是一个包含 3 个部分的示例

在此处输入图像描述

Which requires 2.8% - <div class="defaultSection-svy0khb overflowHidden" style="flex-basis: 2.8%;"> to work.这需要 2.8% - <div class="defaultSection-svy0khb overflowHidden" style="flex-basis: 2.8%;">才能工作。

Here's full code:这是完整的代码:

https://codepen.io/MadBoyEvo/pen/gOrayWL https://codepen.io/MadBoyEvo/pen/gOrayWL

Updated更新

You should make the container hasn't a width value if it collapsed.如果容器折叠,您应该使容器没有宽度值。 In your case, remove the width value from .defaultSection-79s53o1 then add the width value once you need to show the data.在您的情况下,从.defaultSection-79s53o1中删除宽度值,然后在需要显示数据时添加宽度值。 Add a class .--show contains a width value, the class toggled in the show and hide functions.添加一个 class .--show包含一个宽度值,class 在showhide功能中切换。 For the original example codepen .对于原始示例codepen

For multiple panels, remove the width on .defaultSection-* classes and add default classes .--hide for toggling panels and a default .--show class on the static panel.对于多个面板,删除.defaultSection-*类上的宽度并添加默认类.--hide用于切换面板和默认.--show class 在 static 面板上。 You can check a live example codepen .您可以查看一个实时示例codepen

.--show {
  width: 100%;
}

.--hide {
  width: auto;
  min-width: 1.4rem;
}

In show function:显示function:

document.getElementById(obj).parentNode.classList.add('--show');
document.getElementById(obj).parentNode.classList.remove('--hide');

In hide function:隐藏function 中:

document.getElementById(obj).parentNode.classList.remove('--show');
document.getElementById(obj).parentNode.classList.add('--hide');

use like this像这样使用

flex-basis: calc( 1.4 * n );

more about calc refer this: https://developer.mozilla.org/en-US/docs/Web/CSS/calc有关calc的更多信息,请参阅: https://developer.mozilla.org/en-US/docs/Web/CSS/calc

Try using:尝试使用:

  min-width: auto;
  flex-basis: 0;

Or the other way around.或者反过来。

Ultimately, you will have to find your own solution based on what you want exactly.最终,您必须根据自己的确切需求找到自己的解决方案。 Try playing around with properties such as, flex-basis, flex-grow, flex-shring, width, min-width, max-width .尝试使用诸如flex-basis, flex-grow, flex-shring, width, min-width, max-width You can achieve what you want mostly with only these properties.只需这些属性,您就可以实现您最想要的。

If you create a minimum reproducible example and tell clearly how you want it to work, I could help you more.如果您创建一个最小的可重现示例并清楚地说明您希望它如何工作,我可以为您提供更多帮助。

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

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