【问题标题】:Remove padding from fxLayoutGap last element of row从 fxLayoutGap 行的最后一个元素中删除填充
【发布时间】:2019-02-09 14:08:20
【问题描述】:

我在使用 fxLayoutGap 时遇到问题。我列表的最后一列有一个填充,我不想要它。

如何保持 fxLayoutGap(卡片之间的空间)并从最后一列移除填充?谢谢!

<div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
  <div fxHide.lt-sm="true" fxFlex="calc(25%-20px)" fxFlex.md="33">
    <div fxFlexFill fxLayoutAlign="center center">
      <mat-card (click)="addAdvert()" class="mat-card-add-button">
        <div fxLayout="row" fxLayoutAlign="center center" fxFlex="100%">
          <span style="font-size:32px;text-align:center">+<br />test</span>
        </div>
      </mat-card>
    </div>
  </div>
  <div fxFlex="calc(25%-20px)" fxFlex.md="33" *ngFor="let product of products | slice:0:3">
    <div style="border:1px solid #ccc" fxFlexFill fxLayoutAlign="center stretch">
      <mat-card class="ad">
        <img mat-card-image src="https://material.angular.io/assets/img/examples/shiba2.jpg" alt="Photo of a Shiba Inu">
        <mat-card-title>test</mat-card-title>
        <mat-card-content>
          <p>test</p>
        </mat-card-content>
        <mat-divider [inset]="true"></mat-divider>
        <mat-card-actions align="end">
          <button mat-icon-button matTooltip="edit" matTooltipShowDelay="800">
            <mat-icon>mode_edit</mat-icon>
          </button>
          <button mat-icon-button matTooltip="delete" matTooltipShowDelay="800">
            <mat-icon>delete</mat-icon>
          </button>
        </mat-card-actions>
      </mat-card>
    </div>
  </div>
</div>

问题(红色):

更新

我使用了 Abdul Rafay 的解决方案,但这里出现了一个新问题:

【问题讨论】:

  • fxLayoutAlign="center center" 是 ngFor 父元素的最佳解决方案

标签: angular angular-material angular-flex-layout


【解决方案1】:

只要做这个小技巧。

移除 fxLayoutGap 并为子元素添加边距,并将相同边距的负值添加到父元素。

<div fxLayout="row wrap" style="margin-right: -16px;">
    <div fxFlex="calc(50% - 16px)" style="margin-right: 16px;">

    </div>
    <div fxFlex="calc(50% - 16px)" style="margin-right: 16px;">

    </div>
</div>

【讨论】:

    【解决方案2】:

    编辑:尝试在此处通过ngFor 为您的元素分配类:

    <div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
      <div fxFlex="calc(25%-20px)" fxFlex.md="33"
        *ngFor="let product of products;let i = index"
        [ngClass]="'product-'+getClassName(i)">
        ...
    

    .ts

      counter: number = 0;
    
      getClassName(index) {
        if(index%4 === 0)
          this.counter = 0;
        this.counter++;
        switch(this.counter) {
          case 1:
            return "first"
          case 2:
            return "second"
          case 3:
            return "third"
          default:
            return "fourth"
        }
      }
    

    然后添加这些css 样式:

    .product-first[fxFlex] {
      padding-right: 20px !important;
    }
    .product-second[fxFlex] {
      padding-left: 6.66px !important;
      padding-right: 13.34px !important; 
    }
    .product-third[fxFlex] {
      padding-left: 13.34px !important;
      padding-right: 6.66px !important;
    }
    .product-fourth[fxFlex] {
      padding-left: 20px !important;
      padding-right: 0 !important;
    }
    

    我不确定这是否是最好的方法,但它确实有效。

    旧答案:只需添加 css 样式:

    [fxFlex]:last-of-type { padding-right:0px !important; }
    

    如果多于一行:

    [fxFlex]:nth-child(4n) { padding-right:0px !important; }
    

    如果有更多的 fxFlex 元素,你可以分配一个类:

    <div fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="20px grid" style="margin-right:-20px;">
        <div class="padding-fix" fxHide.lt-sm="true" fxFlex="calc(25%-20px)" fxFlex.md="33">
        ...
    
    .padding-fix[fxFlex]:nth-child(4n) { padding-right:0px !important; }
    

    【讨论】:

    • 看起来不错,但这种风格占用了我页面的所有 fxFlex。我怎样才能指定其中之一?非常感谢您的帮助:)
    • 完美!非常感谢:)
    • 我有点兴奋地找到了解决方案,但我没有发现您的回答有什么问题。最后一项比其他项稍大。我将在屏幕截图中更新我的第一篇文章。
    • 你可以试试这个:.padding-fix[fxFlex] { padding-right:23.33px !important; }.padding-fix[fxFlex]:nth-child(4n) { padding-right:0px !important; }
    • 不幸的是,同样的问题。最后一项的图片比其他的大。
    【解决方案3】:

    您可以在 ngFor 中使用索引指定行中的最后一项,比如说 4 列:

    [class.flexLastItem]="((i-3) % 4 === 0)"
    

    终于只有css了

    .flexLastItem{ margin-right: 0 !important; }
    

    【讨论】:

      【解决方案4】:

      在 stackoverflow 上找到:您必须在间隙中添加网格。

      fxLayoutGap="5rem grid"
      

      查看文档: https://github.com/angular/flex-layout/wiki/fxLayoutGap-API

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-23
        • 2020-02-19
        • 2012-01-05
        • 1970-01-01
        • 2014-06-28
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多