[英]I am trying to make a cursor move when a button does
I am trying to make a button that moves when the cursor does so it is just the cursor can't click it but it is attached to it but cannot be clicked.我正在尝试制作一个按钮,当 cursor 这样做时它会移动,所以它只是 cursor 无法单击它,但它已连接到它但无法单击。 I am using this but it is not working (btw I am new to javascript so idk if it was supposed to work or not): document.addEventListener('mousemove',runAway,true)
我正在使用它,但它不工作(顺便说一句,我是 javascript 的新手,所以如果它应该工作还是不工作): document.addEventListener('mousemove',runAway,true)
Use event mousemove
in conjunction with the event event
delegation, to which you need to transfer the coordinates of the mouse by X and Y :将事件mousemove
与事件event
委托结合使用,您需要将鼠标的坐标通过X和Y传递到该委托:
x = event.pageX;
y = event.pageY;
Next, we pass the current coordinates +10 pixels to the button (to prevent the button from being clicked), using property left
and top
:接下来,我们将当前坐标+10 像素传递给按钮(以防止按钮被点击),使用属性left
和top
:
button.style.left = x + 10 + "px";
button.style.top = y + 10 + "px";
Likewise, the moving element must be relative or absolute .同样,移动元素必须是相对的或绝对的。
let button = document.querySelector("button"); document.addEventListener("mousemove", function (event) { x = event.pageX; y = event.pageY; button.style.left = x + 10 + "px"; button.style.top = y + 10 + "px"; });
button { position: absolute; }
<button>Moving button</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.