【问题标题】:Sticky footer in Angular 2 MaterialAngular 2 Material中的粘性页脚
【发布时间】:2018-03-25 04:10:35
【问题描述】:

我现在已经搜索和搜索了大约 3 个小时,因为我不想问,但是我怎样才能在底部保留一个 'footer' 变量而不是固定在底部,所以如果内容我的空间很小,它不会只停留在页面的中间,但是如果我有很多信息,它就不会锁定在页面的底部并在您滚动时坐在数据上方

我尝试了几种方法,包括: https://david-kerwick.github.io/2017/01/14/material-2-flex-layout-making-a-sticky-footer.html 大多数与此相关的问题我已经测试过,但都失败了,或者似乎根本不起作用。

这是我当前的代码:

<div class="content">
  <app-navbar></app-navbar>
  <app-footer></app-footer>
</div>

&lt;app-content&gt;&lt;/app-content&gt; 位于导航栏中,因为导航栏控制全页侧导航。

整个应用导航栏如下所示:

<mat-sidenav-container fullscreen>

  <mat-sidenav mode="push" #sidenav>
    <div fxLayout="column">
      <mat-toolbar fxLayoutAlign="center center" color="primary">
        <button mat-icon-button routerLink="/home" (click)="sidenav.close()">
          <mat-icon>keyboard_backspace</mat-icon>
        </button>
      </mat-toolbar>
      <button mat-button routerLink="/dashboard" (click)="sidenav.close()" >Information</button>
      <button mat-button routerLink="/second" (click)="sidenav.close()" >Web tools</button>
    </div>
  </mat-sidenav>
  <mat-toolbar color="primary" class="fixed-navbar mat-elevation-z10">
    <button mat-icon-button (click)="sidenav.open()" fxHide="false" fxHide.gt-xs>
      <mat-icon>menu</mat-icon>
    </button>

    <div fxLayout="row">
      <button fxLayout="flex-shrink: 0;" mat-button class="title" style="font-size: 25px;">{{this.title}}</button>
      <button fxShow="false" fxShow.gt-xs mat-button routerLink="/dashboard" [matMenuTriggerFor]="infoMenu">Information</button>
      <button fxShow="false" fxShow.gt-xs mat-button routerLink="/second" [matMenuTriggerFor]="toolsMenu">Web tools</button>
    </div>
    <!-- fxFlex will fill the empty space and push the following items to the right -->
    <div fxFlex></div>
    <button mat-icon-button>
      <mat-icon>face</mat-icon>
    </button>
  </mat-toolbar>
  <app-container></app-container>

</mat-sidenav-container>

而页脚只是一个超级基本的组件,看起来像这样:

<mat-toolbar>
  <div class="container">
    <span>this is a toolbar</span>
  </div>
</mat-toolbar>

到目前为止,只有我应用的样式是这样的:

@import url('https://fonts.googleapis.com/css?family=Comfortaa');
.title {
  font-family: "Comfortaa";
}
html, body {
  height: 100%;
  margin: 0;
}
.content {
  min-height: 100%;
}

在全局 style.css 文件中。

【问题讨论】:

  • 值得注意的是,即使是带有 sidenav 示例的官方固定页眉/页脚也存在同样的问题。它可能只是不可行的 atm,我们可能不得不等待材料的更多更新

标签: angular flexbox material-design angular-material2


【解决方案1】:

一种使用 Flexbox 的方法:

当我们使用 Flexbox 时,我们可以获得更清洁的解决方案。此外,我的解决方案将涵盖页面的第一个组件应占高度的 100%。这通常需要适当地定位元素或使用背景。该代码与 Material 2 的当前版本相匹配 - 在撰写本文时是 2.0.0-beta.12。

标记:

<mat-sidenav-container class="all-wrap" fullscreen>
  <mat-sidenav #sidenav>
    <mat-list>
      <mat-list-item [routerLink]="['/']"> Foo</mat-list-item>
      <mat-list-item [routerLink]="['/bar']"> Bar</mat-list-item>
    </mat-list>
  </mat-sidenav>

  <div class="page-wrap">
    <header role="banner">
      <mat-toolbar color="primary">
        <button
          type="button"
          mat-icon-button
          (click)="sidenav.open()"
          title="Open sidenav">
          <mat-icon>menu</mat-icon>
        </button>
        Your Toolbar
      </mat-toolbar>
    </header>
    <main class="content">
      <router-outlet></router-outlet>
    </main>
    <footer>
      Your sticky footer with a variable height.
    </footer>
  </div>
</mat-sidenav-container>

样式:

/*
 * Actual Sticky Footer Styles
 */
.all-wrap {
  min-height: 100vh;
}

.page-wrap {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.content {
  flex: 1;
}


/*
 * Make the Component injected by Router Outlet full height:
 */
main {
  display: flex;
  flex-direction: column;
  > *:not(router-outlet) {
    flex: 1;
    display: block;
  }
}

您可以在我写的 Blogpost 中找到更详细的描述,因为我对在这里找到的解决方案不满意。还有一个demo

【讨论】:

  • 只想说这个解决方案仍然很出色,并且可以与最新版本的 Material (5) 和最新版本的 Angular 一起使用 (5)
  • 我不明白这如何使页脚变粘,我没有看到任何 CSS 应用于页脚?身份证
  • @AlexanderMills 很好的问题,页脚确实没有改变。取而代之的是包含div.page-wrap 的内容是一个弹性框列,然后将main.content 设置为flex:1。所以他占据了所有空间。
  • 你实际上可以在弹性盒子上玩这个the-echoplex.net/flexyboxes/…
【解决方案2】:

下面是几行的解决方案:

app.component.html:

<div fxLayout="column" fxFlexFill>
    <app-header></app-header> // your header
    <div fxFlex>
        <router-outlet></router-outlet> // your content
    </div>
    <app-footer></app-footer> // your footer
</div>

styles.css:

html, body {
    height: 100%;
    box-sizing: border-box;
    margin: 0;
}

另一种选择如果您更喜欢填充页脚而不是内容:

app.component.html:

<div fxLayout="column" style="height: 100%;">
    <app-header></app-header> // your header
    <router-outlet></router-outlet> // your content
    <app-footer fxFlexOffset="auto"></app-footer> // your footer
</div>

styles.css:

html, body {
    height: 100%;
}

【讨论】:

    【解决方案3】:

    答案可以在这里找到: Sticky footer at the bottom in Angular 2

    解决方案

    app {
      min-height: 100%;
      width: 100%;
      margin-bottom: -271px;
      display: table;
    }
    
    header-main,
    router-outlet,
    footer{
      display: table-row;
    }
    
    header-main {
     height: 60px;
    }
    
    router-outlet {
      position: absolute;
    }
    
    footer {
      height: 271px;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 1970-01-01
      • 2019-11-16
      • 2016-04-15
      • 2013-02-23
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      相关资源
      最近更新 更多