如何在悬停时向元素添加类?

时间:2017-03-06 18:38:26

标签: angular angular2-template

如何在div上悬停时将div添加到div。

模板 -

<div class="red">On hover add class ".yellow"</div>

组件 -

import {Component} from 'angular2/core';

@Component({
  selector: 'hello-world',
  templateUrl: 'src/hello_world.html',
  styles: [`
    .red {
      background: red;
    }

    .yellow {
      background: yellow;
    }

  `]
})
export class HelloWorld {
}

Demo

[注意 - 我特别想添加一个新类,而不是修改现有的类]

感叹!这是一个正常的用例,我还没有看到任何直接的解决方案!

7 个答案:

答案 0 :(得分:39)

您也可以使用以下内容:

[ngClass]="color" (mouseover)="changeStyle($event)" (mouseout)="changeStyle($event)"

然后在组件中

color:string = 'red';

changeStyle($event){
  this.color = $event.type == 'mouseover' ? 'yellow' : 'red';
}

Plunker

或者,在标记中执行所有操作:

[ngClass]="color" (mouseover)="color='yellow'" (mouseout)="color='red'"

答案 1 :(得分:17)

简单如下

<button [class.btn-success]="mouseOvered" 
  (mouseover)="mouseOvered=true"
  (mouseout)="mouseOvered=false"> Hover me </button>

<强> LIVE DEMO

答案 2 :(得分:1)

如果您以编程方式设置样式(例如,从组件中的属性设置)并希望在悬停时更改样式,则可以查看this Plunker demo

当多个元素必须响应鼠标悬停事件时,它也会回答这个问题。

以下是代码:

@Component({
    selector: 'app',
    template: `
    <div id="1" 
      (mouseover)="showDivWithHoverStyles(1)" 
      (mouseout)="showAllDivsWithDefaultStyles()" 
      [ngStyle]="hoveredDivId ===1 ? hoveredDivStyles : defaultDivStyles">
      The first div
    </div>

    <div id="2" 
      (mouseover)="showDivWithHoverStyles(2)" 
      (mouseout)="showAllDivsWithDefaultStyles()" 
      [ngStyle]="hoveredDivId === 2 ? hoveredDivStyles :  defaultDivStyles">
      The second div
    </div>`
})
class App{
  hoveredDivId;
  defaultDivStyles= {height: '20px', 'background-color': 'white'};
  hoveredDivStyles= {height: '50px', 'background-color': 'lightblue'};

  showDivWithHoverStyles(divId: number) {
    this.hoveredDivId = divId;
  }

  showAllDivsWithDefaultStyles() {
    this.hoveredDivId = null;
  }
}

答案 3 :(得分:0)

如果您申请整个组件,

@HostListener装饰器也是一个不错的选择。

保持html不变,并在组件中添加@HostListener

  <div class="red">On hover add class ".yellow"</div> 

  @HostListener('mouseenter') onMouseEnter() {
    this.elementRef.nativeElement.class = 'red';
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.elementRef.nativeElement.class = 'yellow';
  }

答案 4 :(得分:0)

不要弄脏我刚刚编写的简单hover-class指令的代码。

<span hover-class="btn-primary" class="btn" >Test Me</span>

Running Sample

Code Editor stackblitz

在指令下方,

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

@Directive({
  selector: '[hover-class]'
})
export class HoverClassDirective {

  constructor(public elementRef:ElementRef) { }
  @Input('hover-class') hoverClass:any;  

  @HostListener('mouseenter') onMouseEnter() {
    this.elementRef.nativeElement.classList.add(this.hoverClass);
 }

  @HostListener('mouseleave') onMouseLeave() {
    this.elementRef.nativeElement.classList.remove(this.hoverClass);
  }

}

答案 5 :(得分:0)

<li *ngFor="let q of questions;let i = index" class="list-group-item"  (mouseenter)="isHover = i" [ngClass]="{'active' : isHover === i}">
            <h5>Question 1</h5>
            <p>what is the largest man made structure on the earth ?</p>
        </li>

isHover和问题是在.ts文件中声明的属性。

答案 6 :(得分:0)

我采用了 Davut 的示例并将其更改为处理多个类...

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

@Directive( {
    selector: '[hover-class]'
} )
export class HoverClassDirective {

    constructor( public elementRef: ElementRef ) {
    }

    @Input( 'hover-class' ) hoverClass: any;

    @HostListener( 'mouseenter' ) onMouseEnter() {
        this.update( 'add' );
    }

    @HostListener( 'mouseleave' ) onMouseLeave() {
        this.update( 'remove' );
    }

    protected update( action: string ): void {
        this.hoverClass.split( ' ' ).forEach( item => this.elementRef.nativeElement.classList[action]( item ) );
    }
}