Angular 5-鼠标进入时显示按钮,鼠标离开时隐藏按钮 [英] Angular 5 - On mouse enter show a button and on mouse leave hide a button
问题描述
当用户将鼠标悬停在列表项上时,我希望显示一个按钮.当用户离开列表项时,我希望按钮不显示.
When a user hovers over a list item i want a button to be displayed. When the user leaves the list item i want the button to not be displayed.
我遇到了mouseenter事件和mouseleave.
I have come across a mouseenter event and a mouseleave.
.html
<mat-list-item (mouseenter)="enter($event)" (mouseleave)="leave($event)" class="chat-message-body" *ngIf="auth._id !== message.author._id" fxLayoutAlign=""
dir="ltl">
<div matLine>
<b>{{message.author.profile.username}} </b>
<span>{{message.created_at | date:'shortTime'}} </span>
</div>
<button mat-icon-button>
<mat-icon aria-label="">keyboard_arrow_down</mat-icon>
</button>
<span matLine> {{message.body}} </span>
<img matListAvatar class="img-box" src="http://via.placeholder.com/30x30" alt="...">
</mat-list-item>
.ts
enter(e) {
console.log("enter");
}
leave(e) {
console.log("leave");
}
除了声明这些功能外,我不知道如何根据用户是否进入列表项块或离开了该列表项中的按钮来显示和隐藏它.
Apart from declaring these functions, i dont know how to target the button within this list item to show and hide it depending on if the user has entered the list-item block or left.
推荐答案
我为此创建了一个解决方案.
I have created a solution for this.
当用户"mouseenters" mat-item-list块时,我将变量设置为true并在按钮中添加ng-if,以便当该变量为true时显示,并且用户从mat-"mouseleaves"- item-list变量设置为false.假设您只有一个mat-item-list,这可以很好地工作.
When a user "mouseenters" the mat-item-list block i set a variable to true and add a ng-if in the button so when the variable is true it shows and when the user "mouseleaves" from the mat-item-list the variable is set to false. This works fine assuming you only have a single mat-item-list.
具有多种含义,当用户进入该块时,我需要一个变量来存储索引值,并且我确定索引值集是否与悬停相同.如果是,将显示该按钮.
Having multiple means i need a variable to store an index value when the user enters the block and i determine if the index value set is the same one as im hovering over. If it is the button will be shown.
.html
<mat-list dense>
<ng-template ngFor let-message [ngForOf]="conversation?.messages" let-i="index" let-odd="odd" [ngForTrackBy]="trackById">
<mat-list-item (mouseenter)="enter(i)" (mouseleave)="leave(i)" class="chat-message-body" *ngIf="auth._id === message.author._id"
fxLayoutAlign="" dir="rtl">
<img matListAvatar class="img-box" src="http://via.placeholder.com/30x30" alt="...">
<button mat-icon-button *ngIf="hoverIndex == i">
<mat-icon aria-label="">keyboard_arrow_down</mat-icon>
</button>
<div matLine>
<b>{{message.author.profile.username}} </b>
<span>{{message.created_at | date:'shortTime'}} </span>
</div>
<span matLine> {{message.body}} </span>
</mat-list-item>
</ng-template>
</mat-list>
.ts
enter(i) {
this.hoverIndex = i;
}
leave(i) {
this.hoverIndex = null;
}
与尝试找到特定的dom元素并向其添加display:block/none相比,此解决方案似乎更干净.
This solution seems more cleaner than trying to find a specific dom element and adding a display:block/none to it.
这篇关于Angular 5-鼠标进入时显示按钮,鼠标离开时隐藏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!