1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Angular Directive - Change color of button on hover

Discussão em 'Angular' iniciado por user3653474, Outubro 5, 2024.

  1. user3653474

    user3653474 Guest

    I have this button:

    <button class="btn-submit" type="button" appControlhover
    [defaultColor]="btnBackgroundColor"
    [highlightColor]="btnhoverColor"
    [btnhovertxtcolor]="btnColor"
    style="margin-top: 20px"
    (click)="Submit()">
    Send
    </button>


    ts code

    btnBackgroundColor='#28e979';
    btnhoverColor='#a6dabb';
    btnhovertxtcolor='#848f90';


    Custom Directive:

    import { Directive,
    OnInit,
    Renderer2,
    ElementRef,
    HostListener,
    Input,
    OnChanges,SimpleChanges,
    HostBinding } from '@angular/core';

    @Directive({
    selector: '[appControlhover]'
    })
    export class ControlhoverDirective implements OnChanges {

    @Input() defaultColor: string;
    @Input() highlightColor: string;
    @Input() isValid: boolean = true;
    @HostBinding('style.backgroundColor') backgroundColor: string;
    @HostBinding('style.color') btnhovercolor: string;

    constructor() { }
    ngOnInit() {

    if (this.highlightColor != 'none') {
    this.backgroundColor = this.defaultColor;
    } else if (this.highlightColor == 'none') {
    this.backgroundColor = this.defaultColor;
    } else {
    this.backgroundColor = this.defaultColor;
    }
    }

    @HostListener('mouseenter') mouseover(eventData: Event) {
    if (this.highlightColor != 'none') {
    this.backgroundColor = this.highlightColor;
    this.btncolor ='#28e979';
    } else if (this.highlightColor == 'none') {
    this.backgroundColor = this.defaultColor;
    } else {
    this.backgroundColor = this.defaultColor;
    }
    }

    @HostListener('mouseleave') mouseleave(eventData: Event) {

    if (this.highlightColor != 'none') {
    this.backgroundColor = this.defaultColor;
    } else if(this.highlightColor == 'none') {
    this.backgroundColor = this.defaultColor;
    } else {
    this.backgroundColor = this.defaultColor;
    }
    }
    }


    The button background color is changing but I also want to change the text color on the button on hover otherwise set with the default color.

    Any solution thanks.

    Continue reading...

Compartilhe esta Página