简体   繁体   English

在展开一个组件(Material-UI + ReactJS)时,如何保留组件(卡片组件)的位置?

[英]How can I retain the position of components (Card component) while one is expanded (Material-UI + ReactJS)?

I am using an expandable Card component ( http://www.material-ui.com/#/components/card ) and flex for alignment of the components. 我正在使用可扩展的Card组件( http://www.material-ui.com/#/components/card )和flex来对齐这些组件。 But when one expands like the following, the empty spaces are added based on the size of the expanded component to the other components in the same row as the expanded component: 但是,当一个组件按如下所示进行扩展时,将根据扩展组件的大小将空白添加到与扩展组件同一行中的其他组件上:

在此处输入图片说明

But even when any one of the cards is expanded, I would like the unexpanded cards to retain their positions as such and not have empty spaces in the between (for example: I would like Card 1 and Card 4, same for Card 3 and 6, to be like the following even when Card 2 is expanded): 但是,即使任何一张卡都展开了,我也希望未展开的卡能保持原样,并且中间没有空格(例如:我想要卡1和卡4,与卡3和6一样) ,即使在扩充Card 2时也如下所示:

在此处输入图片说明

I looked around but can't seem to find the solution. 我环顾四周,但似乎找不到解决方法。 Any suggestions or guidance would be greatly appreciated. 任何建议或指导将不胜感激。 Thank you in advance. 先感谢您。

Snippet of the code for the first row (it is the same format and style for the second row): 第一行的代码片段(第二行的格式和样式相同):

<div style={{display:'flex'}}>
          <div style={{flex: '1 0'}}>
          <Card style={{ marginLeft: 30, marginRight: 30}}>
            <CardHeader
              title="Card 1"
              subtitle="Subtitle 1"
              actAsExpander={true}
              showExpandableButton={true}
            />
            <CardMedia 
              expandable={true} 
            >
              <img src="image1.PNG" />
            </CardMedia>

            <CardText expandable={true}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Donec mattis pretium massa. Aliquam erat volutpat. Nulla facilisi.
            Donec vulputate interdum sollicitudin. Nunc lacinia auctor quam sed pellentesque.
            Aliquam dui mauris, mattis quis lacus id, pellentesque lobortis odio.
            </CardText>
            <CardActions expandable={true}>
              <FlatButton label="Action 1" />
              <FlatButton label="Action 2" />
            </CardActions>
          </Card>
          </div>

        &nbsp;

          <div style={{flex: '1 0'}}>
          <Card style={{ marginRight: 30}}>
            <CardHeader
              title="Card 2"
              subtitle="Subtitle 2"
              actAsExpander={true}
              showExpandableButton={true}
            />
            <CardMedia 
              expandable={true}
            >
              <img src="image2.PNG" />
            </CardMedia>
            <CardText expandable={true}>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit.
              Donec mattis pretium massa. Aliquam erat volutpat. Nulla facilisi.
              Donec vulputate interdum sollicitudin. Nunc lacinia auctor quam sed pellentesque.
              Aliquam dui mauris, mattis quis lacus id, pellentesque lobortis odio.
            </CardText>
            <CardActions expandable={true}>
              <FlatButton label="Action1" />
              <FlatButton label="Action2" />
            </CardActions>
          </Card>
          </div>

        &nbsp;

        <div style={{flex: '1 0'}}>
        <Card style={{ marginRight: 30}}>
          <CardHeader
            title="Card 3"
            subtitle="Subtitle 3"
            actAsExpander={true}
            showExpandableButton={true}
          />

          <CardMedia 
            expandable={true}
          >
            <img src="image3.PNG" />
          </CardMedia>

          <CardText expandable={true}>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Donec mattis pretium massa. Aliquam erat volutpat. Nulla facilisi.
            Donec vulputate interdum sollicitudin. Nunc lacinia auctor quam sed pellentesque.
            Aliquam dui mauris, mattis quis lacus id, pellentesque lobortis odio.
          </CardText>
          <CardActions expandable={true}>
            <FlatButton label="Action1" />
            <FlatButton label="Action2" />
          </CardActions>
        </Card>
        </div>
      </div>

You might want to check out a masonry layout, example shown here . 你可能想看看砖石布局,例如显示在这里 A CSS-only solution is possible in modern browsers, otherwise use the JS fallback outlined in the article. 在现代浏览器中,可能仅使用CSS解决方案,否则请使用本文概述的JS回退。

Check out the browser support for column-count and column-gap at caniuse . caniuse处查看浏览器对column-countcolumn-gap支持

It might not be the exact solution to your problem (the elements can move around between columns), but at least it's a layout that removes the spaces between elements in a column. 它可能不是解决问题的确切方法(元素可以在列之间移动),但至少它是一种布局,用于删除列中元素之间的空格。

HTML 的HTML

<div class="masonry">
  <div class="item">
    <Card>
    ...
    </Card>
  </div>
  <div class="item">
    <Card>
    ...
    </Card>
  </div>
  ...
</div>

CSS 的CSS

.masonry {
    column-count: 3;
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-gap: 1em;
    -webkit-column-gap: 1em;
    -moz-column-gap: 1em;
}

.item {
    display: inline-block;
    margin: 0 0 1em;
    width: 100%;
}

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

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