我试图在按钮移动时移动光标
I am trying to make a cursor move when a button does
我正在尝试制作一个按钮,它会在光标移动时移动,所以它只是光标无法单击它但它已附加到它但无法单击。我正在使用它,但它不起作用(顺便说一句,我是 javascript 的新手,所以不知道它是否应该起作用):document.addEventListener('mousemove',runAway,true)
结合事件event
委托使用事件mousemove
,需要通过X和[=传递鼠标坐标31=]Y:
x = event.pageX;
y = event.pageY;
接下来,我们将当前坐标+10像素传递给按钮(防止按钮被点击),使用属性left
和top
:
button.style.left = x + 10 + "px";
button.style.top = y + 10 + "px";
同样,移动元素必须是relative或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>
我正在尝试制作一个按钮,它会在光标移动时移动,所以它只是光标无法单击它但它已附加到它但无法单击。我正在使用它,但它不起作用(顺便说一句,我是 javascript 的新手,所以不知道它是否应该起作用):document.addEventListener('mousemove',runAway,true)
结合事件event
委托使用事件mousemove
,需要通过X和[=传递鼠标坐标31=]Y:
x = event.pageX;
y = event.pageY;
接下来,我们将当前坐标+10像素传递给按钮(防止按钮被点击),使用属性left
和top
:
button.style.left = x + 10 + "px";
button.style.top = y + 10 + "px";
同样,移动元素必须是relative或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>