问题描述

我想在我正在创建的 HTML /JavaScript 应用程序中显示一个’refresh’ 符号,但是我不想使任何 HTTP 请求加载图像。所有主流浏览器如何可靠地执行此操作?

我可以找到最接近的 Unicode 值是:↺(↺) 但箭头指向错误的方式。

有没有另一个 Unicode 值可以工作?

有没有 Webdings 字体可以使用? (他们通常不会在 Opera 和 Firefox 中工作。)

有没有办法在 JS 中使用由 JavaScript 提供的一个 String 提供的 base64 图像源创建一个图像?

编辑:

这样做的原因不是开销。正在创建的应用程序必须是单个 JavaScript 文件中的 self-contained,而不需要任何其他依赖关系。它是可以添加到任何项目的开发实用程序,而不需要网络。

最佳解决办法

如果浏览器支持从 Base64 字符串加载图像数据,可以使用 img 并将其 src 设置为以下格式:

data:[<mediatype>][;base64],<data>

例如:

data:image/gif;base64,R0lGODl...

JSFiddle Demo

次佳解决办法

如果您发现字符(↻),即 U + 21BB CLOCKWISE OPEN CIRCLE ARROW,可以满足您的需求 (其箭头可能不会像通用重新加载图标中一样),那么您可以使用它相当安全:

<style>
.reload { font-family: Lucida Sans Unicode }
</style>

<span class=reload>↻</span>

Lucida Sans Unicode 字体包含符号,几乎可以在任何 Windows 系统中使用,您可以发现这些天。根据 MS info,它甚至包括在 Windows 98(和 Windows 95 plus) 中。

可以合理地期望 Windows 以外的系统具有某种字体的符号,并具有知道如何从其中的一些接收的浏览器。而 fonts containing it 具有相似的形状。 (但是,如果符号需要文体的文字匹配,则需要更详细地研究这种情况,并尝试确保字体列表中的所有字体,或者“fontstackâÂ它。)

第三种解决办法

Unicode 字符

至少有两个 Unicode 字符可以用作刷新或重新加载图标:

  • CLOCKWISE GAPPED CIRCLE ARROWU +27F3âŸÅ,

  • CLOCKWISE OPEN CIRCLE ARROWU + 21BB¢Â†Å¥

第一个不会出现在 iOS 7 或 8 或 9 中的 Safari 或 Chrome 中。所以给出了,并给出了 the answer by Paul D. White,我推荐使用第二个,CLOCKWISE OPEN CIRCLE ARROW

提示:如果您是 Mac 用户,请获取 free-of-cost 应用程序 UnicodeChecker 。您可以按名称搜索字符,您可以看到包含该字符的字形的字体列表 (在您的特定 Mac 上) 。

图标字体

使用可扩展的 vector-based 图标替代常规字符字形创建了一些字体。

第四种办法

