【问题标题】:Auto fill and adjust cards in given container [duplicate]自动填充和调整给定容器中的卡片[重复]
【发布时间】:2019-09-29 02:25:37
【问题描述】:

我有很多 Angular mat-cards,即不确定数量的带有不同尺寸图像的卡片。

我正在尝试实现的是根据容器自动填充和调整此类卡片,例如this

现在上面的例子是使用 4 列并且可能使用网格。

现在,我该如何使用卡片来实现它?我尝试使用 flex 但不知何故无法正确实现它。这是我毫无价值的尝试。

<div class="mat-card-container" style="
display: flex;
justify-content: center;    /* Add this */
flex-wrap: wrap;

">
<div class="mat-card-holder" style="
margin:0.6em;
width:220px;
" *ngFor="let art of arts.records">
    <mat-card class="art-card" >
      <mat-card-header>
        <mat-card-title>{{ art.TITLE }}</mat-card-title>
        <mat-card-subtitle>{{ art.AUTHOR }}</mat-card-subtitle>
      </mat-card-header>
      <img mat-card-image src="http://localhost/piwigo/{{ art.URL }}" alt="Photo of a Shiba Inu">
      <mat-card-content>
          {{art.TECHNIQUE}} / {{art.SCHOOL}} / {{art.FORM}} / {{art.TYPE}} / {{art.LOCATION}}
      </mat-card-content>
    </mat-card>
</div>
</div>

这给了我这么多,但仍有很多空白需要弥补。

这只是将它们排列成列,我如何在容器中动态填充和调整它们?假设如果容器大小发生变化,卡片应该重新排列而不改变它们自己的 css,即尺寸。我的代码是否正确?我该如何填补两者之间的空白?

【问题讨论】:

    标签: css flexbox css-grid image-gallery


    【解决方案1】:

    据我所知,您希望实现的这种布局样式无法通过 css 本身实现。

    在 javascript helper library 的帮助下,这很容易实现。当您使用 Angular 时,this is 是前一个库的 Angular 包装器,它允许进行这样的设计。

    从我的手机回复,无法真正重现您的确切 ui,但在此处逐字复制他们的示例实现,

    App.module.ts

    import { MasonryModule } from '@thisissoon/angular-masonry';
    
    const masonryProviders = [
      { provide: Masonry, useFactory: () => window['Masonry'] },
    ];
    
    @NgModule({
      imports: [MasonryModule.forRoot(masonryProviders)],
    })
    export class AppModule {}
    

    angular.json

    "scripts": [
      "../node_modules/masonry-layout/dist/masonry.pkgd.js"
    ],
    

    app.component.ts

    export class AppComponent implements AfterViewInit, OnDestroy {
      @ViewChild('grid') public grid: ElementRef;
    
      public masonryInstance: MasonryInstance;
    
      public cards = cards;
    
      constructor(@Inject(Masonry) public masonry) {}
    
      ngAfterViewInit() {
        const options: MasonryOptions = {
          itemSelector: '.card',
          columnWidth: '.card',
          gutter: 20,
          fitWidth: true,
        };
        this.masonryInstance = new this.masonry(this.grid.nativeElement, options);
      }
    
      layout() {
        this.masonryInstance.layout();
      }
    
      ngOnDestroy() {
        this.masonryInstance.destroy();
      }
    }
    

    app.component.css

    :host {
      display: block;
      margin-top: 1rem;
    }
    
    .grid {
      margin: 0 auto;
    }
    
    .card {
      display: inline-block;
      margin-bottom: 1rem;
      width: 18rem;
    }
    

    app.component.html

    <div class="grid" #grid>
      <div class="card" *ngFor="let card of cards">
        <div class="card-body">
          <h5 class="card-title">{{ card.title }}</h5>
          <p class="card-text">{{ card.text }}</p>
          <a href="#" class="btn btn-primary">Go somewhere</a>
        </div>
      </div>
    </div>
    

    【讨论】:

    • 感谢您为我指明正确的方向,我最终改用 this 库(我是一个懒惰的流浪汉)。但是,this 也是非常简单的轻量级库。
    • 如何调用ngx-gallery的layout()函数。我似乎无法获取它。
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多