I want to be able to change the src path based on the css class, when the user hovers li a:hover or when that li is active li a.active, the src path will be directed to "http://contohaja/inihover" but when it’s not, it’ll be directed to "http://contohaja/inipasbiasaaja"
mycomponent.component.html
<li>
<a
aria-expanded="false"
[routerLink]="['/restaurant-a']"
[routerLinkActive]="['active']">
<img class="logo-icon mr-3"/>
</a>
</li>
I don’t know how to do this in angular. Are there any tips to make it possible?
3
Answers
You can do that with pure CSS, don’t need javascript for that:
One option is not use a img tag else a div
I supouse your imgs are in your folder assets/img, you can also use https
Update if we want to have a "fade efect" we can also use some like
And a .css
a stackblitz
You can approach this with state variables
isHovered
andisActive
in your component,Set your src attribute to the getImagePath() function and the mouse enter and leave event bindings each to a handler function
onHover
andonHoverOut
The handler function can control the state variables and class changing logic. If user hovers li a element or it’s active, the src path will be
http://contohaja/inihover
, otherwisehttp://contohaja/inipasbiasaaja