Affix固钉

将页面元素钉在可视范围。

何时使用

当内容区域比较长,需要滚动页面时,这部分内容对应的操作或者导航需要在滚动范围内始终展现。常用于侧边菜单和按钮组合。

页面可视范围过小时,慎用此功能以免遮挡页面内容。

代码演示

Affix固钉 - 图1

基本

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-affix-basic',
  4. template: `
  5. <nz-affix>
  6. <button nz-button [nzType]="'primary'">
  7. <span>Affix top</span>
  8. </button>
  9. </nz-affix>
  10. <br />
  11. <nz-affix nzOffsetBottom="0">
  12. <button nz-button [nzType]="'primary'">
  13. <span>Affix bottom</span>
  14. </button>
  15. </nz-affix>
  16. `
  17. })
  18. export class NzDemoAffixBasicComponent {}

Affix固钉 - 图2

滚动容器

nzTarget 设置 nz-affix 需要监听其滚动事件的元素,默认为 window

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-affix-target',
  4. template: `
  5. <div class="scrollable-container" #target>
  6. <div class="background">
  7. <nz-affix [nzTarget]="target" id="affix-container-target">
  8. <button nz-button [nzType]="'primary'">
  9. <span>Fixed at the top of container</span>
  10. </button>
  11. </nz-affix>
  12. </div>
  13. </div>
  14. `,
  15. styles: [
  16. `
  17. .scrollable-container {
  18. height: 100px;
  19. overflow-y: scroll;
  20. }
  21. .background {
  22. padding-top: 60px;
  23. height: 300px;
  24. background-image: url(//zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg);
  25. }
  26. `
  27. ]
  28. })
  29. export class NzDemoAffixTargetComponent {}

Affix固钉 - 图3

固定状态改变的回调

可以获得是否固定的状态。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-affix-on-change',
  4. template: `
  5. <nz-affix [nzOffsetTop]="120" (nzChange)="onChange($event)">
  6. <button nz-button>
  7. <span>120px to affix top</span>
  8. </button>
  9. </nz-affix>
  10. `
  11. })
  12. export class NzDemoAffixOnChangeComponent {
  13. onChange(status: boolean) {
  14. console.log(status);
  15. }
  16. }

API

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzAffixModule } from 'ng-zorro-antd';

nz-affixcomponent

成员说明类型默认值
[nzOffsetBottom]距离窗口底部达到指定偏移量后触发number-
[nzOffsetTop]距离窗口顶部达到指定偏移量后触发number0
[nzTarget]设置 nz-affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数string|HTMLElementwindow
(nzChange)固定状态改变时触发的回调函数EventEmitter<boolean>-

注意:nz-affix 内的元素不要使用绝对定位,如需要绝对定位的效果,可以直接设置 nz-affix 为绝对定位:

  1. <nz-affix style="position: absolute; top: 10px, left: 10px">
  2. ...
  3. </nz-affix>