Chrome扩展程序自定义光标
我构建了一个 Google Chrome 扩展程序,在网站中放置一些 IMG 标签。 :hover 上的此 img 标记必须显示自定义光标。该扩展使用 jQuery 作为其注入的核心脚本。我尝试了以下方法:
1.
var cursor = 'url('+chrome.extension.getURL('icons/cursor.cur')+')';
$('#myImgId').css({
'position': 'absolute',
'top':'5px',
'left':'5px',
'cursor':cursor
});
这是最好的方法。在较小的网站上,它显示光标。在加载速度较慢的网站上则不然。但在小网站上有时会失败。
2.
var cursor = 'url('+chrome.extension.getURL('icons/cursor.cur')+')';
$('<style>#myImgId{cursor:'+cursor+'}</style>').appendTo('head');
这根本没有做任何事情。
3.
在manifest.json中我注入了css。
"content_scripts": [
{
"matches": ["http://*/*"],
"css": ["css/style.css"],
"js": ["j/c.js", "j/s.js"]
}
css文件只有cursor:url(icons/cursor.cur),因为我不知道如何在css文件中获取真实的url。这根本不起作用。我认为它一定是这样工作的,但我在 code.google 上没有找到这方面的参考。
I building an Google Chrome extension that place some IMG tag in sites. This img tag on :hover must show a custom cursor. The extension uses jQuery as its injected core script. I tried the following methods:
1.
var cursor = 'url('+chrome.extension.getURL('icons/cursor.cur')+')';
$('#myImgId').css({
'position': 'absolute',
'top':'5px',
'left':'5px',
'cursor':cursor
});
This is the best working. On smaller sites its shows the cursor. On slower loading sites it does not. But on little sites it fails sometimes.
2.
var cursor = 'url('+chrome.extension.getURL('icons/cursor.cur')+')';
$('<style>#myImgId{cursor:'+cursor+'}</style>').appendTo('head');
This did nothing at all.
3.
In manifest.json I injected the css.
"content_scripts": [
{
"matches": ["http://*/*"],
"css": ["css/style.css"],
"js": ["j/c.js", "j/s.js"]
}
The css file just had the cursor:url(icons/cursor.cur) as I don't have idea, how to get real url in a css file. This isn't working at all. I think it must work like this, I didn't find reference for this on code.google though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明,要使其正常工作,CSS 规则应编写为:
{cursor:url(...),default;}
对于您的第三种方法,请在 css 中尝试此操作(由于错误而不起作用)As it turned out to make it work css rule should be written as:
{cursor:url(...),default;}
For your 3rd method try this in css(doesn't work due to a bug)为此,您不需要 Chrome 扩展程序; CSS 的一项标准功能是能够在元素上移动时更改光标,包括将其更改为自定义图像的能力。
您应该能够将这样的内容添加到 CSS 中:
根本不需要编写任何脚本。
然而,此功能在各种浏览器中都存在错误、怪癖和实现差异。
要了解的最大的怪癖是 Internet Explorer 期望光标文件是 .cur 文件,而所有其他浏览器期望常规图像文件(例如 .gif)。如果您希望它能够跨浏览器工作,则需要提供两个版本的图标,并在 CSS 中使用特定于浏览器的测试或 hack 来使其选择正确的版本。
可以在这里找到 CSS
cursor
功能及其怪癖和各种浏览器支持的非常好的总结:http://www.quirksmode.org/css/cursor.html该页面还指出“图像在 Chrome 中出现乱码”。这对你来说可能是个坏消息,但测试已经有一段时间没有更新了,因此该信息适用于 Chrome 5,所以如果那里有错误,它现在很可能已经修复了。
You shouldn't need a Chrome extension for this; it's a standard feature of CSS to be able to change the cursor when moving over elements, including the ability to change it to a custom image.
You should be able to just add something like this to your CSS:
without having to do any scripting at all.
However there are bugs, quirks and implementation differences in this feature in a variety of browsers.
The biggest quirk to know about is that Internet Explorer expects the cursor file to be a .cur file, whereas all other browsers expect a regular image file (eg a .gif). If you want it to work cross-browser, you will need to provide two versions of your icon and use a browser-specific test or hack in your CSS to make it pick the right one.
A very good summary of the CSS
cursor
feature with its quirks and support in various browsers can be found here: http://www.quirksmode.org/css/cursor.htmlThis page also states that "the image is garbled in Chrome". This may be bad news for you, but the test hasn't been updated for a while so that info applies to Chrome 5, so if there was bug there it may well have been fixed by now.
要添加:
要删除:
不要忘记将 .cur 文件添加到清单中:
To add:
To remove:
Do not forget to add the .cur file to the manifest: