简体   繁体   English

如何在鼠标悬停时向菜单项添加和删除类

[英]How to add and remove class to menu items on mouseover

I want to add and remove a 'hover-open' class to the menu item when mouseover on that item. 当鼠标悬停在菜单项上时,我想向菜单项添加和删除“悬停打开”类。 The code I tried is adding the class to all the menu items when mouseovered on a single item. 我尝试的代码是将鼠标悬停在单个菜单项上时将该类添加到所有菜单项。

menu-bar.component.html 菜单bar.component.html

<ul>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 1</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 2</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 3</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 4</a></li>
</ul>

menu-bar.component.ts 菜单bar.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-menu-bar',
  templateUrl: './menu-bar.component.html'
})
export class MenuBarComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }

  hovered = false;
  changeStyle($event) {
    this.hovered = !this.hovered;
  }
}

use CSS :hover Pseudo-class, and it will work. 使用CSS :hover伪类,它将起作用。

css : css:

li: hover{
          write down style which you want to apply
}

You do this using *ngFor 您可以使用*ngFor

<ul>
  <li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">{{menu.name}}</a></li>
</ul>

menu-bar.component.ts 菜单bar.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-menu-bar',
  templateUrl: './menu-bar.component.html'
})
export class MenuBarComponent implements OnInit {
 menuItems: any = [];
 constructor() { 
  this.menuItems = [{name: 'link1', name: 'link2'}]
 }
  ngOnInit() {
  }

  changeStyle(menu) {
    menu.hovered = !menu.hovered;
 }
}

As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class. 由于您有多个使用同一功能的元素,因此需要一些标识来标识需要添加类的导航项。

Note this change: 注意此更改:

(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'"  (mouseout)="changeStyle('link1','out')" class="nav-item"

What I changed? 我改变了什么?

(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
 then add the class other wise remove

You can do something like this 你可以做这样的事情

<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 
'link1'"  (mouseout)="changeStyle('link1','out')" 
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')" 
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"  
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')"  class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')" 
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')" 
 class="nav-item"><a href="#">Link 4</a></li>
</ul>



export class AppComponent {     
  hovered = '';

  changeStyle(linkName, typeOfMove) {
    if (typeOfMove === 'out') {
      this.hovered = '';
    } else {
      this.hovered = linkName;
    }
  }
}

Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a 这是它的演示: https : //stackblitz.com/edit/angular-jvk15a

I think this code be done on css if you just used only for changing style. 我认为如果仅用于更改样式,则可以在CSS上完成此代码。

li { // default style for unhovered }
li:hover { // style when hover }

if you want access it by DOM. 如果要通过DOM访问它。 maybe you could try this one 也许你可以试试这个

changeStyle(event) {
  const klass = event.classList

  if (klass.contains("hover-open"))
    // remove class here
  else // add the class here
}

If hope this would help. 如果希望,这将有所帮助。

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

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