将鼠标悬停在内容上时,在工具提示中显示图像会给我一种闪烁效果

Displaying image in tooltip is giving me a flickering effect when hovered over the content

我正在使用 Angular here.I 我试图在将鼠标悬停在特定内容上时在工具提示中显示 image/video。但是,当我将鼠标悬停在内容上时,图像在渲染之前会产生闪烁效果。过渡不如我用文字代替图像时那么流畅。我如何解决它?

我正在尝试将输入的图像 src 设置为工具提示组件。我有一个生成工具提示内容的指令。这里的代码是:

app.component.html

<button awesomeTooltip="Hello World!"  image="https://imgplaceholder.com/420x320/ff7f7f/333333/fa-image">Hi there, I have a tooltip</button>

tooltip.component.ts

import { Component, Input } from '@angular/core';
import { animate, style, transition, trigger } from '@angular/animations';

@Component({
  selector: 'awesome-tooltip',
  styleUrls: ['./tooltip.component.css'],
  templateUrl: './tooltip.component.html',
  animations: [
    trigger('tooltip', [
      transition(':enter', [
        style({ opacity: 0 }),
        animate(300, style({ opacity: 1 })),
      ]),
      transition(':leave', [
        animate(300, style({ opacity: 0 })),
      ]),
    ]),
  ],
})
export class AwesomeTooltipComponent {

  @Input() image=''
  @Input() text = '';
}

tooltip.component.html

<div @tooltip>
   <img [src]="image" width="20px" height="30px">
   <!-- Hello -->
</div>

tooltip.directive.ts

import { ComponentRef, Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
import { Overlay, OverlayPositionBuilder, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';

import { AwesomeTooltipComponent } from './tooltip.component';

@Directive({ selector: '[awesomeTooltip]' })
export class AwesomeTooltipDirective implements OnInit {

  @Input('awesomeTooltip') text = '';
  @Input('image') image = '';
  private overlayRef: OverlayRef;

  constructor(private overlay: Overlay,
              private overlayPositionBuilder: OverlayPositionBuilder,
              private elementRef: ElementRef) {
  }

  ngOnInit(): void {
    const positionStrategy = this.overlayPositionBuilder
      .flexibleConnectedTo(this.elementRef)
      .withPositions([{
        originX: 'center',
        originY: 'top',
        overlayX: 'center',
        overlayY: 'bottom',
        offsetY: -8,
      }]);

    this.overlayRef = this.overlay.create({ positionStrategy });
  }

  @HostListener('mouseenter')
  show() {
    const tooltipRef: ComponentRef<AwesomeTooltipComponent>
      = this.overlayRef.attach(new ComponentPortal(AwesomeTooltipComponent));
    tooltipRef.instance.text = this.text;
    tooltipRef.instance.image = this.image;
  }

  @HostListener('mouseout')
  hide() {
    this.overlayRef.detach();
  }
}

stackblitz link 是 here

我要找的结果是:

叠加层的设置方式如下:

  • 当您将鼠标悬停在按钮上时,叠加层变为可见
  • 当您移动到按钮以外的任何地方时,叠加层会隐藏

您遇到的闪烁是由于无法解决的情况

  1. 当您将鼠标悬停在按钮的底部时,叠加层变为可见
  2. 但由于叠加层从按钮的中间开始,从技术上讲,您现在悬停在叠加层上(不再悬停在按钮上),因此叠加层会隐藏
  3. 叠加层一隐藏,您就回到上面的点#1...

这种情况一直持续下去,这就是您遇到闪烁效果的原因。

解决方案,将以下代码添加到您的 tooltip.component.css,将叠加层放在按钮下方,这样递归闪烁就不会发生:

::ng-deep .cdk-overlay-connected-position-bounding-box{top:60px !important; position:absolute;}

更新:对于更大的按钮,可以更改顶部值以确保覆盖从按钮下方开始并且没有重叠。 demo here

给大家搜索上面例子的相对定位:

移除

::ng-deep .cdk-overlay-connected-position-bounding-box{top:60px !important; position:absolute;}

!添加

@import '~@angular/cdk/overlay-prebuilt.css';

进入你的styles.css。我花了很长时间才找到答案。

来源:https://medium.com/angular-in-depth/building-tooltips-for-angular-3cdaac16d138

https://stackblitz.com/edit/building-tooltip

它们完全一样,除了这个最小的变化:)