最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript-firefox: how to set custom cursor from local png file? - Stack Overflow

programmeradmin9浏览0评论

how do you set a custom cursor for the current page from a local image file? basically I need to change to a different cursor programmatically using javascript, but the cursor is stored locally on my hard drive.

how do you set a custom cursor for the current page from a local image file? basically I need to change to a different cursor programmatically using javascript, but the cursor is stored locally on my hard drive.

Share Improve this question asked Feb 23, 2012 at 7:27 KJWKJW 15.3k51 gold badges141 silver badges247 bronze badges 2
  • Duplicate of stackoverflow./questions/4564251/… – synthesizerpatel Commented Feb 23, 2012 at 7:33
  • yes but how to indicate local file url? im guessing this is not possible? – KJW Commented Feb 23, 2012 at 8:50
Add a ment  | 

1 Answer 1

Reset to default 7

You can't (but see below), because the file:// cannot be accessed or referred from other protocols.

The basic way to include a cursor through JavaScript is:

element.style.cursor = "url(...location-of-pic...), auto";
//                                                ^^^^^^ (required as fallback)
  1. You can convert your image to a base64-dataURI, and use it instead of file://..../cursor.png:

    document.documentElement.style.cursor = 'url("data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCwAAAAAAAAAAAAAAAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAAAAAAGxsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/AAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABsbG3/bGxt/wAAAAAAAAAAbGxt/2xsbf8AAAAAbGxt/2xsbf9sbG3/bGxt/2xsbf9sbG3/bGxt/2xsbf8AAAAAbGxt/2xsbf8AAAAAAAAAAGxsbf9sbG3/AAAAAGxsbf9sbG3/bGx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///+H////DwAA/g8AAA%3D%3D"), auto';
    

    You can upload and convert any file to base-64 format at this page. Trim the whitespace, and prefix data:image/png;base64,.

  2. Instead of using data-URIs, an image can also be uploaded to a file host, and embedded as follows:

    document.documentElement.style.cursor = 'url("http://sstatic/stackoverflow/img/favicon.ico"), auto';
    

See also:

  • MDN: CSS cursor property
  • MDN: Using URL values for the cursor property
发布评论

评论列表(0)

  1. 暂无评论