顺时针相当于您的 Unicode 字符是,↻(虽然,作为评论中的 David C. Bishop 笔记,Chromebook 使用 #27f2,âŸÅ) 。

使用文本字符是一个选项,但 Windows XP 可能不显示字符 (取决于您使用的字符,以及您显示的字体) 。 @Jukka K. Korpela notes that this character is included in the Lucida Sans Unicode font, so it should work fine even in Windows XP.

就 Wingdings 字体而言,为了避免 HTTP 请求,您需要确信将要安装在最终用户的计算机上的相关字体。我不认为这是一个常见的 cross-platform Wingdings 字体 (我可能完全错了) 。

您可以从包含使用 JavaScript 的图像的 base64 编码表示形式的字符串创建一个图像。你可以这样做:

var image = new Image();
image.src = 'data:image/gif;base64,' + BASE64_ENCODED_STRING; // Replace gif with whatever image format it actually is

尽管如此,data-uri 在 Internet Explorer 8 和更早版本中可能无法正常工作,哪些 Windows XP 用户可能会被卡住。 (而且您不需要 JavaScript,因此您可以使用 src 属性将<img> 标签放在页面中。)

第五种办法

Unicode 6 字符

「时钟下降和 UPWARDS 打开圆圈」(U + 1F503)ĒŸÂ「ƒ

令人遗憾的字体支持字形不是最好的,所以 「所有主要浏览器」 可能是一个伸展…: – /

第六种办法

你提到 「所有主要的浏览器」,所以 let’假设:

  1. 不是每个人都有 javascript

  2. 不是每个人都可以嵌入字体 (黑莓等)

  3. 不是每个人都支持所有的 UNCODE 表示 (Windows XP)

  4. 不是每个人都支持内联数据 uri’(IE7-)

那么留下什么?我建议一个 HTML1 兼容的 IMG 元素,也将在第一次加载后被缓存:

<img src="/refresh.gif" alt="refresh">

它将在我知道的每个浏览器中工作,除了 LYNX 和其他纯 text-based 浏览器。但是,他们会显示”refresh”,所以¢Â¢¢™是罚款。

如果您真的想避免对”modern” 浏览器额外的 2bytes 请求,则可以对 IE7-使用条件注释:

<!--[if lte IE 7]>
<img src="refresh.gif" alt="refresh">
<![endif]-->

其余的还有这样的事情:

<img src="data:image/gif;base64,..." alt="refresh">

如果您最新的新功能可以使用 CSS 框阴影 (demo):

.refresh{
    border-radius: 0;
    display: inline-block;
    width: 1px;
    height: 1px;
    box-shadow: 0px 0px rgba(0,0,0,0),0px 1px rgba(0,0,0,0),0px 2px rgba(0,0,0,0),0px 3px rgba(0,0,0,0),0px 4px rgba(0,0,0,0),0px 5px rgba(0,0,0,0),0px 6px rgba(0,0,0,0),0px 7px rgba(0,0,0,0),0px 8px rgba(0,0,0,0),0px 9px rgba(0,0,0,0),0px 10px rgba(0,0,0,0),0px 11px rgba(0,0,0,0),0px 12px rgba(0,0,0,0),0px 13px rgba(0,0,0,0),0px 14px rgba(0,0,0,0),0px 15px rgba(0,0,0,0),1px 0px rgba(0,0,0,0),1px 1px rgba(0,0,0,0),1px 2px rgba(0,0,0,0),1px 3px rgba(0,0,0,0),1px 4px rgba(0,0,0,0),1px 5px rgba(0,0,0,0),1px 6px rgba(0,0,0,0),1px 7px rgba(0,0,0,0),1px 8px rgba(0,0,0,0),1px 9px rgba(0,0,0,0),1px 10px rgba(0,0,0,0),1px 11px rgba(0,0,0,0),1px 12px rgba(0,0,0,0),1px 13px rgba(0,0,0,0),1px 14px rgba(0,0,0,0),1px 15px rgba(0,0,0,0),2px 0px rgba(0,0,0,0),2px 1px rgba(0,0,0,0),2px 2px rgba(0,0,0,0),2px 3px rgba(0,0,0,0),2px 4px rgba(0,0,0,0),2px 5px rgba(0,0,0,0.06),2px 6px rgba(0,0,0,0.55),2px 7px rgba(0,0,0,0.79),2px 8px rgba(0,0,0,0.86),2px 9px rgba(0,0,0,0.86),2px 10px rgba(0,0,0,0.65),2px 11px rgba(0,0,0,0.31),2px 12px rgba(0,0,0,0),2px 13px rgba(0,0,0,0),2px 14px rgba(0,0,0,0),2px 15px rgba(0,0,0,0),3px 0px rgba(0,0,0,0),3px 1px rgba(0,0,0,0),3px 2px rgba(0,0,0,0),3px 3px rgba(0,0,0,0),3px 4px rgba(0,0,0,0.25),3px 5px rgba(0,0,0,0.83),3px 6px rgba(0,0,0,0.86),3px 7px rgba(0,0,0,0.83),3px 8px rgba(0,0,0,0.69),3px 9px rgba(0,0,0,0.74),3px 10px rgba(0,0,0,0.86),3px 11px rgba(0,0,0,0.86),3px 12px rgba(0,0,0,0.6),3px 13px rgba(0,0,0,0.06),3px 14px rgba(0,0,0,0),3px 15px rgba(0,0,0,0),4px 0px rgba(0,0,0,0),4px 1px rgba(0,0,0,0),4px 2px rgba(0,0,0,0),4px 3px rgba(0,0,0,0.25),4px 4px rgba(0,0,0,0.83),4px 5px rgba(0,0,0,0.83),4px 6px rgba(0,0,0,0.43),4px 7px rgba(0,0,0,0),4px 8px rgba(0,0,0,0),4px 9px rgba(0,0,0,0),4px 10px rgba(0,0,0,0.13),4px 11px rgba(0,0,0,0.69),4px 12px rgba(0,0,0,0.86),4px 13px rgba(0,0,0,0.6),4px 14px rgba(0,0,0,0),4px 15px rgba(0,0,0,0),5px 0px rgba(0,0,0,0),5px 1px rgba(0,0,0,0),5px 2px rgba(0,0,0,0),5px 3px rgba(0,0,0,0.69),5px 4px rgba(0,0,0,0.86),5px 5px rgba(0,0,0,0.25),5px 6px rgba(0,0,0,0),5px 7px rgba(0,0,0,0),5px 8px rgba(0,0,0,0),5px 9px rgba(0,0,0,0),5px 10px rgba(0,0,0,0),5px 11px rgba(0,0,0,0),5px 12px rgba(0,0,0,0.69),5px 13px rgba(0,0,0,0.86),5px 14px rgba(0,0,0,0.25),5px 15px rgba(0,0,0,0),6px 0px rgba(0,0,0,0),6px 1px rgba(0,0,0,0),6px 2px rgba(0,0,0,0.13),6px 3px rgba(0,0,0,0.86),6px 4px rgba(0,0,0,0.6),6px 5px rgba(0,0,0,0),6px 6px rgba(0,0,0,0),6px 7px rgba(0,0,0,0),6px 8px rgba(0,0,0,0),6px 9px rgba(0,0,0,0),6px 10px rgba(0,0,0,0),6px 11px rgba(0,0,0,0),6px 12px rgba(0,0,0,0.13),6px 13px rgba(0,0,0,0.86),6px 14px rgba(0,0,0,0.65),6px 15px rgba(0,0,0,0),7px 0px rgba(0,0,0,0),7px 1px rgba(0,0,0,0),7px 2px rgba(0,0,0,0.43),7px 3px rgba(0,0,0,0.86),7px 4px rgba(0,0,0,0.31),7px 5px rgba(0,0,0,0),7px 6px rgba(0,0,0,0),7px 7px rgba(0,0,0,0),7px 8px rgba(0,0,0,0),7px 9px rgba(0,0,0,0),7px 10px rgba(0,0,0,0),7px 11px rgba(0,0,0,0),7px 12px rgba(0,0,0,0),7px 13px rgba(0,0,0,0.75),7px 14px rgba(0,0,0,0.83),7px 15px rgba(0,0,0,0),8px 0px rgba(0,0,0,0),8px 1px rgba(0,0,0,0.43),8px 2px rgba(0,0,0,0.69),8px 3px rgba(0,0,0,0.86),8px 4px rgba(0,0,0,0.6),8px 5px rgba(0,0,0,0.31),8px 6px rgba(0,0,0,0),8px 7px rgba(0,0,0,0),8px 8px rgba(0,0,0,0),8px 9px rgba(0,0,0,0),8px 10px rgba(0,0,0,0),8px 11px rgba(0,0,0,0),8px 12px rgba(0,0,0,0),8px 13px rgba(0,0,0,0.69),8px 14px rgba(0,0,0,0.86),8px 15px rgba(0,0,0,0),9px 0px rgba(0,0,0,0),9px 1px rgba(0,0,0,0.48),9px 2px rgba(0,0,0,0.86),9px 3px rgba(0,0,0,0.86),9px 4px rgba(0,0,0,0.86),9px 5px rgba(0,0,0,0.13),9px 6px rgba(0,0,0,0),9px 7px rgba(0,0,0,0),9px 8px rgba(0,0,0,0),9px 9px rgba(0,0,0,0),9px 10px rgba(0,0,0,0),9px 11px rgba(0,0,0,0),9px 12px rgba(0,0,0,0),9px 13px rgba(0,0,0,0.75),9px 14px rgba(0,0,0,0.83),9px 15px rgba(0,0,0,0),10px 0px rgba(0,0,0,0),10px 1px rgba(0,0,0,0),10px 2px rgba(0,0,0,0.79),10px 3px rgba(0,0,0,0.86),10px 4px rgba(0,0,0,0.6),10px 5px rgba(0,0,0,0),10px 6px rgba(0,0,0,0),10px 7px rgba(0,0,0,0),10px 8px rgba(0,0,0,0),10px 9px rgba(0,0,0,0),10px 10px rgba(0,0,0,0),10px 11px rgba(0,0,0,0),10px 12px rgba(0,0,0,0.13),10px 13px rgba(0,0,0,0.86),10px 14px rgba(0,0,0,0.65),10px 15px rgba(0,0,0,0),11px 0px rgba(0,0,0,0),11px 1px rgba(0,0,0,0),11px 2px rgba(0,0,0,0.38),11px 3px rgba(0,0,0,0.83),11px 4px rgba(0,0,0,0.06),11px 5px rgba(0,0,0,0),11px 6px rgba(0,0,0,0),11px 7px rgba(0,0,0,0),11px 8px rgba(0,0,0,0),11px 9px rgba(0,0,0,0),11px 10px rgba(0,0,0,0),11px 11px rgba(0,0,0,0),11px 12px rgba(0,0,0,0.69),11px 13px rgba(0,0,0,0.86),11px 14px rgba(0,0,0,0.25),11px 15px rgba(0,0,0,0),12px 0px rgba(0,0,0,0),12px 1px rgba(0,0,0,0),12px 2px rgba(0,0,0,0),12px 3px rgba(0,0,0,0.31),12px 4px rgba(0,0,0,0),12px 5px rgba(0,0,0,0),12px 6px rgba(0,0,0,0.25),12px 7px rgba(0,0,0,0),12px 8px rgba(0,0,0,0),12px 9px rgba(0,0,0,0),12px 10px rgba(0,0,0,0.13),12px 11px rgba(0,0,0,0.69),12px 12px rgba(0,0,0,0.86),12px 13px rgba(0,0,0,0.6),12px 14px rgba(0,0,0,0),12px 15px rgba(0,0,0,0),13px 0px rgba(0,0,0,0),13px 1px rgba(0,0,0,0),13px 2px rgba(0,0,0,0),13px 3px rgba(0,0,0,0),13px 4px rgba(0,0,0,0),13px 5px rgba(0,0,0,0.06),13px 6px rgba(0,0,0,0.83),13px 7px rgba(0,0,0,0.83),13px 8px rgba(0,0,0,0.69),13px 9px rgba(0,0,0,0.75),13px 10px rgba(0,0,0,0.86),13px 11px rgba(0,0,0,0.86),13px 12px rgba(0,0,0,0.6),13px 13px rgba(0,0,0,0.06),13px 14px rgba(0,0,0,0),13px 15px rgba(0,0,0,0),14px 0px rgba(0,0,0,0),14px 1px rgba(0,0,0,0),14px 2px rgba(0,0,0,0),14px 3px rgba(0,0,0,0),14px 4px rgba(0,0,0,0),14px 5px rgba(0,0,0,0.13),14px 6px rgba(0,0,0,0.55),14px 7px rgba(0,0,0,0.79),14px 8px rgba(0,0,0,0.86),14px 9px rgba(0,0,0,0.86),14px 10px rgba(0,0,0,0.65),14px 11px rgba(0,0,0,0.31),14px 12px rgba(0,0,0,0),14px 13px rgba(0,0,0,0),14px 14px rgba(0,0,0,0),14px 15px rgba(0,0,0,0),15px 0px rgba(0,0,0,0),15px 1px rgba(0,0,0,0),15px 2px rgba(0,0,0,0),15px 3px rgba(0,0,0,0),15px 4px rgba(0,0,0,0),15px 5px rgba(0,0,0,0),15px 6px rgba(0,0,0,0),15px 7px rgba(0,0,0,0),15px 8px rgba(0,0,0,0),15px 9px rgba(0,0,0,0),15px 10px rgba(0,0,0,0),15px 11px rgba(0,0,0,0),15px 12px rgba(0,0,0,0),15px 13px rgba(0,0,0,0),15px 14px rgba(0,0,0,0),15px 15px rgba(0,0,0,0);
}

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。