javascript - Mouseenter 事件正在跳过元素
问题描述
mouseenter
当在元素上触发事件时,我有这个指令来做一些事情。但是当我快速拖动鼠标时,一些元素会在不触发mouseenter
事件的情况下被跳过。
我实际上想在鼠标移动时突出显示网格的一系列单元格。我已将此指令添加到网格单元的模板中。
@Directive({
// tslint:disable-next-line:directive-selector
selector: '[appRangeSelector]'
})
export class RangeSelectorDirective {
@Input() public selectorParams: any;
public isSelected = false;
constructor(private elRef: ElementRef,
private renderer: Renderer2) { }
@HostListener('mouseenter', ['$event']) public onMouseEnter(e) {
if (e.buttons === 1 || e.buttons === 3) {
if (!this.isSelected) {
console.log('selected');
this.renderer.setStyle(this.elRef.nativeElement, 'background', 'blue');
this.isSelected = true;
} else {
console.log('deselected');
this.renderer.setStyle(this.elRef.nativeElement, 'background', 'unset');
this.isSelected = false;
}
}
}
}
当用户以任何速度选择单元格范围时,我需要选择范围。对此的任何帮助表示赞赏。
解决方案
操作系统只在一定的时间间隔更新鼠标位置,不保证连续移动。
如果你想防弹,你可能需要监听 mousemove 事件,计算轨迹,并检查它是否与你的任何元素相交。但是,我担心这可能有点重,所以你最好先对其进行基准测试。
推荐阅读
- python - 如何找到我试图在 django 的模板文件夹中使用的静态 img 的绝对路径?
- reactjs - 警告:收到非布尔属性“jsx”的“真”。时代风格的 Jsx
- java - 即使在移动到元素并在 Selenium Java 中显式等待之后,元素也无法交互
- python - 如何在 alpine linux docker 中使用 py-requests?
- vue.js - Nuxtjs - 身份验证
- javascript - 从字符串中删除特殊字符并将每个 str 转换为数组项
- hdfs - 什么是 HDFS NFS “访问时间”?
- azure-functions - Azure Functions - IoTHubTrigger 但它不会触发
- firebase-realtime-database - 在 firebase 控制台的实时数据库中查看
- c# - 无法在 EF Core 2.2 中配置延迟加载以切断未加载的部分