【问题标题】:how to disable matRipple on a div when clicking buttons inside that div?单击该div内的按钮时如何禁用该div上的matRipple?
【发布时间】:2021-04-21 03:40:59
【问题描述】:

我正在用 angular 11 创建这个图片上传组件

我有一个divmatRipple 的拖放区,所以每当用户拖放文件时,它都会显示涟漪效应。在那个 div 里面我有按钮可以左右旋转。当我单击这些按钮时,也会触发放置区 div 的涟漪效应。 有没有办法禁用它?因此,如果用户单击该 div 中的按钮,该 div 的涟漪效应不会发生?我认为通过将event.stopPropagation() 添加到按钮事件处理程序中它会停止它,但它没有。

这是我的组件:

<div class="container">
  <div class="box" style="width: 264px; height: 264px;transition: transform 0.5s" [style.transform]="'rotate(' + this.imageRotation + 'deg)'">
    <img style="max-width: 264px; max-height: 264px; position: relative" [src]="selectedFile?.src ? selectedFile?.src : previewImageUrl ? previewImageUrl : null" /></div>
  <div class="box stack-top">
    <div class="dropzone" comTuxinUpArea (fileDropped)="processFile($event)" matRipple>
      <input type="file" id="fileDropRef" accept="image/jpeg,image/png,image/webp" (change)="processFile($event.target)" />
      <h3 i18n>drag and drop file here</h3>
      <h3 i18n>or</h3>
      <label for="fileDropRef" i18n>Browse for file</label>
      <div fxLayout="row" fxLayoutAlign="center center" id="rotate-div">
        <button mat-icon-button *ngIf="this.selectedFile" (click)="rotateLeft()"><mat-icon>rotate_left</mat-icon></button>
        <button mat-icon-button *ngIf="this.selectedFile" (click)="rotateRight()"><mat-icon>rotate_right</mat-icon></button>
      </div>
    </div>
  </div>

旋转按钮事件处理程序:

 rotateLeft() {
    this.imageRotation+=90;
  }

  rotateRight() {
    this.imageRotation-=90;
  }

【问题讨论】:

  • 你尝试过 event.stopPropagation() 的 mousedown 吗?

标签: angular angular-material stoppropagation


【解决方案1】:

另一个解决方案有点棘手,不那么花哨,默认情况下直接从模板中禁用 matRipple 并手动处理父元素点击事件:

my-app.component.html

<div *ngFor="let item of items; index as i" matRipple [matRippleDisabled]="true" (click)="toggleView(i)">
  ...
  <button mat-button (click)="$event.stopPropagation(); doYourStuff()"></button>
</div>

my-app.component.ts

@ViewChildren(MatRipple) public matRipples!:QueryList<MatRipple>;

public toggleView(i:number):void {
  this.matRipples.get(i)!.launch({ centered: true });
}

它比 @rishabhsharma 的解决方案效果更好,因为它甚至适用于触摸屏(鼠标较少,因此没有光标事件)设备。

希望对某人有所帮助。

【讨论】:

    【解决方案2】:

    在您的 div 上使用 [matRippleDisabled]="someVariable"。在您的按钮上,当您将鼠标悬停在按钮上时,您可以在 true 和 false 之间更改 someVariable 的值。

    在您的按钮上使用: (mouseenter)="someVariable=true" (mouseleave)="someVariable=false" 启用或禁用外部 div 的波纹效果。

    【讨论】:

    • 别忘了在组件ts文件中声明变量
    • 只在有光标的设备上工作。尝试使用触摸屏设备,它不会工作......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多