移动鼠标光标 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4804068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:10:48  来源:igfitidea点击:

Move Mouse Cursor Javascript

javascripthtmlmousemouseevent

提问by Patrick McCreanor

I'm building a joke microsite for a company. They want a feature where the mouse cursor will randomly change position when the user hovers over a certain image.

我正在为一家公司建立一个笑话微型网站。他们想要一个功能,当用户将鼠标悬停在某个图像上时,鼠标光标会随机改变位置。

Is this possible in Javascript? How would I implement it?

这在 Javascript 中可能吗?我将如何实施它?

Thanks!

谢谢!

回答by Tom Tu

You can't... but you can - in a way.

你不能……但你可以——在某种程度上。

What you can do to mimic this behavior is that you can hide the actual cursor with css cursor: noneand then create an image representing the cursor which would look the same and would be placed in position of the real cursor. Then whe user would move the mouse you'd have to update the position of the cursor image and apply your random position changes as you wish - though be aware that when user will scroll outside of the browser content window (on the controls or outside the browser) they will get their mouse cursor back to real position and your cursor will be stuck in its last position.

您可以做些什么来模仿这种行为,您可以使用 css 隐藏实际的光标,cursor: none然后创建一个表示光标的图像,该图像看起来相同并放置在实际光标的位置。然后,当用户移动鼠标时,您必须更新光标图像的位置并根据需要应用随机位置更改 - 但请注意,当用户将滚动到浏览器内容窗口之外(在控件上或在浏览器)他们会将鼠标光标移回实际位置,而您的光标将停留在其最后一个位置。

The only way you could pull this prank out. Good luck with it and hope they'll enjoy it ;)

你能把这个恶作剧拉出来的唯一方法。祝你好运,希望他们会喜欢它;)

PS.

附注。

inverting the mouse movement direction can be funny as well :>

反转鼠标移动方向也很有趣:>

回答by Dan Grossman

You can't move the mouse cursor.

您无法移动鼠标光标。

You can move the page or the image relative to the position of the cursor, which might make it look like the cursor moved when it didn't?

您可以相对于光标的位置移动页面或图像,这可能会使光标看起来像没有移动时一样?

回答by Konstantin Sizikov

You can change your cursor with invisible custom icon, and then make a fake cursor and move it.

您可以使用不可见的自定义图标更改光标,然后制作一个假光标并移动它。