代码之家  ›  专栏  ›  技术社区  ›  bob.mazzo

如何禁用svg图标

  •  0
  • bob.mazzo  · 技术社区  · 8 年前

    类似于经典的引导禁用状态-

    <button type="button" class="btn btn-lg btn-primary" disabled>Primary button</button>

    disabled Angular Materials svg图标使用 cursor: not-allowed css。但是我想禁用click事件。

    .toolbar-icon-disabled {
        fill: gray;
        cursor: not-allowed;
        pointer-events: none;
      }
      <mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="true"></mat-icon>

    问题是我不能两者兼用 not-allowed 和 pointer-events: none

    在这里你可以看到我最左边的图标显示为禁用,因为 fill 颜色为灰色;但是,如果我添加 .

    left icon disabled

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  1
  •   Learning    8 年前

    建议解决方法: 1) 将mat图标放在另一个锚定标记中,并向该锚定添加curson not allowed。 HTML格式:

    <a [class.linkDisabled]="true"><mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="true"></mat-icon></a>
    

    CSS格式:

    .toolbar-icon-disabled {pointer-events: none;}
    .linkDisabled {cursor: not-allowed;}
    

    2) 保留“cursor:not allowed;”在css中,我假设图标上会有一个事件侦听器来执行一些操作。进入该方法,检查禁用条件,如果为真则返回。例如:

    html格式:

    <mat-icon svgIcon="historical-compare" [class.toolbar-icon-disabled]="flgDisabled" (click)="onClick()"></mat-icon>
    

    方法:

      onClick() {
        if(this.flgDisabled) {
          return
        }
        console.log('called');
      }