简体   繁体   English

将div定位到光标位置时的Javascript偏移量

[英]Javascript offset on positioning div to cursor position

I want to position a div to mouse position. 我想将div定位到鼠标位置。

This is my pen: http://codepen.io/Linuxer4Fun/pen/QdJzWv 这是我的笔: http : //codepen.io/Linuxer4Fun/pen/QdJzWv

It does what it has to, but when I go to my browser, the div has an unnerving offset 它做了它必须要做的,但是当我进入浏览器时,div的偏移量令人不安

I think, the problem is in here: I already tried pageX and clientX 我认为问题出在这里:我已经尝试过pageX和clientX

document.onmousemove = function (e) {
    cursor_x = e.clientX;
    cursor_y = e.clientY;
}

This is how it looks on my pc: HERE 这是我的电脑上的样子: 这里

Is it because the box stays where the cursor first entered the element? 是因为框停留在光标第一次进入元素的位置? Try using the mousemove event to update the position of the box as the mouse moves. 尝试使用mousemove事件在鼠标移动时更新框的位置。

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

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