在新的Angular2框架中,有没有人知道如何像事件一样执行鼠标悬停效果?
在Angular1中,有ng-Mouseover
,但这似乎没有被继承。
我查阅了文档,但没有找到相关内容。
在新的Angular2框架中,有没有人知道如何像事件一样执行鼠标悬停效果?
在Angular1中,有ng-Mouseover
,但这似乎没有被继承。
我查阅了文档,但没有找到相关内容。
<div (mouseenter) ="mouseEnter('div a') " (mouseleave) ="mouseLeave('div A')">
<h2>Div A</h2>
</div>
<div (mouseenter) ="mouseEnter('div b')" (mouseleave) ="mouseLeave('div B')">
<h2>Div B</h2>
</div>
组件
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'basic-detail',
templateUrl: 'basic.component.html',
})
export class BasicComponent{
mouseEnter(div : string){
console.log("mouse enter : " + div);
}
mouseLeave(div : string){
console.log('mouse leave :' + div);
}
}
为了完全实现 Angular 2 中的可悬停事件,您应该同时使用 mouseenter
和 mouseleave
事件。
在 Angular 2 中,ng-Mouseover
被替换为 on-mouseover
,因此您需要编写以下内容:
是的,在 Angular 2 中有 on-mouseover
,而不是像在 Angular 1.x 中使用的 ng-Mouseover
。因此,您需要编写以下内容:
<div on-mouseover='over()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
over(){
console.log("Mouseover called");
}
正如@Gunter在评论中建议的,有一个on-mouseover
的替代方法,我们也可以使用这个。一些人更喜欢使用on-前缀的替代方法,即规范形式。
HTML代码 -
<div (mouseover)='over()' (mouseout)='out()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
控制器/.TS 代码 -
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
over(){
console.log("Mouseover called");
}
out(){
console.log("Mouseout called");
}
}
在Angular中还可以使用其他鼠标事件 -
(mouseenter)="myMethod()"
(mousedown)="myMethod()"
(mouseup)="myMethod()"
<div (mouseover)='over()'
呢?;-) 说明:该句话是一个问题,其中包含一个HTML标记,并在其中使用了AngularJS框架的事件绑定方式。最后带有一个笑脸表情符号,表示语气轻松幽默。 - Günter Zöchbauer你可以通过主机来完成:
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[myHighlight]',
host: {
'(mouseenter)': 'onMouseEnter()',
'(mouseleave)': 'onMouseLeave()'
}
})
export class HighlightDirective {
private _defaultColor = 'blue';
private el: HTMLElement;
constructor(el: ElementRef) { this.el = el.nativeElement; }
@Input('myHighlight') highlightColor: string;
onMouseEnter() { this.highlight(this.highlightColor || this._defaultColor); }
onMouseLeave() { this.highlight(null); }
private highlight(color:string) {
this.el.style.backgroundColor = color;
}
}
只需将其适应于您的代码(位于:https://angular.io/docs/ts/latest/guide/attribute-directives.html)
如果您对鼠标进入或离开组件的行为感兴趣,可以使用@HostListener
装饰器:
import { Component, HostListener, OnInit } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
styleUrls: ['./my-component.scss']
})
export class MyComponent implements OnInit {
@HostListener('mouseenter')
onMouseEnter() {
this.highlight('yellow');
}
@HostListener('mouseleave')
onMouseLeave() {
this.highlight(null);
}
...
}
如 @Brandon 在评论中提供的链接所解释的那样,参见原始问题 (https://angular.io/docs/ts/latest/guide/attribute-directives.html)
只需在Angular2+中使用(mouseenter)
属性...
在您的HTML中添加:
<div (mouseenter)="mouseHover($event)">Hover!</div>
并且在你的组件中执行:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'component',
templateUrl: './component.html',
styleUrls: ['./component.scss']
})
export class MyComponent implements OnInit {
mouseHover(e) {
console.log('hovered', e);
}
}
如果您希望通过鼠标悬停来操作所有组件,您可以直接使用@hostListener
处理事件,执行以下所有的鼠标悬停操作。
import {HostListener} from '@angular/core';
@HostListener('mouseenter') onMouseEnter() {
this.hover = true;
this.elementRef.nativeElement.addClass = 'edit';
}
@HostListener('mouseleave') onMouseLeave() {
this.hover = false;
this.elementRef.nativeElement.addClass = 'un-edit';
}
这个功能可以在@angular/core
中找到。我已经在angular的4.x.x
版本中进行了测试。
如果要处理鼠标悬停事件,可以尝试以下代码(经本人测试可行):
在 HTML 模板中:
<div (mouseenter)="onHovering($event)" (mouseleave)="onUnovering($event)">
<img src="../../../contents/ctm-icons/alarm.svg" class="centering-me" alt="Alerts" />
</div>
onHovering(eventObject) {
console.log("AlertsBtnComponent.onHovering:");
var regExp = new RegExp(".svg" + "$");
var srcObj = eventObject.target.offsetParent.children["0"];
if (srcObj.tagName == "IMG") {
srcObj.setAttribute("src", srcObj.getAttribute("src").replace(regExp, "_h.svg"));
}
}
onUnovering(eventObject) {
console.log("AlertsBtnComponent.onUnovering:");
var regExp = new RegExp("_h.svg" + "$");
var srcObj = eventObject.target.offsetParent.children["0"];
if (srcObj.tagName == "IMG") {
srcObj.setAttribute("src", srcObj.getAttribute("src").replace(regExp, ".svg"));
}
}
@Component({
selector: 'drag-drop',
template: `
<h1>Drag 'n Drop</h1>
<div #container
class="container"
(mousemove)="onMouseMove( container)">
<div #draggable
class="draggable"
(mousedown)="onMouseButton( container)"
(mouseup)="onMouseButton( container)">
</div>
</div>`,
})
@Output() elemHovered: EventEmitter<any> = new EventEmitter<any>();
onHoverEnter(): void {
this.elemHovered.emit([`The button was entered!`,this.event]);
}
onHoverLeave(): void {
this.elemHovered.emit([`The button was left!`,this.event])
}
(mouseenter) = "onHoverEnter()" (mouseleave)="onHoverLeave()"
在您的js/ts文件中,将接收悬停信息
elemHoveredCatch(d): void {
console.log(d)
}
(elemHovered) = "elemHoveredCatch($event)"
如果你只是想要一个悬停效果,请使用Hover.css
。
npm i hover.css
angular.json
的属性projects.architect.build.options.styles
中,将此行添加到数组中:node_modules/hover.css/scss/hover.scss
在你想要应用效果的元素上使用它们的任何类,例如:
<div *ngFor="let source of sources">
<div class="row justify-content-center">
<div class="col-12 hvr-glow">
<!-- My content -->
</div>
</div>
</div>
mousemove
事件在这里也可以有所帮助。请参考此页面的示例。 - Abhi