How to create an attribute directive that changes element behavior on hover events

0 votes
Can Someone help me regarding How to create an attribute directive that changes element behavior on hover events?
2 days ago in Java-Script by Nidhi
• 14,600 points
39 views

1 answer to this question.

0 votes

You can follow this example. This directive will change the background color of an element when hovered over:

import { Directive, ElementRef, HostListener, Renderer2 } from '@angular/core';

@Directive({

  selector: '[appHover]' // The attribute name to use in HTML

})

export class HoverDirective {

  constructor(

    private el: ElementRef,    // Reference to the host element

    private renderer: Renderer2 // Safely manipulate DOM

  ) {}

  // Listen for mouseenter event

  @HostListener('mouseenter') onMouseEnter() {

    this.renderer.setStyle(

      this.el.nativeElement,

      'backgroundColor',

      'lightblue'

    );

  }

  // Listen for mouseleave event

  @HostListener('mouseleave') onMouseLeave() {

    this.renderer.removeStyle(

      this.el.nativeElement,

      'backgroundColor'

    );

  }

}


To use this directive:

Add it to your module's declarations:

@NgModule({

  declarations: [HoverDirective],

  // ...

})

Apply it in your template:

<div appHover>Hover over me!</div>

answered 2 days ago by anonymous

Related Questions In Java-Script

0 votes
1 answer

How do I select an element with its name attribute in jQuery?

Hello @kartik, You can use: jQuery('[name="' + nameAttributeValue + ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,840 points
1,185 views
0 votes
1 answer

How to show a different value from an input that what will be received as php?

Hello @kartik, You can't change the field's value ...READ MORE

answered Jul 8, 2020 in Java-Script by Niroj
• 82,840 points
11,715 views
0 votes
2 answers

How can I set focus on an element in an HTML form using JavaScript?

Hi Kartik, try the following script <script>  (window.onload = ...READ MORE

answered Sep 24, 2020 in Java-Script by Okugbe
• 280 points
2,012 views
0 votes
1 answer

How to disable postback on an asp Button in System.Web.UI.WebControls.Button?

Hello @kartik, Use this: <asp:button runat="server".... OnClientClick="myfunction(); return false;" ...READ MORE

answered Sep 9, 2020 in Java-Script by Niroj
• 82,840 points
2,346 views
0 votes
0 answers

How do Observables improve API call handling in Angular?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
106 views
0 votes
0 answers

What’s the difference between Observables and Promises?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
76 views
0 votes
0 answers

What type of operation do RxJS operators allow for observables?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
85 views
0 votes
0 answers

When to use switchMap vs concatMap?

With the help of an example, can ...READ MORE

Mar 3 in Angular by Nidhi
• 14,600 points
111 views
0 votes
1 answer

How to create custom pagination in JavaScript?

It enhances user experience by dividing large ...READ MORE

answered Dec 24, 2024 in Java-Script by Navya
125 views
0 votes
1 answer

How to sum the values of a javascript object

Certainly, I'd be happy to help you ...READ MORE

answered Sep 25, 2023 in Java-Script by Edureka
• 12,690 points
5,133 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP