JavaScript 如何将鼠标光标更改为图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10866471/
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-24 03:20:01  来源:igfitidea点击:

JavaScript how to change mouse cursor to an image?

javascriptjqueryhtmlimagemouse

提问by JDS

I've seen examples where you can change it to preset images that your operating system has; example:

我见过一些示例,您可以将其更改为操作系统具有的预设图像;例子:

$('body').css('cursor', 'wait'); 

But how about my own /img/my_image.png?

但是我自己的 /img/my_image.png 怎么样?

Thanks for any help.

谢谢你的帮助。

回答by malangi

You can, by specifying a url to it in CSS:

您可以通过在 CSS 中为其指定一个 url:

div
{
cursor:url(smiley.gif),url(myBall.cur),auto;
}

See http://www.w3schools.com/cssref/pr_class_cursor.asp

http://www.w3schools.com/cssref/pr_class_cursor.asp

回答by Kalle

The Jquery way to set a custom cursor (or default cursor as a fallback):

设置自定义游标(或默认游标作为后备)的 Jquery 方式:

$('selector').css({'cursor': 'url(/cursors/customMoveCursor.cur), default'});

回答by C.S.

I don't usually link external articles, but this one covers your needs. I've copied the relevant stuff here for persistence.

我通常不链接外部文章,但这一篇涵盖了您的需求。为了持久性,我在这里复制了相关的内容。

#dragMe {
    cursor: url('../cursors/customMoveCursor.cur'),     /* Modern browsers    */
            url('cursors/customMoveCursor.cur'),        /* Internet Explorer  */
            move;                                       /* Older browsers     */
}

Credit: http://www.useragentman.com/blog/2011/12/21/cross-browser-css-cursor-images-in-depth/

信用:http: //www.useragentman.com/blog/2011/12/21/cross-browser-css-cursor-images-in-depth/

Don't forget to consider accessibility when making your custom cursors! Cheers :)

在制作自定义光标时不要忘记考虑可访问性!干杯:)