简体   繁体   中英

Javascript offset on positioning div to cursor position

I want to position a div to mouse position.

This is my pen: 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

I think, the problem is in here: I already tried pageX and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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