Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change image src on mouseover and mouseleave in angular4 or above version?

Tags:

angular6

I am very new to angular and I want to change src value using mouse over events. How we can do it in angular 4 or above version? Please help me.

export const ROUTES: RouteInfo[] = [
{ path: '/dashboard', title: 'Dashboard',  icon: 'fa fa-tachometer', class: '' ,name:'',src:"../../../../assets/images/header-icon.png",srcOn:"../../../../assets/images/dashboard_highlighted.png"},
{ path: '/forms', title: 'Forms',  icon:'fa fa-eyedropper', class: '',name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/tables', title: 'Desk Blotter',  icon:'fa fa-pencil', class: '' ,name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/bs-element', title: 'Admin',  icon:'fa fa-picture-o', class: '' ,name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:""},
{ path: '/login', title: 'Logout',  icon:'fa fa-power-off', class: '',name:'',src:"../../../../assets/images/dashboard_highlighted.png",srcOn:"" },
];

<li *ngFor="let menuItem of menuItems" routerLinkActive="active" class="{{menuItem.class}}" >
        <a [routerLink]="[menuItem.path]" name="{{menuItem.name}}">
           <img src="{{menuItem.src}}" style="width:66%;"  (mouseenter)="'src=''{{menuItem.srcOn}}'"   id="{{menuItem.title}}"/> {{menuItem.title}}
        </a>
    </li>
like image 203
sireesha j Avatar asked Jan 02 '23 07:01

sireesha j


1 Answers

Let's look at this to see how to work with a model instead of rewriting element properties:

<img [src]="imgSrc"
           (mouseover)="imgSrc = 'http://www.impresabaraldo.it/Blog/wp-content/uploads/2015/05/casa-ecologica-vicenza.jpg'"
           (mouseout)="imgSrc = 'https://www.ediltecnico.it/wp-content/uploads/2017/06/casa-300x223.png'">

A possibile solution to your problem could be something like this:

export const ROUTES: RouteInfo[] = [
  { path: '/dashboard', title: 'Dashboard',  icon: 'fa fa-tachometer', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/header-icon.png",srcOn:"../../../../assets/images/dashboard_highlighted.png"},
  { path: '/forms', title: 'Forms',  icon:'fa fa-eyedropper', class: '',name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
  { path: '/tables', title: 'Desk Blotter',  icon:'fa fa-pencil', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
  { path: '/bs-element', title: 'Admin',  icon:'fa fa-picture-o', class: '' ,name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:""},
  { path: '/login', title: 'Logout',  icon:'fa fa-power-off', class: '',name:'',src: '', srcOut: "../../../../assets/images/dashboard_highlighted.png",srcOn:"" },
];

Note that we have a src property and a srcOn and a srcOut. The idea is to bind images src property to this json object src property, then updating json object src property according to your mouse events

<li *ngFor="let menuItem of menuItems" routerLinkActive="active" class="{{menuItem.class}}" >
    <a [routerLink]="[menuItem.path]" name="{{menuItem.name}}">
      <img style="width:66%;"  id="{{menuItem.title}}"
           [src]="menuItem.src || menuItem.srcOut"
           (mouseover)="menuItem.src = menuItem.srcOn"
           (mouseout)="menuItem.src = menuItem.srcOut"/> {{menuItem.title}}
    </a>
  </li>

Finally I suggest you to use mouseover instead of mouseenter

like image 84
firegloves Avatar answered Apr 17 '23 19:04

firegloves