简体   繁体   English

我如何通过在angularjs2中使用mouseover选项将鼠标悬停在按钮上来闪烁图像?

[英]how can i flash image by hovering on button with the help of mouseover option in angularjs2?

What I want to do is when I hover on the 'click me' button then it should show an image on the web page and when i unhover it should not show any image with the help of mouseover option 我想要做的是,当我将鼠标悬停在“单击我”按钮上时,它应该在网页上显示一个图像,而当我将其悬停时,它不应在mouseover选项的帮助下显示任何图像

here is what i tried to do in app.component.ts and my.component.ts files 这是我尝试在app.component.ts和my.component.ts文件中执行的操作

here is the code for app.component.ts : 这是app.component.ts的代码:

import { Component } from '@angular/core';     //importing components from angular
import { MyComponent } from './my.component';   //importing components from my.component  



@Component({
     selector: 'my-app',
     template: `<h1> Hi Buddy!! </h1>
          <mytag></mytag>`,
     directives: [MyComponent]         //adding directives from mycomponents
})
export class AppComponent { }

and here is the code for my.component.ts: 这是my.component.ts的代码:

import { Component } from "@angular/core";

@Component({
        selector:'mytag',
        template: `<button (mouseover)="<img [src]="image"> "  >click me</button>`   // here i tried to flash image by hovering
})
export class MyComponent{
      public image="http://lorempixel.com/400/200";
      myclick(klm){
           console.log(klm);

     }
}

so what changes should i make in the class or meta data of my.component.ts in order to do so 所以我应该对my.component.ts的类或元数据进行哪些更改

You can use Angular Animations module to achieve the same. 您可以使用Angular Animations模块来实现相同的功能。

Make the below changes to your MyComponent: 对MyComponent进行以下更改:

import { Component } from '@angular/core'
import { trigger, state, style, transition, animate, keyframes, group } from '@angular/animations';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@Component({
      selector:'mytag',
      template: `<button (mouseover)="toggleOnOff()">click me</button>
                  <img [src]="image" [@switchImageDisplay]="showImage"/>
                  `
      ,
      animations: [
        trigger("switchImageDisplay",[
          state("show", style({
            display : 'block'
          })),
          state("hide", style({
            display : 'none'
          })),
          transition('show <-> hide',[animate('0s')]),
        ])

      ]
    })
export class SwitchDisplayComponent {
      public image="http://lorempixel.com/400/200";
      public showImage : string;
          toggleOnOff(){
            console.log("Previous display value is",this.showImage);
               this.showImage = (this.showImage === "show") ? "hide" : "show";
              console.log("Current display value is",this.showImage);
         }

}

Explanation: toggleOnOff() function sets a string variable showImage as show and hide. 说明:toggleOnOff()函数将字符串变量showImage设置为show和hide。 In Animations we create a trigger and give it a name. 在“动画”中,我们创建一个触发器并为其命名。 In our case we have named it as "switchImageDisplay". 在我们的例子中,我们将其命名为“ switchImageDisplay”。 We declared two states in the animation trigger that is "show" and "hide". 我们在动画触发器中声明了两个状态:“显示”和“隐藏”。 In those states we defined what CSS to be used. 在这些状态下,我们定义了要使用的CSS。 Finally we defined a transition, which is 2 ways binded and is performed in 0 seconds. 最后,我们定义了一个过渡,该过渡是2种方式绑定的,并且在0秒内执行。 If you want the image to be hidden over a period of time increase the time. 如果要在一段时间后隐藏图像,请增加时间。

In template code, we have binded the img tag to the animation using the code [@switchImageDisplay]="showImage". 在模板代码中,我们已使用代码[@switchImageDisplay] =“ showImage”将img标签绑定到动画。 Based on the "showImage" value, the animation "switchImageDisplay"'s state is defined. 基于“ showImage”值,定义动画“ switchImageDisplay”的状态。

Import the import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 从“ @ angular / platform-b​​rowser / animations”导入导入{BrowserAnimationsModule}; in your app.module.ts and in the imports array as well. 在app.module.ts和imports数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM