简体   繁体   English

我试图让 cursor 在按钮移动时移动

[英]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委托结合使用,您需要将鼠标的坐标通过XY传递到该委托:

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 像素传递给按钮(以防止按钮被点击),使用属性lefttop

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.

相关问题 当我在 html5 中按下按钮时,我正在尝试进行 rect 移动 - i am trying to make a rect move when i push a button in html5 我试图用javascript单击按钮时显示表单 - I am trying to make a form display when button is clicked with javascript 当我使用鼠标 hover 使用 CSS 时,我试图让按钮中的文本更改颜色 - I am trying to make the text in a button change color when I hover over in with the mouse using CSS 当我单击此按钮时,我试图访问以前的状态,但它似乎不起作用 - I am trying to access the previous state when i click this button but it does not seem to be working 当您使用Jquery按下按钮时,我试图使页面滚动速度变慢吗? - I am trying to make my page scroll slower when you press a button using Jquery? 我正在尝试制作一个在值为 true 时显示红色的按钮 - I am Trying to make a Button that shows red color when the value is true 我正在尝试制作一个清晰的按钮。 当用户单击清除按钮时,所有列表都应永久清除 - i am trying to make a clear button. when user click on the clear button then all list should be clear permanently 我正在尝试在 javascript 中制作一个简单的按钮来做两件事 - I am trying to make a simple button in javascript doing two stuff 我正在尝试在 javascript 中制作一个简单的切换按钮 - I am trying to make a simple toggle button in javascript 我正在尝试使用 React js 在电话簿应用程序上创建一个删除按钮 - I am trying to make a delete button on phonebook app with React js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM