So my goal is to create a mouseenter/mouseexit event for an individual li's child elements. For my app, when a user mouseenters an li elememt, it's child element div class='thumbs' is added to the DOM through a component boolean property called "hover" -- *"ngIf='hover'"
My problem is, when I mousenter over an indivual li elememt, all the thumb icons are shown instead of the just the individual li's thumb icons.
Here is a video higlighting my problem:
HTML:
<ul> <!-- Each song on the album -->
<li class="song-block"
*ngFor='let song of songsToDisplay'
(click)="getSong(song)"
(mouseenter)="hoverStateIn()"
(mouseleave)="hoverStateOut()">
<div class="song-card"
(click)="addPlay(song)">
<p *ngIf="!song.isPlaying"
class="song-number">{{song.tracknumber}}</p>
<i *ngIf="song.isPlaying" class="fa fa-play"></i>
<p class="song-name">{{song.name}}</p>
<p class="song-length">{{song.length}}</p>
<div class="thumbs"
*ngIf="hover"> <!-- Thumbs section -->
<i class="fa fa-thumbs-up"></i>
<i class="fa fa-thumbs-down"></i>
</div>.....
</ul>
TypeScript:
hover: boolean = false;
hoverStateIn(){
this.hover = true
}
hoverStateOut(){
this.hover = false;
}
You can simply set the hover boolean value to individual item of the *ngFor.
<ul>
<!-- Each song on the album -->
<li class="song-block"
*ngFor='let song of songsToDisplay'
(click)="getSong(song)"
(mouseenter)="song.hover=true"
(mouseleave)="song.hover=false">
<div class="song-card"
(click)="addPlay(song)">
<p *ngIf="!song.isPlaying"
class="song-number">{{song.tracknumber}}</p>
<i *ngIf="song.isPlaying" class="fa fa-play"></i>
<p class="song-name">{{song.name}}</p>
<p class="song-length">{{song.length}}</p>
<div class="thumbs"
*ngIf="song.hover">
<!-- Thumbs section -->
<i class="fa fa-thumbs-up"></i>
<i class="fa fa-thumbs-down"></i>
</div>.....
</ul>
A bit late, but hope it would be useful.
Using template reference variable is a quick simple way.
<div #hoverField></div>
<li (mouseenter)="hoverField.style.display = 'block';
(mouseleave)="hoverField.style.display = 'none';>
</li>
Full code like this:
<ul> <!-- Each song on the album -->
<li class="song-block"
*ngFor='let song of songsToDisplay'
(click)="getSong(song)"
(mouseenter)="hoverField.style.display = 'block';"
(mouseleave)="hoverField.style.display = 'none';">
<div class="song-card"
(click)="addPlay(song)">
<p *ngIf="!song.isPlaying"
class="song-number">{{song.tracknumber}}</p>
<i *ngIf="song.isPlaying" class="fa fa-play"></i>
<p class="song-name">{{song.name}}</p>
<p class="song-length">{{song.length}}</p>
<div class="thumbs"
#hoverField> <!-- Thumbs section -->
<i class="fa fa-thumbs-up"></i>
<i class="fa fa-thumbs-down"></i>
</div>.....
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With