[英]mousemove event not return the correct mouse position
I try to put a tooltip that follow mouse position with the next code:我尝试在鼠标 position 之后放置一个工具提示,并使用下一个代码:
@HostListener('mousemove', ['$event']) onMouseMove(event) {
let tooltipNode = document.getElementsByClassName('dhemax-tooltip')[0] as HTMLElement;
tooltipNode.style.left = event.pageX + 'px'
tooltipNode.style.top = event.pageY + 'px';
}
public onMouseOver (event) {
let tooltipNode = document.getElementsByClassName('dhemax-tooltip')[0] as HTMLElement;
tooltipNode.style.display = 'block';
this.tooltipMsg = event.target.innerText;
}
public onMouseOut (event) {
let tooltipNode = document.getElementsByClassName('dhemax-tooltip')[0] as HTMLElement;
tooltipNode.style.display = 'none';
}
And this is the css for my tooltip:这是我的工具提示的 css:
.dhemax-tooltip {
z-index: 999;
display: none;
position: absolute;
background: #FFFFFF;
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
padding: 8px;
font-family: 'Nunito';
font-style: normal;
font-weight: 400;
font-size: 14px;
line-height: 100%;
letter-spacing: 0.01em;
color: #828282;
}
My tooltip is just a div:我的工具提示只是一个 div:
<div class="dhemax-tooltip"> {{ tooltipMsg }} </div>
This is the result:这是结果:
(I put a red circle in mouse position, the tooltip is too far from the current mouse position) (我在鼠标position上画了一个红圈,tooltip离当前鼠标位置太远了)
This is not an Angular issue, but a CSS one.这不是 Angular 问题,而是 CSS 问题。 The positioning of the element in question is set to absolute, which means it's positioned relative to a parent container with position:relative.
相关元素的定位设置为绝对,这意味着它相对于具有 position:relative 的父容器定位。
In your case, you want to position relative to the page/viewport, so you need to change it to position:fixed.在你的例子中,你想要 position 相对于页面/视口,所以你需要将它更改为 position:fixed。 See also this explanation about CSS Layout.
另请参阅有关 CSS 布局的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.