Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show or hide element on hover in angular

I have an angular material card with an "x" button.

just wanted the "x" button to be hidden normally and be shown while hovering on the card.
code for the card:

<mat-card>
    <mat-card-title>hello title</mat-card-title>
    <mat-card-content>hello content</mat-card-content>
    <mat-card-actions>
        <button mat-icon-button><mat-icon>close</mat-icon></button>
    </mat-card-actions>
</mat-card>

thought of using ngClass but didn't know how to tell it if user isHovering or not

like image 205
hamthiii Avatar asked Jun 18 '26 12:06

hamthiii


2 Answers

Try this:

<mat-card (mouseover)="showButton = true" (mouseleave)="showButton = false" >
    <mat-card-title>hello title</mat-card-title>
    <mat-card-content>hello content</mat-card-content>
    <mat-card-actions>
        <button mat-icon-button *ngIf = "showButton"><mat-icon>close</mat-icon></button>
    </mat-card-actions>
</mat-card>

Declare the showButton variable in your component .ts file like this:

showButton: boolean = false;
like image 148
Hasan Fathi Avatar answered Jun 20 '26 02:06

Hasan Fathi


There is many way of soluation

1.you can set using scss or css

html file

<mat-card class="CARD">
   <mat-card-title>hello title</mat-card-title>
   <mat-card-content>hello content</mat-card-content>
   <mat-card-actions>
    <button class="CLOSE" mat-icon-button><mat-icon>close</mat-icon></button>
   </mat-card-actions>
</mat-card>

.scss file

    .CLOSE {
        display : none;
      }
    .CARD {
           &:hover {
               .CLOSE {
                display : block !important
              }
            }
         }

2. Using variables .ts file

 btnVisible: boolean = true;

.html file

<mat-card (mouseover)="btnVisible = false" (mouseleave)="btnVisible = true" >
<mat-card-title>hello title</mat-card-title>
<mat-card-content>hello content</mat-card-content>
<mat-card-actions>
    <button mat-icon-button *ngIf ="btnVisible"><mat-icon>close</mat-icon></button>
</mat-card-actions>

Hope it will usefull for all !

like image 34
Dako patel Avatar answered Jun 20 '26 01:06

Dako patel