【问题标题】:How to show the angular material drop down on mouse over?如何在鼠标悬停时显示角度材料下拉?
【发布时间】:2019-06-15 12:02:30
【问题描述】:

现在点击汉堡菜单我得到下拉列表而不是鼠标悬停在汉堡菜单上时需要它这里是堆栈闪电战link

【问题讨论】:

    标签: angular angular-material angular7


    【解决方案1】:

    您可以使用matMenuTrigger 指令来做到这一点

    <button mat-icon-button [matMenuTriggerFor]="menu" 
        #menuTrigger="matMenuTrigger" (mouseenter)="menuTrigger.openMenu()">
    

    要隐藏菜单,请为菜单添加mouseleave 事件。

    将所有菜单项捆绑在 spandiv 标记中。然后将(mouseleave)事件附加到它

    <mat-menu #menu="matMenu" [overlapTrigger]="false">
        <span (mouseleave)="menuTrigger.closeMenu()">
          <button mat-menu-item>
            <mat-icon>home</mat-icon>
            <span>Home</span>
            ........
           <mat-icon>exit_to_app</mat-icon>
           <span>Logout</span>
         </button>
        </span>
    </mat-menu>
    

    分叉DEMO

    【讨论】:

    • 鼠标移开下拉应该去
    • @PrashanthGH 为(mouseleave) 添加另一个事件绑定
    • @Amit chigadani 我添加了 (mouseleave) 事件,但它无法正常工作这里是堆栈闪电战 stackblitz.com/edit/…
    • @PrashanthGH 嘿,请检查更新的答案和演示
    • 这应该是 StackOverflow 中的第一个链接
    【解决方案2】:

    我知道我参加聚会很晚了,但以上都没有真正对我有用。我最终写了一个指令,所以为我解决这个问题。

    HoverDropDownDirective

    import { NgModule } from '@angular/core';
    import { Directive, Input, ElementRef, OnInit } from '@angular/core';
    import { MatMenuTrigger, _MatMenu } from '@angular/material';
    
    @Directive({
        selector: '[hoverDropDown]'
    })
    export class HoverDropDownDirective implements OnInit {
        isInHoverBlock = false;
    
        constructor(private el: ElementRef) {}
    
        @Input() hoverTrigger: MatMenuTrigger;
        @Input() menu: any;
    
        ngOnInit() {
            this.el.nativeElement.addEventListener('mouseenter', () => {
                this.setHoverState(true);
                this.hoverTrigger.openMenu();
    
                const openMenu = document.querySelector(`.mat-menu-after.${this.menu._elementRef.nativeElement.className}`);
                if (!openMenu) {
                    this.hoverTrigger.closeMenu();
                    return;
                }
                openMenu.addEventListener('mouseenter', () => {
                    this.setHoverState(true);
                });
                openMenu.addEventListener('mouseleave', () => {
                    this.setHoverState(false);
                });
            });
            this.el.nativeElement.addEventListener('mouseleave', () => {
                this.setHoverState(false);
            });
        }
    
        private setHoverState(isInBlock: boolean) {
            this.isInHoverBlock = isInBlock;
            if (!isInBlock) {
                this.checkHover();
            }
        }
    
        private checkHover() {
            setTimeout(() => {
                if (!this.isInHoverBlock && this.hoverTrigger.menuOpen) {
                    this.hoverTrigger.closeMenu();
                }
            }, 50);
        }
    }
    
    @NgModule({
        declarations: [
            HoverDropDownDirective
        ],
        exports: [
            HoverDropDownDirective
        ]
    })
    export class HoverDropDownDirectiveModule {}
    

    app.module

    import { HoverDropDownDirectiveModule } from '../../directives/hover-drop-down.directive';
    
    imports: [
        HoverDropDownDirectiveModule
    ]
    

    HTML

    <div *ngFor="let category of categories">
        <button #menuTrigger="matMenuTrigger"
                mat-button
                [matMenuTriggerFor]="children"
                (click)="navigateMain(category.Category)"
    
                hoverDropDown
                [menu]="children"
                [hoverTrigger]="menuTrigger">
                 {{category.Category.Description}}
        </button>
        <mat-menu #children="matMenu" hasBackdrop="false">
            <button mat-menu-item *ngFor="let sub of category.SubCategories" (click)="navigateSubCategory(sub)">{{sub.Description}}</button>
        </mat-menu>
    </div>
    

    需要注意的两件事:

    1. 主按钮中的 3 个属性(“hoverDropDown”、“[menu]”和“[hoverTrigger]”)
    2. hasBackdrop="false" mat-menu 中的属性

    hasBackdrop="false" 属性记录在 Angular Material 上。希望这对你有用...

    【讨论】:

    • 适用于多个菜单。
    • 我要试试这个小子菜单
    • 对我来说比接受的答案更好。谢谢
    【解决方案3】:

    为 mat 按钮添加一个引用变量,用于在 mouseover 事件上触发 click

    <button mat-icon-button
            #matBtn
            (mouseover)="matBtn._elementRef.nativeElement.click()"
            [matMenuTriggerFor]="menu"
    >
    

    注意:我真的不喜欢像这样访问对象的“私有”属性,我的这个解决方案更像是一种解决方法,如果你没有找到其他解决方案,请使用它。

    【讨论】:

      【解决方案4】:

      MatMenuTrigger

      该指令旨在与 mat-menu 一起使用 标签。它负责切换提供的菜单的显示 实例。

      <button #r="matMenuTrigger" mat-icon-button [matMenuTriggerFor]="menu" >
            <mat-icon (mouseover)="open(r)"  >menu</mat-icon>
      </button>
      

      例如:https://stackblitz.com/edit/example-angular-material-toolbar-menu-wrut3v

      【讨论】:

        【解决方案5】:

        覆盖mouseover 事件并创建#menuTrigger 引用变量。这将解决您的问题。

          <button mat-icon-button [matMenuTriggerFor]="menu" #menuTrigger="matMenuTrigger" 
          (mouseover)="menuTrigger.openMenu()" >
        

        我已经更新了你的stackblitz

        【讨论】:

        • 如果有多个菜单,这是行不通的。不管怎样,我投票了。
        【解决方案6】:

        app.component.html:

        <mat-toolbar color="primary" >
          <span class="fill-remaining-space">
            <button #clickMe mat-icon-button [matMenuTriggerFor]="menu" (mouseenter)="clickOnHover()">
              <mat-icon>menu</mat-icon>
            </button>
            <mat-menu #menu="matMenu" [overlapTrigger]="false">
              <button mat-menu-item>
                <mat-icon>home</mat-icon>
                <span>Home</span>
              </button>
              <button mat-menu-item>
                <mat-icon>people_outline</mat-icon>
                <span>Connecting</span>
              </button>
              <button mat-menu-item>
                <mat-icon>videocam</mat-icon>
                <span>Let's talk</span>
              </button>
              <button mat-menu-item>
                <mat-icon>exit_to_app</mat-icon>
                <span>Logout</span>
              </button>
            </mat-menu>
          </span>
          <span class="fill-remaining-space">Application Title</span>
        </mat-toolbar>
        

        app.component.ts:

        import { Component, ViewChild } from '@angular/core';
        
        
        @Component({
          selector: 'my-app',
          templateUrl: './app.component.html',
          styleUrls: [ './app.component.css' ]
        })
        export class AppComponent  {
          @ViewChild('clickMe') clickMe: any;
        
          clickOnHover() {
            this.clickMe._elementRef.nativeElement.click();
          }
        
        }
        

        【讨论】:

          【解决方案7】:

          希望对你有帮助

          使用(鼠标输入)功能 Angular-hover-stackblitz

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-03-14
            • 1970-01-01
            • 1970-01-01
            • 2016-01-28
            • 1970-01-01
            • 2017-12-25
            相关资源
            最近更新 更多